blob: c739fe776f390611ff4b5d93b6194b74e350fed6 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Hanno Becker58fccf22019-02-06 14:30:46 +0000371int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
372 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
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 }
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000396
397#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
398 if( src->peer_cert_digest != NULL )
399 {
400 dst->peer_cert_digest_len = src->peer_cert_digest_len;
401 dst->peer_cert_digest =
402 mbedtls_calloc( 1, dst->peer_cert_digest_len );
403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
409 }
410#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200413
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200414#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200415 if( src->ticket != NULL )
416 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200417 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200418 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200420
421 memcpy( dst->ticket, src->ticket, src->ticket_len );
422 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200423#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200424
425 return( 0 );
426}
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
429int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200430 const unsigned char *key_enc, const unsigned char *key_dec,
431 size_t keylen,
432 const unsigned char *iv_enc, const unsigned char *iv_dec,
433 size_t ivlen,
434 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200435 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
437int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
438int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
441#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000442
Paul Bakker5121ce52009-01-03 21:22:43 +0000443/*
444 * Key material generation
445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200447static int ssl3_prf( const unsigned char *secret, size_t slen,
448 const char *label,
449 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000450 unsigned char *dstbuf, size_t dlen )
451{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100452 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000453 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200454 mbedtls_md5_context md5;
455 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000456 unsigned char padding[16];
457 unsigned char sha1sum[20];
458 ((void)label);
459
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200460 mbedtls_md5_init( &md5 );
461 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200462
Paul Bakker5f70b252012-09-13 14:23:06 +0000463 /*
464 * SSLv3:
465 * block =
466 * MD5( secret + SHA1( 'A' + secret + random ) ) +
467 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
468 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
469 * ...
470 */
471 for( i = 0; i < dlen / 16; i++ )
472 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200473 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000474
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100481 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100483 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100484 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000485
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100486 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100487 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100488 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100489 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100490 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100491 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100492 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100493 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000494 }
495
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200497 mbedtls_md5_free( &md5 );
498 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000499
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500500 mbedtls_platform_zeroize( padding, sizeof( padding ) );
501 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000502
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200508static int tls1_prf( const unsigned char *secret, size_t slen,
509 const char *label,
510 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000511 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000512{
Paul Bakker23986e52011-04-24 08:57:21 +0000513 size_t nb, hs;
514 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200515 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 unsigned char tmp[128];
517 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 const mbedtls_md_info_t *md_info;
519 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100520 int ret;
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 hs = ( slen + 1 ) / 2;
528 S1 = secret;
529 S2 = secret + slen - hs;
530
531 nb = strlen( label );
532 memcpy( tmp + 20, label, nb );
533 memcpy( tmp + 20 + nb, random, rlen );
534 nb += rlen;
535
536 /*
537 * First compute P_md5(secret,label+random)[0..dlen]
538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100543 return( ret );
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
546 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
547 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 for( i = 0; i < dlen; i += 16 )
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_md_hmac_reset ( &md_ctx );
552 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
553 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_reset ( &md_ctx );
556 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
557 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
560
561 for( j = 0; j < k; j++ )
562 dstbuf[i + j] = h_i[j];
563 }
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100566
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 /*
568 * XOR out with P_sha1(secret,label+random)[0..dlen]
569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100574 return( ret );
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
577 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
578 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 for( i = 0; i < dlen; i += 20 )
581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_hmac_reset ( &md_ctx );
583 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
584 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_md_hmac_reset ( &md_ctx );
587 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
588 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
591
592 for( j = 0; j < k; j++ )
593 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
594 }
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100597
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500598 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
599 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 return( 0 );
602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
606static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607 const unsigned char *secret, size_t slen,
608 const char *label,
609 const unsigned char *random, size_t rlen,
610 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611{
612 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
616 const mbedtls_md_info_t *md_info;
617 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100618 int ret;
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100626
627 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000629
630 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100631 memcpy( tmp + md_len, label, nb );
632 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000633 nb += rlen;
634
635 /*
636 * Compute P_<hash>(secret, label + random)[0..dlen]
637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100639 return( ret );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
642 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
643 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100644
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100645 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_md_hmac_reset ( &md_ctx );
648 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
649 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_md_hmac_reset ( &md_ctx );
652 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
653 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000654
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100655 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000656
657 for( j = 0; j < k; j++ )
658 dstbuf[i + j] = h_i[j];
659 }
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100662
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500663 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
664 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000665
666 return( 0 );
667}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100670static int tls_prf_sha256( const unsigned char *secret, size_t slen,
671 const char *label,
672 const unsigned char *random, size_t rlen,
673 unsigned char *dstbuf, size_t dlen )
674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100676 label, random, rlen, dstbuf, dlen ) );
677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200681static int tls_prf_sha384( const unsigned char *secret, size_t slen,
682 const char *label,
683 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000684 unsigned char *dstbuf, size_t dlen )
685{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100687 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_SHA512_C */
690#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
695 defined(MBEDTLS_SSL_PROTO_TLS1_1)
696static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200697#endif
Paul Bakker380da532012-04-18 16:10:25 +0000698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200700static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702#endif
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200705static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200707#endif
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
710#if defined(MBEDTLS_SHA256_C)
711static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200712static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200714#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_SHA512_C)
717static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200718static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000722
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200723/* Type for the TLS PRF */
724typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
725 const unsigned char *, size_t,
726 unsigned char *, size_t);
727
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200728/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200729 * Populate a transform structure with session keys and all the other
730 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200731 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200732 * Parameters:
733 * - [in/out]: transform: structure to populate
734 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200735 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200736 * - [in] ciphersuite
737 * - [in] master
738 * - [in] encrypt_then_mac
739 * - [in] trunc_hmac
740 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200741 * - [in] tls_prf: pointer to PRF to use for key derivation
742 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200743 * - [in] minor_ver: SSL/TLS minor version
744 * - [in] endpoint: client or server
745 * - [in] ssl: optionally used for:
746 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
747 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
748 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200749 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200750static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200751 int ciphersuite,
752 const unsigned char master[48],
753#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
754 int encrypt_then_mac,
755#endif
756#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
757 int trunc_hmac,
758#endif
759#if defined(MBEDTLS_ZLIB_SUPPORT)
760 int compression,
761#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200762 ssl_tls_prf_t tls_prf,
763 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200764 int minor_ver,
765 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200766 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000767{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200768 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 unsigned char keyblk[256];
770 unsigned char *key1;
771 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100772 unsigned char *mac_enc;
773 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000774 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200775 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000776 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000777 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 const mbedtls_cipher_info_t *cipher_info;
779 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100780
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200781#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
782 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
783 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200784 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200785 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000786#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000787
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200788 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200790 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000791#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200792 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200794 /*
795 * Get various info structures
796 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200797 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 if( ciphersuite_info == NULL )
799 {
800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
803 }
804
Hanno Becker8759e162017-12-27 21:34:08 +0000805 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100806 if( cipher_info == NULL )
807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000809 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100811 }
812
Hanno Becker8759e162017-12-27 21:34:08 +0000813 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100814 if( md_info == NULL )
815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000817 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100819 }
820
Hanno Beckera5a2b082019-05-15 14:03:01 +0100821#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100822 /* Copy own and peer's CID if the use of the CID
823 * extension has been negotiated. */
824 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
825 {
826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100827
Hanno Becker4932f9f2019-05-03 15:23:51 +0100828 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100829 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100830 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100831 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100832
833 transform->out_cid_len = ssl->handshake->peer_cid_len;
834 memcpy( transform->out_cid, ssl->handshake->peer_cid,
835 ssl->handshake->peer_cid_len );
836 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
837 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100838 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100839#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200842 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000843 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200844 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100845 if( ret != 0 )
846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100848 return( ret );
849 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200852 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200853 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200854 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 /*
858 * Determine the appropriate key, IV and MAC length.
859 */
Paul Bakker68884e32013-01-07 18:20:04 +0100860
Hanno Beckere7f2df02017-12-27 08:17:40 +0000861 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200862
Hanno Beckerf1229442018-01-03 15:32:31 +0000863#if defined(MBEDTLS_GCM_C) || \
864 defined(MBEDTLS_CCM_C) || \
865 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 cipher_info->mode == MBEDTLS_MODE_CCM ||
868 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 {
Hanno Becker8759e162017-12-27 21:34:08 +0000870 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200872 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000873 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000874 transform->taglen =
875 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200877 /* All modes haves 96-bit IVs;
878 * GCM and CCM has 4 implicit and 8 explicit bytes
879 * ChachaPoly has all 12 bytes implicit
880 */
Paul Bakker68884e32013-01-07 18:20:04 +0100881 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200882 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
883 transform->fixed_ivlen = 12;
884 else
885 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200886
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200887 /* Minimum length of encrypted record */
888 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000889 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100890 }
891 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000892#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
893#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
894 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
895 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100896 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200897 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
899 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200902 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100903 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200905 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000906 mac_key_len = mbedtls_md_get_size( md_info );
907 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200910 /*
911 * If HMAC is to be truncated, we shall keep the leftmost bytes,
912 * (rfc 6066 page 13 or rfc 2104 section 4),
913 * so we only need to adjust the length here.
914 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200915 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000918
919#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
920 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000921 * HMAC implementation which also truncates the key
922 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000923 mac_key_len = transform->maclen;
924#endif
925 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200927
928 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100929 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200931 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200933 transform->minlen = transform->maclen;
934 else
Paul Bakker68884e32013-01-07 18:20:04 +0100935 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200936 /*
937 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100938 * 1. if EtM is in use: one block plus MAC
939 * otherwise: * first multiple of blocklen greater than maclen
940 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200943 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100944 {
945 transform->minlen = transform->maclen
946 + cipher_info->block_size;
947 }
948 else
949#endif
950 {
951 transform->minlen = transform->maclen
952 + cipher_info->block_size
953 - transform->maclen % cipher_info->block_size;
954 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200957 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
958 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100960 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200963 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
964 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965 {
966 transform->minlen += transform->ivlen;
967 }
968 else
969#endif
970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200973 }
Paul Bakker68884e32013-01-07 18:20:04 +0100974 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000976 else
977#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
978 {
979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
981 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Hanno Beckere7f2df02017-12-27 08:17:40 +0000983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
984 (unsigned) keylen,
985 (unsigned) transform->minlen,
986 (unsigned) transform->ivlen,
987 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989 /*
990 * Finally setup the cipher contexts, IVs and MAC secrets.
991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200993 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000995 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000996 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Paul Bakker68884e32013-01-07 18:20:04 +0100998 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001001 /*
1002 * This is not used in TLS v1.1.
1003 */
Paul Bakker48916f92012-09-16 19:57:18 +00001004 iv_copy_len = ( transform->fixed_ivlen ) ?
1005 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001006 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1007 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001008 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 }
1010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_CLI_C */
1012#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001013 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001015 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001016 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Hanno Becker81c7b182017-11-09 18:39:33 +00001018 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001019 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001021 /*
1022 * This is not used in TLS v1.1.
1023 */
Paul Bakker48916f92012-09-16 19:57:18 +00001024 iv_copy_len = ( transform->fixed_ivlen ) ?
1025 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001026 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1027 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001028 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Hanno Becker92231322018-01-03 15:32:51 +00001037#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001039 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001040 {
Hanno Becker92231322018-01-03 15:32:51 +00001041 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001045 }
1046
Hanno Becker81c7b182017-11-09 18:39:33 +00001047 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1048 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001049 }
1050 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1052#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1053 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001054 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001055 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001056 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1057 For AEAD-based ciphersuites, there is nothing to do here. */
1058 if( mac_key_len != 0 )
1059 {
1060 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1061 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1062 }
Paul Bakker68884e32013-01-07 18:20:04 +01001063 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001064 else
1065#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001069 }
Hanno Becker92231322018-01-03 15:32:51 +00001070#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1073 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001074 {
1075 int ret = 0;
1076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001078
Hanno Beckere7f2df02017-12-27 08:17:40 +00001079 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001080 transform->iv_enc, transform->iv_dec,
1081 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001082 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001083 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1086 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001087 }
1088 }
Hanno Becker92231322018-01-03 15:32:51 +00001089#else
1090 ((void) mac_dec);
1091 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001093
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001094#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1095 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001096 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001097 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001098 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001099 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 iv_copy_len );
1101 }
1102#endif
1103
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001104 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001105 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001106 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001108 return( ret );
1109 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001110
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 cipher_info ) ) != 0 )
1113 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001115 return( ret );
1116 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001119 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001123 return( ret );
1124 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001127 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131 return( ret );
1132 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_CIPHER_MODE_CBC)
1135 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1138 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001141 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001142 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1145 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001148 return( ret );
1149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001153 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001155 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001157 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001160
Paul Bakker48916f92012-09-16 19:57:18 +00001161 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1162 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001163
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001164 if( deflateInit( &transform->ctx_deflate,
1165 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001166 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1169 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001170 }
1171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 return( 0 );
1175}
1176
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001177/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001178 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001179 *
1180 * Inputs:
1181 * - SSL/TLS minor version
1182 * - hash associated with the ciphersuite (only used by TLS 1.2)
1183 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001184 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001185 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1186 */
1187static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1188 int minor_ver,
1189 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001190{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001191#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1192 (void) hash;
1193#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001195#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001196 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001197 {
1198 handshake->tls_prf = ssl3_prf;
1199 handshake->calc_verify = ssl_calc_verify_ssl;
1200 handshake->calc_finished = ssl_calc_finished_ssl;
1201 }
1202 else
1203#endif
1204#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001205 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001206 {
1207 handshake->tls_prf = tls1_prf;
1208 handshake->calc_verify = ssl_calc_verify_tls;
1209 handshake->calc_finished = ssl_calc_finished_tls;
1210 }
1211 else
1212#endif
1213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1214#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001215 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1216 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001217 {
1218 handshake->tls_prf = tls_prf_sha384;
1219 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1220 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1221 }
1222 else
1223#endif
1224#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001225 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001226 {
1227 handshake->tls_prf = tls_prf_sha256;
1228 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1229 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1230 }
1231 else
1232#endif
1233#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1234 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1236 }
1237
1238 return( 0 );
1239}
1240
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001241/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001242 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001243 *
1244 * Parameters:
1245 * [in/out] handshake
1246 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1247 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001248 * [out] master
1249 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001250 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001251static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001253 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254{
1255 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256
1257#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001258 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001259 (void) ssl;
1260#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001261
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001262 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001263 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1265 return( 0 );
1266 }
1267
1268 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1269 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001270
1271#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001272 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001273 {
1274 unsigned char session_hash[48];
1275 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001276
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001277 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001278
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001279 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1280 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001281
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001282 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001283 "extended master secret",
1284 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001285 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001286 }
1287 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001288#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001289 {
1290 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1291 "master secret",
1292 handshake->randbytes, 64,
1293 master, 48 );
1294 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001295 if( ret != 0 )
1296 {
1297 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1298 return( ret );
1299 }
1300
1301 mbedtls_platform_zeroize( handshake->premaster,
1302 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001303
1304 return( 0 );
1305}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001306
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001307int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1308{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001309 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001310 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1311 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001312
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1314
1315 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001316 ret = ssl_set_handshake_prfs( ssl->handshake,
1317 ssl->minor_ver,
1318 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001319 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001320 {
1321 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001322 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001323 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001324
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001325 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001326 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001327 ssl->session_negotiate->master,
1328 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001329 if( ret != 0 )
1330 {
1331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1332 return( ret );
1333 }
1334
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001335 /* Swap the client and server random values:
1336 * - MS derivation wanted client+server (RFC 5246 8.1)
1337 * - key derivation wants server+client (RFC 5246 6.3) */
1338 {
1339 unsigned char tmp[64];
1340 memcpy( tmp, ssl->handshake->randbytes, 64 );
1341 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1342 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1343 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1344 }
1345
1346 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001347 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001348 ssl->session_negotiate->ciphersuite,
1349 ssl->session_negotiate->master,
1350#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1351 ssl->session_negotiate->encrypt_then_mac,
1352#endif
1353#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1354 ssl->session_negotiate->trunc_hmac,
1355#endif
1356#if defined(MBEDTLS_ZLIB_SUPPORT)
1357 ssl->session_negotiate->compression,
1358#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001359 ssl->handshake->tls_prf,
1360 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001361 ssl->minor_ver,
1362 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001363 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001364 if( ret != 0 )
1365 {
1366 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1367 return( ret );
1368 }
1369
1370 /* We no longer need Server/ClientHello.random values */
1371 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1372 sizeof( ssl->handshake->randbytes ) );
1373
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001374 /* Allocate compression buffer */
1375#if defined(MBEDTLS_ZLIB_SUPPORT)
1376 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1377 ssl->compress_buf == NULL )
1378 {
1379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1380 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1381 if( ssl->compress_buf == NULL )
1382 {
1383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001384 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001385 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1386 }
1387 }
1388#endif
1389
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1391
1392 return( 0 );
1393}
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001396void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1397 unsigned char hash[36],
1398 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001399{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001400 mbedtls_md5_context md5;
1401 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 unsigned char pad_1[48];
1403 unsigned char pad_2[48];
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001407 mbedtls_md5_init( &md5 );
1408 mbedtls_sha1_init( &sha1 );
1409
1410 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1411 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Paul Bakker380da532012-04-18 16:10:25 +00001413 memset( pad_1, 0x36, 48 );
1414 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001416 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1417 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1418 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001420 mbedtls_md5_starts_ret( &md5 );
1421 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1422 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1423 mbedtls_md5_update_ret( &md5, hash, 16 );
1424 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001426 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1427 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1428 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001429
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001430 mbedtls_sha1_starts_ret( &sha1 );
1431 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1432 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1433 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1434 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001436 *hlen = 36;
1437
1438 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001441 mbedtls_md5_free( &md5 );
1442 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001443
Paul Bakker380da532012-04-18 16:10:25 +00001444 return;
1445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001449void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1450 unsigned char hash[36],
1451 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001452{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001453 mbedtls_md5_context md5;
1454 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001457
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001458 mbedtls_md5_init( &md5 );
1459 mbedtls_sha1_init( &sha1 );
1460
1461 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1462 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001463
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001464 mbedtls_md5_finish_ret( &md5, hash );
1465 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001467 *hlen = 36;
1468
1469 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001471
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001472 mbedtls_md5_free( &md5 );
1473 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001474
Paul Bakker380da532012-04-18 16:10:25 +00001475 return;
1476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1480#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001481void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1482 unsigned char hash[32],
1483 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001484{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001485 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001486
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001487 mbedtls_sha256_init( &sha256 );
1488
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001490
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001491 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001492 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001494 *hlen = 32;
1495
1496 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001498
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001499 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001500
Paul Bakker380da532012-04-18 16:10:25 +00001501 return;
1502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001506void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1507 unsigned char hash[48],
1508 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001509{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001510 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001512 mbedtls_sha512_init( &sha512 );
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001515
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001516 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001517 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001519 *hlen = 48;
1520
1521 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001524 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001525
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 return;
1527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_SHA512_C */
1529#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1532int 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 +02001533{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001534 unsigned char *p = ssl->handshake->premaster;
1535 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001536 const unsigned char *psk = ssl->conf->psk;
1537 size_t psk_len = ssl->conf->psk_len;
1538
1539 /* If the psk callback was called, use its result */
1540 if( ssl->handshake->psk != NULL )
1541 {
1542 psk = ssl->handshake->psk;
1543 psk_len = ssl->handshake->psk_len;
1544 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001545
1546 /*
1547 * PMS = struct {
1548 * opaque other_secret<0..2^16-1>;
1549 * opaque psk<0..2^16-1>;
1550 * };
1551 * with "other_secret" depending on the particular key exchange
1552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1554 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001555 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001556 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001558
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001559 *(p++) = (unsigned char)( psk_len >> 8 );
1560 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001561
1562 if( end < p || (size_t)( end - p ) < psk_len )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
1565 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001566 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001567 }
1568 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1570#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1571 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001572 {
1573 /*
1574 * other_secret already set by the ClientKeyExchange message,
1575 * and is 48 bytes long
1576 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001577 if( end - p < 2 )
1578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1579
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001580 *p++ = 0;
1581 *p++ = 48;
1582 p += 48;
1583 }
1584 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1586#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1587 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001588 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001589 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001590 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001591
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001592 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001594 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001595 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001598 return( ret );
1599 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001600 *(p++) = (unsigned char)( len >> 8 );
1601 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 p += len;
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605 }
1606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1608#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1609 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001611 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 size_t zlen;
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001615 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001616 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 return( ret );
1620 }
1621
1622 *(p++) = (unsigned char)( zlen >> 8 );
1623 *(p++) = (unsigned char)( zlen );
1624 p += zlen;
1625
Janos Follath3fbdada2018-08-15 10:26:53 +01001626 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1627 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628 }
1629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634 }
1635
1636 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001637 if( end - p < 2 )
1638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001639
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001640 *(p++) = (unsigned char)( psk_len >> 8 );
1641 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001642
1643 if( end < p || (size_t)( end - p ) < psk_len )
1644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1645
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001646 memcpy( p, psk, psk_len );
1647 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648
1649 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1650
1651 return( 0 );
1652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656/*
1657 * SSLv3.0 MAC functions
1658 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001659#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001660static void ssl_mac( mbedtls_md_context_t *md_ctx,
1661 const unsigned char *secret,
1662 const unsigned char *buf, size_t len,
1663 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001664 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001665{
1666 unsigned char header[11];
1667 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001668 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1670 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001671
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001672 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001674 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001675 else
Paul Bakker68884e32013-01-07 18:20:04 +01001676 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
1678 memcpy( header, ctr, 8 );
1679 header[ 8] = (unsigned char) type;
1680 header[ 9] = (unsigned char)( len >> 8 );
1681 header[10] = (unsigned char)( len );
1682
Paul Bakker68884e32013-01-07 18:20:04 +01001683 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 mbedtls_md_starts( md_ctx );
1685 mbedtls_md_update( md_ctx, secret, md_size );
1686 mbedtls_md_update( md_ctx, padding, padlen );
1687 mbedtls_md_update( md_ctx, header, 11 );
1688 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001689 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Paul Bakker68884e32013-01-07 18:20:04 +01001691 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 mbedtls_md_starts( md_ctx );
1693 mbedtls_md_update( md_ctx, secret, md_size );
1694 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001695 mbedtls_md_update( md_ctx, out, md_size );
1696 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001699
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001700/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001701 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001702#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001703 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1704 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1705 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1706/* This function makes sure every byte in the memory region is accessed
1707 * (in ascending addresses order) */
1708static void ssl_read_memory( unsigned char *p, size_t len )
1709{
1710 unsigned char acc = 0;
1711 volatile unsigned char force;
1712
1713 for( ; len != 0; p++, len-- )
1714 acc ^= *p;
1715
1716 force = acc;
1717 (void) force;
1718}
1719#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1720
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001721/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001723 */
Hanno Becker3307b532017-12-27 21:37:21 +00001724
Hanno Beckera5a2b082019-05-15 14:03:01 +01001725#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001726/* This functions transforms a DTLS plaintext fragment and a record content
1727 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001728 *
1729 * struct {
1730 * opaque content[DTLSPlaintext.length];
1731 * ContentType real_type;
1732 * uint8 zeros[length_of_padding];
1733 * } DTLSInnerPlaintext;
1734 *
1735 * Input:
1736 * - `content`: The beginning of the buffer holding the
1737 * plaintext to be wrapped.
1738 * - `*content_size`: The length of the plaintext in Bytes.
1739 * - `max_len`: The number of Bytes available starting from
1740 * `content`. This must be `>= *content_size`.
1741 * - `rec_type`: The desired record content type.
1742 *
1743 * Output:
1744 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1745 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1746 *
1747 * Returns:
1748 * - `0` on success.
1749 * - A negative error code if `max_len` didn't offer enough space
1750 * for the expansion.
1751 */
1752static int ssl_cid_build_inner_plaintext( unsigned char *content,
1753 size_t *content_size,
1754 size_t remaining,
1755 uint8_t rec_type )
1756{
1757 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001758 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1759 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1760 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001761
1762 /* Write real content type */
1763 if( remaining == 0 )
1764 return( -1 );
1765 content[ len ] = rec_type;
1766 len++;
1767 remaining--;
1768
1769 if( remaining < pad )
1770 return( -1 );
1771 memset( content + len, 0, pad );
1772 len += pad;
1773 remaining -= pad;
1774
1775 *content_size = len;
1776 return( 0 );
1777}
1778
Hanno Becker7dc25772019-05-20 15:08:01 +01001779/* This function parses a DTLSInnerPlaintext structure.
1780 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001781static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1782 size_t *content_size,
1783 uint8_t *rec_type )
1784{
1785 size_t remaining = *content_size;
1786
1787 /* Determine length of padding by skipping zeroes from the back. */
1788 do
1789 {
1790 if( remaining == 0 )
1791 return( -1 );
1792 remaining--;
1793 } while( content[ remaining ] == 0 );
1794
1795 *content_size = remaining;
1796 *rec_type = content[ remaining ];
1797
1798 return( 0 );
1799}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001800#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001801
Hanno Becker99abf512019-05-20 14:50:53 +01001802/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001803 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001804static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001805 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001806 mbedtls_record *rec )
1807{
Hanno Becker99abf512019-05-20 14:50:53 +01001808 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001809 *
1810 * additional_data = seq_num + TLSCompressed.type +
1811 * TLSCompressed.version + TLSCompressed.length;
1812 *
Hanno Becker99abf512019-05-20 14:50:53 +01001813 * For the CID extension, this is extended as follows
1814 * (quoting draft-ietf-tls-dtls-connection-id-05,
1815 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001816 *
1817 * additional_data = seq_num + DTLSPlaintext.type +
1818 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001819 * cid +
1820 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001821 * length_of_DTLSInnerPlaintext;
1822 */
1823
Hanno Becker3307b532017-12-27 21:37:21 +00001824 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1825 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001826 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001827
Hanno Beckera5a2b082019-05-15 14:03:01 +01001828#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001829 if( rec->cid_len != 0 )
1830 {
1831 memcpy( add_data + 11, rec->cid, rec->cid_len );
1832 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1833 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1834 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1835 *add_data_len = 13 + 1 + rec->cid_len;
1836 }
1837 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001838#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001839 {
1840 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1841 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1842 *add_data_len = 13;
1843 }
Hanno Becker3307b532017-12-27 21:37:21 +00001844}
1845
Hanno Becker611a83b2018-01-03 14:27:32 +00001846int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1847 mbedtls_ssl_transform *transform,
1848 mbedtls_record *rec,
1849 int (*f_rng)(void *, unsigned char *, size_t),
1850 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001853 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001854 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001855 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001856 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001857 size_t post_avail;
1858
1859 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001860#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001861 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001862 ((void) ssl);
1863#endif
1864
1865 /* The PRNG is used for dynamic IV generation that's used
1866 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1867#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1868 ( defined(MBEDTLS_AES_C) || \
1869 defined(MBEDTLS_ARIA_C) || \
1870 defined(MBEDTLS_CAMELLIA_C) ) && \
1871 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1872 ((void) f_rng);
1873 ((void) p_rng);
1874#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Hanno Becker3307b532017-12-27 21:37:21 +00001878 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001879 {
Hanno Becker3307b532017-12-27 21:37:21 +00001880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1881 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1882 }
Hanno Becker505089d2019-05-01 09:45:57 +01001883 if( rec == NULL
1884 || rec->buf == NULL
1885 || rec->buf_len < rec->data_offset
1886 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001887#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001888 || rec->cid_len != 0
1889#endif
1890 )
Hanno Becker3307b532017-12-27 21:37:21 +00001891 {
1892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001894 }
1895
Hanno Becker3307b532017-12-27 21:37:21 +00001896 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001897 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001899 data, rec->data_len );
1900
1901 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1902
1903 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1904 {
1905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1906 (unsigned) rec->data_len,
1907 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1909 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001910
Hanno Beckera5a2b082019-05-15 14:03:01 +01001911#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001912 /*
1913 * Add CID information
1914 */
1915 rec->cid_len = transform->out_cid_len;
1916 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1917 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001918
1919 if( rec->cid_len != 0 )
1920 {
1921 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001922 * Wrap plaintext into DTLSInnerPlaintext structure.
1923 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001924 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001925 * Note that this changes `rec->data_len`, and hence
1926 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001927 */
1928 if( ssl_cid_build_inner_plaintext( data,
1929 &rec->data_len,
1930 post_avail,
1931 rec->type ) != 0 )
1932 {
1933 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1934 }
1935
1936 rec->type = MBEDTLS_SSL_MSG_CID;
1937 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001938#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001939
Hanno Becker92c930f2019-04-29 17:31:37 +01001940 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1941
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001943 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001945#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 if( mode == MBEDTLS_MODE_STREAM ||
1947 ( mode == MBEDTLS_MODE_CBC
1948#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001949 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001950#endif
1951 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 {
Hanno Becker3307b532017-12-27 21:37:21 +00001953 if( post_avail < transform->maclen )
1954 {
1955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1956 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1957 }
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001960 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001961 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001962 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001963 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1964 data, rec->data_len, rec->ctr, rec->type, mac );
1965 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001966 }
1967 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001968#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1970 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001971 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001972 {
Hanno Becker992b6872017-11-09 18:57:39 +00001973 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1974
Hanno Beckere83efe62019-04-29 13:52:53 +01001975 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001976
Hanno Becker3307b532017-12-27 21:37:21 +00001977 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001978 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001979 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1980 data, rec->data_len );
1981 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1982 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1983
1984 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001985 }
1986 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001987#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001991 }
1992
Hanno Becker3307b532017-12-27 21:37:21 +00001993 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1994 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001995
Hanno Becker3307b532017-12-27 21:37:21 +00001996 rec->data_len += transform->maclen;
1997 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001998 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001999 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002000#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002002 /*
2003 * Encrypt
2004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2006 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002008 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002009 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002011 "including %d bytes of padding",
2012 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Hanno Becker3307b532017-12-27 21:37:21 +00002014 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2015 transform->iv_enc, transform->ivlen,
2016 data, rec->data_len,
2017 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002020 return( ret );
2021 }
2022
Hanno Becker3307b532017-12-27 21:37:21 +00002023 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002027 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
Paul Bakker68884e32013-01-07 18:20:04 +01002029 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002031
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002032#if defined(MBEDTLS_GCM_C) || \
2033 defined(MBEDTLS_CCM_C) || \
2034 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002036 mode == MBEDTLS_MODE_CCM ||
2037 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002038 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002039 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002041 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002042
Hanno Becker3307b532017-12-27 21:37:21 +00002043 /* Check that there's space for both the authentication tag
2044 * and the explicit IV before and after the record content. */
2045 if( post_avail < transform->taglen ||
2046 rec->data_offset < explicit_iv_len )
2047 {
2048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2049 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2050 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002051
Paul Bakker68884e32013-01-07 18:20:04 +01002052 /*
2053 * Generate IV
2054 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002055 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2056 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002057 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002059 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2060 explicit_iv_len );
2061 /* Prefix record content with explicit IV. */
2062 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002063 }
2064 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2065 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002066 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 unsigned char i;
2068
2069 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2070
2071 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002072 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002073 }
2074 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002075 {
2076 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002079 }
2080
Hanno Beckere83efe62019-04-29 13:52:53 +01002081 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002082
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002083 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2084 iv, transform->ivlen );
2085 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002086 data - explicit_iv_len, explicit_iv_len );
2087 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002088 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002091 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002092
Paul Bakker68884e32013-01-07 18:20:04 +01002093 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002094 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002095 */
Hanno Becker3307b532017-12-27 21:37:21 +00002096
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002097 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002098 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002099 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002100 data, rec->data_len, /* source */
2101 data, &rec->data_len, /* destination */
2102 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002105 return( ret );
2106 }
2107
Hanno Becker3307b532017-12-27 21:37:21 +00002108 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2109 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002110
Hanno Becker3307b532017-12-27 21:37:21 +00002111 rec->data_len += transform->taglen + explicit_iv_len;
2112 rec->data_offset -= explicit_iv_len;
2113 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002114 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002115 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2118#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002119 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002122 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002123 size_t padlen, i;
2124 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002125
Hanno Becker3307b532017-12-27 21:37:21 +00002126 /* Currently we're always using minimal padding
2127 * (up to 255 bytes would be allowed). */
2128 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2129 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 padlen = 0;
2131
Hanno Becker3307b532017-12-27 21:37:21 +00002132 /* Check there's enough space in the buffer for the padding. */
2133 if( post_avail < padlen + 1 )
2134 {
2135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2136 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2137 }
2138
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002140 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Hanno Becker3307b532017-12-27 21:37:21 +00002142 rec->data_len += padlen + 1;
2143 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002146 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002147 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2148 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149 */
Hanno Becker3307b532017-12-27 21:37:21 +00002150 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002151 {
Hanno Becker3307b532017-12-27 21:37:21 +00002152 if( f_rng == NULL )
2153 {
2154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2156 }
2157
2158 if( rec->data_offset < transform->ivlen )
2159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2161 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2162 }
2163
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002164 /*
2165 * Generate IV
2166 */
Hanno Becker3307b532017-12-27 21:37:21 +00002167 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002168 if( ret != 0 )
2169 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002170
Hanno Becker3307b532017-12-27 21:37:21 +00002171 memcpy( data - transform->ivlen, transform->iv_enc,
2172 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002173
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002174 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002178 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002179 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002180 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181
Hanno Becker3307b532017-12-27 21:37:21 +00002182 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2183 transform->iv_enc,
2184 transform->ivlen,
2185 data, rec->data_len,
2186 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002189 return( ret );
2190 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002191
Hanno Becker3307b532017-12-27 21:37:21 +00002192 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002196 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002199 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002200 {
2201 /*
2202 * Save IV in SSL3 and TLS1
2203 */
Hanno Becker3307b532017-12-27 21:37:21 +00002204 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2205 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
Hanno Becker3307b532017-12-27 21:37:21 +00002207 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002208#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002209 {
2210 data -= transform->ivlen;
2211 rec->data_offset -= transform->ivlen;
2212 rec->data_len += transform->ivlen;
2213 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002216 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002217 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002218 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2219
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002220 /*
2221 * MAC(MAC_write_key, seq_num +
2222 * TLSCipherText.type +
2223 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002224 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002225 * IV + // except for TLS 1.0
2226 * ENC(content + padding + padding_length));
2227 */
Hanno Becker3307b532017-12-27 21:37:21 +00002228
2229 if( post_avail < transform->maclen)
2230 {
2231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2232 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2233 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234
Hanno Beckere83efe62019-04-29 13:52:53 +01002235 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002238 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002239 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002240
Hanno Becker3307b532017-12-27 21:37:21 +00002241 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002242 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002243 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2244 data, rec->data_len );
2245 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2246 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002247
Hanno Becker3307b532017-12-27 21:37:21 +00002248 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002249
Hanno Becker3307b532017-12-27 21:37:21 +00002250 rec->data_len += transform->maclen;
2251 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002252 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002253 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002258 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002264 /* Make extra sure authentication was performed, exactly once */
2265 if( auth_done != 1 )
2266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002269 }
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 return( 0 );
2274}
2275
Hanno Becker611a83b2018-01-03 14:27:32 +00002276int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2277 mbedtls_ssl_transform *transform,
2278 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002280 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002282 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002283#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002284 size_t padlen = 0, correct = 1;
2285#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002286 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002287 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002288 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002289
Hanno Becker611a83b2018-01-03 14:27:32 +00002290#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002291 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002292 ((void) ssl);
2293#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002296 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002297 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2300 }
2301 if( rec == NULL ||
2302 rec->buf == NULL ||
2303 rec->buf_len < rec->data_offset ||
2304 rec->buf_len - rec->data_offset < rec->data_len )
2305 {
2306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002308 }
2309
Hanno Becker4c6876b2017-12-27 21:28:58 +00002310 data = rec->buf + rec->data_offset;
2311 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Hanno Beckera5a2b082019-05-15 14:03:01 +01002313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002314 /*
2315 * Match record's CID with incoming CID.
2316 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002317 if( rec->cid_len != transform->in_cid_len ||
2318 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2319 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002320 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002321 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002322#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2325 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002326 {
2327 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002328 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2329 transform->iv_dec,
2330 transform->ivlen,
2331 data, rec->data_len,
2332 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002335 return( ret );
2336 }
2337
Hanno Becker4c6876b2017-12-27 21:28:58 +00002338 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 }
Paul Bakker68884e32013-01-07 18:20:04 +01002344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002346#if defined(MBEDTLS_GCM_C) || \
2347 defined(MBEDTLS_CCM_C) || \
2348 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002350 mode == MBEDTLS_MODE_CCM ||
2351 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002352 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002355
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 /*
2357 * Compute and update sizes
2358 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002359 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002362 "+ taglen (%d)", rec->data_len,
2363 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002365 }
Paul Bakker68884e32013-01-07 18:20:04 +01002366
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002367 /*
2368 * Prepare IV
2369 */
2370 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2371 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002372 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002373 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002374 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002375
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002376 }
2377 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2378 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002379 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002380 unsigned char i;
2381
2382 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2383
2384 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002385 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002386 }
2387 else
2388 {
2389 /* Reminder if we ever add an AEAD mode with a different size */
2390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2392 }
2393
Hanno Becker4c6876b2017-12-27 21:28:58 +00002394 data += explicit_iv_len;
2395 rec->data_offset += explicit_iv_len;
2396 rec->data_len -= explicit_iv_len + transform->taglen;
2397
Hanno Beckere83efe62019-04-29 13:52:53 +01002398 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002399 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401
2402 memcpy( transform->iv_dec + transform->fixed_ivlen,
2403 data - explicit_iv_len, explicit_iv_len );
2404
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002405 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002406 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002407 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002408
Paul Bakker68884e32013-01-07 18:20:04 +01002409
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002410 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002411 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002412 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002413 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2414 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002415 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002416 data, rec->data_len,
2417 data, &olen,
2418 data + rec->data_len,
2419 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2424 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002425
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002426 return( ret );
2427 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002428 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002429
Hanno Becker4c6876b2017-12-27 21:28:58 +00002430 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2433 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002434 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2438#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002439 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002442 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 /*
Paul Bakker45829992013-01-03 14:52:21 +01002445 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002448 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2449 {
2450 /* The ciphertext is prefixed with the CBC IV. */
2451 minlen += transform->ivlen;
2452 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002453#endif
Paul Bakker45829992013-01-03 14:52:21 +01002454
Hanno Becker4c6876b2017-12-27 21:28:58 +00002455 /* Size considerations:
2456 *
2457 * - The CBC cipher text must not be empty and hence
2458 * at least of size transform->ivlen.
2459 *
2460 * Together with the potential IV-prefix, this explains
2461 * the first of the two checks below.
2462 *
2463 * - The record must contain a MAC, either in plain or
2464 * encrypted, depending on whether Encrypt-then-MAC
2465 * is used or not.
2466 * - If it is, the message contains the IV-prefix,
2467 * the CBC ciphertext, and the MAC.
2468 * - If it is not, the padded plaintext, and hence
2469 * the CBC ciphertext, has at least length maclen + 1
2470 * because there is at least the padding length byte.
2471 *
2472 * As the CBC ciphertext is not empty, both cases give the
2473 * lower bound minlen + maclen + 1 on the record size, which
2474 * we test for in the second check below.
2475 */
2476 if( rec->data_len < minlen + transform->ivlen ||
2477 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002480 "+ 1 ) ( + expl IV )", rec->data_len,
2481 transform->ivlen,
2482 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002484 }
2485
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002486 /*
2487 * Authenticate before decrypt if enabled
2488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002491 {
Hanno Becker992b6872017-11-09 18:57:39 +00002492 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002495
Hanno Becker4c6876b2017-12-27 21:28:58 +00002496 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2497 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498
Hanno Beckere83efe62019-04-29 13:52:53 +01002499 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002500
Hanno Beckere83efe62019-04-29 13:52:53 +01002501 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2502 add_data_len );
2503 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2504 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002505 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2506 data, rec->data_len );
2507 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2508 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002509
Hanno Becker4c6876b2017-12-27 21:28:58 +00002510 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2511 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002512 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002513 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
Hanno Becker4c6876b2017-12-27 21:28:58 +00002515 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2516 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002521 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002522 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002524
2525 /*
2526 * Check length sanity
2527 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002528 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002533 }
2534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002536 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002537 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002538 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002539 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002540 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002541 /* This is safe because data_len >= minlen + maclen + 1 initially,
2542 * and at this point we have at most subtracted maclen (note that
2543 * minlen == transform->ivlen here). */
2544 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545
Hanno Becker4c6876b2017-12-27 21:28:58 +00002546 data += transform->ivlen;
2547 rec->data_offset += transform->ivlen;
2548 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002549 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002551
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2553 transform->iv_dec, transform->ivlen,
2554 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002557 return( ret );
2558 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002559
Hanno Becker4c6876b2017-12-27 21:28:58 +00002560 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002568 {
2569 /*
2570 * Save IV in SSL3 and TLS1
2571 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002572 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2573 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002575#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Hanno Becker4c6876b2017-12-27 21:28:58 +00002577 /* Safe since data_len >= minlen + maclen + 1, so after having
2578 * subtracted at most minlen and maclen up to this point,
2579 * data_len > 0. */
2580 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002581
Hanno Becker4c6876b2017-12-27 21:28:58 +00002582 if( auth_done == 1 )
2583 {
2584 correct *= ( rec->data_len >= padlen + 1 );
2585 padlen *= ( rec->data_len >= padlen + 1 );
2586 }
2587 else
Paul Bakker45829992013-01-03 14:52:21 +01002588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002590 if( rec->data_len < transform->maclen + padlen + 1 )
2591 {
2592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2593 rec->data_len,
2594 transform->maclen,
2595 padlen + 1 ) );
2596 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002597#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002598
2599 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2600 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Hanno Becker4c6876b2017-12-27 21:28:58 +00002603 padlen++;
2604
2605 /* Regardless of the validity of the padding,
2606 * we have data_len >= padlen here. */
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002609 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002611 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_DEBUG_ALL)
2614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002615 "should be no more than %d",
2616 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002617#endif
Paul Bakker45829992013-01-03 14:52:21 +01002618 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620 }
2621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2623#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2624 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002625 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002627 /* The padding check involves a series of up to 256
2628 * consecutive memory reads at the end of the record
2629 * plaintext buffer. In order to hide the length and
2630 * validity of the padding, always perform exactly
2631 * `min(256,plaintext_len)` reads (but take into account
2632 * only the last `padlen` bytes for the padding check). */
2633 size_t pad_count = 0;
2634 size_t real_count = 0;
2635 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002636
Hanno Becker4c6876b2017-12-27 21:28:58 +00002637 /* Index of first padding byte; it has been ensured above
2638 * that the subtraction is safe. */
2639 size_t const padding_idx = rec->data_len - padlen;
2640 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2641 size_t const start_idx = rec->data_len - num_checks;
2642 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002643
Hanno Becker4c6876b2017-12-27 21:28:58 +00002644 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002645 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002646 real_count |= ( idx >= padding_idx );
2647 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002648 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002649 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002652 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002654#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002655 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002657 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2659 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002663 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002664
Hanno Becker4c6876b2017-12-27 21:28:58 +00002665 /* If the padding was found to be invalid, padlen == 0
2666 * and the subtraction is safe. If the padding was found valid,
2667 * padlen hasn't been changed and the previous assertion
2668 * data_len >= padlen still holds. */
2669 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002671 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002673 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002679#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002681 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002682#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
2684 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002685 * Authenticate if not done yet.
2686 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002688#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002689 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002690 {
Hanno Becker992b6872017-11-09 18:57:39 +00002691 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002692
Hanno Becker4c6876b2017-12-27 21:28:58 +00002693 /* If the initial value of padlen was such that
2694 * data_len < maclen + padlen + 1, then padlen
2695 * got reset to 1, and the initial check
2696 * data_len >= minlen + maclen + 1
2697 * guarantees that at this point we still
2698 * have at least data_len >= maclen.
2699 *
2700 * If the initial value of padlen was such that
2701 * data_len >= maclen + padlen + 1, then we have
2702 * subtracted either padlen + 1 (if the padding was correct)
2703 * or 0 (if the padding was incorrect) since then,
2704 * hence data_len >= maclen in any case.
2705 */
2706 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Hanno Beckere83efe62019-04-29 13:52:53 +01002708 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002711 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002712 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002713 ssl_mac( &transform->md_ctx_dec,
2714 transform->mac_dec,
2715 data, rec->data_len,
2716 rec->ctr, rec->type,
2717 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002718 }
2719 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2721#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2722 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002723 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002724 {
2725 /*
2726 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002727 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002728 *
2729 * Known timing attacks:
2730 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2731 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002732 * To compensate for different timings for the MAC calculation
2733 * depending on how much padding was removed (which is determined
2734 * by padlen), process extra_run more blocks through the hash
2735 * function.
2736 *
2737 * The formula in the paper is
2738 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2739 * where L1 is the size of the header plus the decrypted message
2740 * plus CBC padding and L2 is the size of the header plus the
2741 * decrypted message. This is for an underlying hash function
2742 * with 64-byte blocks.
2743 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2744 * correctly. We round down instead of up, so -56 is the correct
2745 * value for our calculations instead of -55.
2746 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002747 * Repeat the formula rather than defining a block_size variable.
2748 * This avoids requiring division by a variable at runtime
2749 * (which would be marginally less efficient and would require
2750 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002751 */
2752 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002753 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002754
2755 /*
2756 * The next two sizes are the minimum and maximum values of
2757 * in_msglen over all padlen values.
2758 *
2759 * They're independent of padlen, since we previously did
2760 * in_msglen -= padlen.
2761 *
2762 * Note that max_len + maclen is never more than the buffer
2763 * length, as we previously did in_msglen -= maclen too.
2764 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002765 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002766 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2767
Hanno Becker4c6876b2017-12-27 21:28:58 +00002768 memset( tmp, 0, sizeof( tmp ) );
2769
2770 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002771 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002772#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2773 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002774 case MBEDTLS_MD_MD5:
2775 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002776 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002778 extra_run =
2779 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2780 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002781 break;
2782#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002783#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002784 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002785 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002786 extra_run =
2787 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2788 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002789 break;
2790#endif
2791 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2794 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002795
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002796 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002797
Hanno Beckere83efe62019-04-29 13:52:53 +01002798 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2799 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002800 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2801 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002802 /* Make sure we access everything even when padlen > 0. This
2803 * makes the synchronisation requirements for just-in-time
2804 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002805 ssl_read_memory( data + rec->data_len, padlen );
2806 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002807
2808 /* Call mbedtls_md_process at least once due to cache attacks
2809 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002810 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002811 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002812
Hanno Becker4c6876b2017-12-27 21:28:58 +00002813 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002814
2815 /* Make sure we access all the memory that could contain the MAC,
2816 * before we check it in the next code block. This makes the
2817 * synchronisation requirements for just-in-time Prime+Probe
2818 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002819 ssl_read_memory( data + min_len,
2820 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002821 }
2822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2824 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2827 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002828 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002830#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002831 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2832 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Hanno Becker4c6876b2017-12-27 21:28:58 +00002835 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2836 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838#if defined(MBEDTLS_SSL_DEBUG_ALL)
2839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002840#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002841 correct = 0;
2842 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002843 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002844 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002845
2846 /*
2847 * Finally check the correct flag
2848 */
2849 if( correct == 0 )
2850 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002851#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002852
2853 /* Make extra sure authentication was performed, exactly once */
2854 if( auth_done != 1 )
2855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002858 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Hanno Beckera5a2b082019-05-15 14:03:01 +01002860#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002861 if( rec->cid_len != 0 )
2862 {
2863 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2864 &rec->type );
2865 if( ret != 0 )
2866 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2867 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002868#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
2872 return( 0 );
2873}
2874
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002875#undef MAC_NONE
2876#undef MAC_PLAINTEXT
2877#undef MAC_CIPHERTEXT
2878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002880/*
2881 * Compression/decompression functions
2882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002884{
2885 int ret;
2886 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002887 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002888 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002889 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002892
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002893 if( len_pre == 0 )
2894 return( 0 );
2895
Paul Bakker2770fbd2012-07-03 13:30:23 +00002896 memcpy( msg_pre, ssl->out_msg, len_pre );
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 ssl->out_msglen ) );
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902 ssl->out_msg, ssl->out_msglen );
2903
Paul Bakker48916f92012-09-16 19:57:18 +00002904 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2905 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2906 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002907 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908
Paul Bakker48916f92012-09-16 19:57:18 +00002909 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002910 if( ret != Z_OK )
2911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2913 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914 }
2915
Angus Grattond8213d02016-05-25 20:56:48 +10002916 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002917 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 ssl->out_msglen ) );
2921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 ssl->out_msg, ssl->out_msglen );
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926
2927 return( 0 );
2928}
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931{
2932 int ret;
2933 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002934 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002935 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002936 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002939
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002940 if( len_pre == 0 )
2941 return( 0 );
2942
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 memcpy( msg_pre, ssl->in_msg, len_pre );
2944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946 ssl->in_msglen ) );
2947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002949 ssl->in_msg, ssl->in_msglen );
2950
Paul Bakker48916f92012-09-16 19:57:18 +00002951 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2952 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2953 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002954 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002955 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956
Paul Bakker48916f92012-09-16 19:57:18 +00002957 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958 if( ret != Z_OK )
2959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2961 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002962 }
2963
Angus Grattond8213d02016-05-25 20:56:48 +10002964 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002965 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002968 ssl->in_msglen ) );
2969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002971 ssl->in_msg, ssl->in_msglen );
2972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002974
2975 return( 0 );
2976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2980static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#if defined(MBEDTLS_SSL_PROTO_DTLS)
2983static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002984{
2985 /* If renegotiation is not enforced, retransmit until we would reach max
2986 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002987 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002988 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002989 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002990 unsigned char doublings = 1;
2991
2992 while( ratio != 0 )
2993 {
2994 ++doublings;
2995 ratio >>= 1;
2996 }
2997
2998 if( ++ssl->renego_records_seen > doublings )
2999 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003001 return( 0 );
3002 }
3003 }
3004
3005 return( ssl_write_hello_request( ssl ) );
3006}
3007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003009
Paul Bakker5121ce52009-01-03 21:22:43 +00003010/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003011 * Fill the input message buffer by appending data to it.
3012 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003013 *
3014 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3015 * available (from this read and/or a previous one). Otherwise, an error code
3016 * is returned (possibly EOF or WANT_READ).
3017 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003018 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3019 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3020 * since we always read a whole datagram at once.
3021 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003022 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003023 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003026{
Paul Bakker23986e52011-04-24 08:57:21 +00003027 int ret;
3028 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003032 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003035 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003037 }
3038
Angus Grattond8213d02016-05-25 20:56:48 +10003039 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003043 }
3044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003046 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003048 uint32_t timeout;
3049
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003050 /* Just to be sure */
3051 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3052 {
3053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3054 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3056 }
3057
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003058 /*
3059 * The point is, we need to always read a full datagram at once, so we
3060 * sometimes read more then requested, and handle the additional data.
3061 * It could be the rest of the current record (while fetching the
3062 * header) and/or some other records in the same datagram.
3063 */
3064
3065 /*
3066 * Move to the next record in the already read datagram if applicable
3067 */
3068 if( ssl->next_record_offset != 0 )
3069 {
3070 if( ssl->in_left < ssl->next_record_offset )
3071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003074 }
3075
3076 ssl->in_left -= ssl->next_record_offset;
3077
3078 if( ssl->in_left != 0 )
3079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003081 ssl->next_record_offset ) );
3082 memmove( ssl->in_hdr,
3083 ssl->in_hdr + ssl->next_record_offset,
3084 ssl->in_left );
3085 }
3086
3087 ssl->next_record_offset = 0;
3088 }
3089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003092
3093 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003094 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003095 */
3096 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003099 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003100 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003101
3102 /*
3103 * A record can't be split accross datagrams. If we need to read but
3104 * are not at the beginning of a new record, the caller did something
3105 * wrong.
3106 */
3107 if( ssl->in_left != 0 )
3108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003111 }
3112
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113 /*
3114 * Don't even try to read if time's out already.
3115 * This avoids by-passing the timer when repeatedly receiving messages
3116 * that will end up being dropped.
3117 */
3118 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003119 {
3120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003121 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003122 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003123 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003124 {
Angus Grattond8213d02016-05-25 20:56:48 +10003125 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003128 timeout = ssl->handshake->retransmit_timeout;
3129 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003130 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003133
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003134 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003135 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3136 timeout );
3137 else
3138 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003141
3142 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003144 }
3145
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003146 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003149 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003152 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003153 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003156 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003157 }
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003162 return( ret );
3163 }
3164
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003165 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003166 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003168 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003170 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003171 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003174 return( ret );
3175 }
3176
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003177 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003180 }
3181
Paul Bakker5121ce52009-01-03 21:22:43 +00003182 if( ret < 0 )
3183 return( ret );
3184
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003185 ssl->in_left = ret;
3186 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003187 MBEDTLS_SSL_TRANSPORT_ELSE
3188#endif /* MBEDTLS_SSL_PROTO_DTLS */
3189#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003192 ssl->in_left, nb_want ) );
3193
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003194 while( ssl->in_left < nb_want )
3195 {
3196 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003197
3198 if( ssl_check_timer( ssl ) != 0 )
3199 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3200 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003201 {
3202 if( ssl->f_recv_timeout != NULL )
3203 {
3204 ret = ssl->f_recv_timeout( ssl->p_bio,
3205 ssl->in_hdr + ssl->in_left, len,
3206 ssl->conf->read_timeout );
3207 }
3208 else
3209 {
3210 ret = ssl->f_recv( ssl->p_bio,
3211 ssl->in_hdr + ssl->in_left, len );
3212 }
3213 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003216 ssl->in_left, nb_want ) );
3217 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003218
3219 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003221
3222 if( ret < 0 )
3223 return( ret );
3224
mohammad160352aecb92018-03-28 23:41:40 -07003225 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003226 {
Darryl Green11999bb2018-03-13 15:22:58 +00003227 MBEDTLS_SSL_DEBUG_MSG( 1,
3228 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003229 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3231 }
3232
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003233 ssl->in_left += ret;
3234 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003236#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
3240 return( 0 );
3241}
3242
3243/*
3244 * Flush any data not yet written
3245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003247{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003248 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003249 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003252
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003253 if( ssl->f_send == NULL )
3254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003256 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003258 }
3259
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003260 /* Avoid incrementing counter if data is flushed */
3261 if( ssl->out_left == 0 )
3262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003264 return( 0 );
3265 }
3266
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 while( ssl->out_left > 0 )
3268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003270 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003271
Hanno Becker2b1e3542018-08-06 11:19:13 +01003272 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003273 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
3277 if( ret <= 0 )
3278 return( ret );
3279
mohammad160352aecb92018-03-28 23:41:40 -07003280 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003281 {
Darryl Green11999bb2018-03-13 15:22:58 +00003282 MBEDTLS_SSL_DEBUG_MSG( 1,
3283 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003284 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3286 }
3287
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 ssl->out_left -= ret;
3289 }
3290
Hanno Becker2b1e3542018-08-06 11:19:13 +01003291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003292 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003293 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003294 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003295 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003296 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003297#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003298#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003299 {
3300 ssl->out_hdr = ssl->out_buf + 8;
3301 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003302#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003303 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
3307 return( 0 );
3308}
3309
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003310/*
3311 * Functions to handle the DTLS retransmission state machine
3312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003314/*
3315 * Append current handshake message to current outgoing flight
3316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3321 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3322 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323
3324 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003325 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003326 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003329 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003330 }
3331
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003332 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003336 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003337 }
3338
3339 /* Copy current handshake message with headers */
3340 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3341 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003342 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343 msg->next = NULL;
3344
3345 /* Append to the current flight */
3346 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003347 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348 else
3349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003351 while( cur->next != NULL )
3352 cur = cur->next;
3353 cur->next = msg;
3354 }
3355
Hanno Becker3b235902018-08-06 09:54:53 +01003356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357 return( 0 );
3358}
3359
3360/*
3361 * Free the current flight of handshake messages
3362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 mbedtls_ssl_flight_item *cur = flight;
3366 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003367
3368 while( cur != NULL )
3369 {
3370 next = cur->next;
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_free( cur->p );
3373 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003374
3375 cur = next;
3376 }
3377}
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3380static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003381#endif
3382
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003383/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003384 * Swap transform_out and out_ctr with the alternative ones
3385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003389 unsigned char tmp_out_ctr[8];
3390
3391 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003394 return;
3395 }
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003398
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003399 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003400 tmp_transform = ssl->transform_out;
3401 ssl->transform_out = ssl->handshake->alt_transform_out;
3402 ssl->handshake->alt_transform_out = tmp_transform;
3403
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003404 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003405 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3406 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003408
3409 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003410 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3413 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3418 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003419 }
3420 }
3421#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003422}
3423
3424/*
3425 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003426 */
3427int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3428{
3429 int ret = 0;
3430
3431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3432
3433 ret = mbedtls_ssl_flight_transmit( ssl );
3434
3435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3436
3437 return( ret );
3438}
3439
3440/*
3441 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003442 *
3443 * Need to remember the current message in case flush_output returns
3444 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003445 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003446 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003447int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003448{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003449 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003453 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003455
3456 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003457 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003458 ssl_swap_epochs( ssl );
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003462
3463 while( ssl->handshake->cur_msg != NULL )
3464 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003466 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003467
Hanno Beckere1dcb032018-08-17 16:47:58 +01003468 int const is_finished =
3469 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3470 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3471
Hanno Becker04da1892018-08-14 13:22:10 +01003472 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3473 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3474
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003475 /* Swap epochs before sending Finished: we can't do it after
3476 * sending ChangeCipherSpec, in case write returns WANT_READ.
3477 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003478 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003479 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003481 ssl_swap_epochs( ssl );
3482 }
3483
Hanno Becker67bc7c32018-08-06 11:33:50 +01003484 ret = ssl_get_remaining_payload_in_datagram( ssl );
3485 if( ret < 0 )
3486 return( ret );
3487 max_frag_len = (size_t) ret;
3488
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003489 /* CCS is copied as is, while HS messages may need fragmentation */
3490 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3491 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003492 if( max_frag_len == 0 )
3493 {
3494 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3495 return( ret );
3496
3497 continue;
3498 }
3499
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003500 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003501 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003502 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003503
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003504 /* Update position inside current message */
3505 ssl->handshake->cur_msg_p += cur->len;
3506 }
3507 else
3508 {
3509 const unsigned char * const p = ssl->handshake->cur_msg_p;
3510 const size_t hs_len = cur->len - 12;
3511 const size_t frag_off = p - ( cur->p + 12 );
3512 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003513 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003514
Hanno Beckere1dcb032018-08-17 16:47:58 +01003515 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003516 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003517 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003518 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003519
Hanno Becker67bc7c32018-08-06 11:33:50 +01003520 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3521 return( ret );
3522
3523 continue;
3524 }
3525 max_hs_frag_len = max_frag_len - 12;
3526
3527 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3528 max_hs_frag_len : rem_len;
3529
3530 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003531 {
3532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003533 (unsigned) cur_hs_frag_len,
3534 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003535 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003536
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003537 /* Messages are stored with handshake headers as if not fragmented,
3538 * copy beginning of headers then fill fragmentation fields.
3539 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3540 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003541
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003542 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3543 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3544 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3545
Hanno Becker67bc7c32018-08-06 11:33:50 +01003546 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3547 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3548 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003549
3550 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3551
Hanno Becker3f7b9732018-08-28 09:53:25 +01003552 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003553 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3554 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003555 ssl->out_msgtype = cur->type;
3556
3557 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003558 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003559 }
3560
3561 /* If done with the current message move to the next one if any */
3562 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3563 {
3564 if( cur->next != NULL )
3565 {
3566 ssl->handshake->cur_msg = cur->next;
3567 ssl->handshake->cur_msg_p = cur->next->p + 12;
3568 }
3569 else
3570 {
3571 ssl->handshake->cur_msg = NULL;
3572 ssl->handshake->cur_msg_p = NULL;
3573 }
3574 }
3575
3576 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003577 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580 return( ret );
3581 }
3582 }
3583
Hanno Becker67bc7c32018-08-06 11:33:50 +01003584 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3585 return( ret );
3586
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003587 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3589 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003590 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003593 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3594 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003595
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003597
3598 return( 0 );
3599}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003600
3601/*
3602 * To be called when the last message of an incoming flight is received.
3603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003605{
3606 /* We won't need to resend that one any more */
3607 ssl_flight_free( ssl->handshake->flight );
3608 ssl->handshake->flight = NULL;
3609 ssl->handshake->cur_msg = NULL;
3610
3611 /* The next incoming flight will start with this msg_seq */
3612 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3613
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003614 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003615 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003616
Hanno Becker0271f962018-08-16 13:23:47 +01003617 /* Clear future message buffering structure. */
3618 ssl_buffering_free( ssl );
3619
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003620 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003621 ssl_set_timer( ssl, 0 );
3622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3624 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003627 }
3628 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003630}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003631
3632/*
3633 * To be called when the last message of an outgoing flight is send.
3634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003636{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003637 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003638 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3641 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003644 }
3645 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003649
Paul Bakker5121ce52009-01-03 21:22:43 +00003650/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003651 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003653
3654/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003655 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003656 *
3657 * - fill in handshake headers
3658 * - update handshake checksum
3659 * - DTLS: save message for resending
3660 * - then pass to the record layer
3661 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003662 * DTLS: except for HelloRequest, messages are only queued, and will only be
3663 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003664 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003665 * Inputs:
3666 * - ssl->out_msglen: 4 + actual handshake message len
3667 * (4 is the size of handshake headers for TLS)
3668 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3669 * - ssl->out_msg + 4: the handshake message body
3670 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003671 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003672 * - ssl->out_msglen: the length of the record contents
3673 * (including handshake headers but excluding record headers)
3674 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003675 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003677{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003678 int ret;
3679 const size_t hs_len = ssl->out_msglen - 4;
3680 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3683
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003684 /*
3685 * Sanity checks
3686 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003687 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003688 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3689 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003690 /* In SSLv3, the client might send a NoCertificate alert. */
3691#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3692 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3693 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3694 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3695#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3696 {
3697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3699 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003702 /* Whenever we send anything different from a
3703 * HelloRequest we should be in a handshake - double check. */
3704 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3705 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003706 ssl->handshake == NULL )
3707 {
3708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3710 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003713 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003714 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003719 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003721
Hanno Beckerb50a2532018-08-06 11:52:54 +01003722 /* Double-check that we did not exceed the bounds
3723 * of the outgoing record buffer.
3724 * This should never fail as the various message
3725 * writing functions must obey the bounds of the
3726 * outgoing record buffer, but better be safe.
3727 *
3728 * Note: We deliberately do not check for the MTU or MFL here.
3729 */
3730 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3731 {
3732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3733 "size %u, maximum %u",
3734 (unsigned) ssl->out_msglen,
3735 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3737 }
3738
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003739 /*
3740 * Fill handshake headers
3741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003744 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3745 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3746 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003748 /*
3749 * DTLS has additional fields in the Handshake layer,
3750 * between the length field and the actual payload:
3751 * uint16 message_seq;
3752 * uint24 fragment_offset;
3753 * uint24 fragment_length;
3754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003756 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003757 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003758 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003759 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003760 {
3761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3762 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003763 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003764 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003765 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3766 }
3767
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003768 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003769 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003770
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003771 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003772 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003773 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003774 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3775 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3776 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003777 }
3778 else
3779 {
3780 ssl->out_msg[4] = 0;
3781 ssl->out_msg[5] = 0;
3782 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003783
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003784 /* Handshake hashes are computed without fragmentation,
3785 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003786 memset( ssl->out_msg + 6, 0x00, 3 );
3787 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003790
Hanno Becker0207e532018-08-28 10:28:28 +01003791 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003792 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3793 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 }
3795
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003796 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003798 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003799 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3800 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003801 {
3802 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003805 return( ret );
3806 }
3807 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003808 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003809#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003810 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003811 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003812 {
3813 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3814 return( ret );
3815 }
3816 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003817
3818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3819
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003820 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003821}
3822
3823/*
3824 * Record layer functions
3825 */
3826
3827/*
3828 * Write current record.
3829 *
3830 * Uses:
3831 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3832 * - ssl->out_msglen: length of the record content (excl headers)
3833 * - ssl->out_msg: record content
3834 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003836{
3837 int ret, done = 0;
3838 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003840
3841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003844 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003846 {
3847 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003850 return( ret );
3851 }
3852
3853 len = ssl->out_msglen;
3854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3858 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 ret = mbedtls_ssl_hw_record_write( ssl );
3863 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3866 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003867 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003868
3869 if( ret == 0 )
3870 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003871 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003873 if( !done )
3874 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003875 unsigned i;
3876 size_t protected_record_size;
3877
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003878 /* Skip writing the record content type to after the encryption,
3879 * as it may change when using the CID extension. */
3880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003882 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003883
Hanno Becker19859472018-08-06 09:40:20 +01003884 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003885 ssl->out_len[0] = (unsigned char)( len >> 8 );
3886 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003887
Paul Bakker48916f92012-09-16 19:57:18 +00003888 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003889 {
Hanno Becker3307b532017-12-27 21:37:21 +00003890 mbedtls_record rec;
3891
3892 rec.buf = ssl->out_iv;
3893 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3894 ( ssl->out_iv - ssl->out_buf );
3895 rec.data_len = ssl->out_msglen;
3896 rec.data_offset = ssl->out_msg - rec.buf;
3897
3898 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3899 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3900 ssl->conf->transport, rec.ver );
3901 rec.type = ssl->out_msgtype;
3902
Hanno Beckera5a2b082019-05-15 14:03:01 +01003903#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003904 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003905 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003906#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003907
Hanno Becker611a83b2018-01-03 14:27:32 +00003908 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003909 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003912 return( ret );
3913 }
3914
Hanno Becker3307b532017-12-27 21:37:21 +00003915 if( rec.data_offset != 0 )
3916 {
3917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3919 }
3920
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003921 /* Update the record content type and CID. */
3922 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003923#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003924 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003925#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003926 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003927 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3928 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003929 }
3930
Hanno Becker43395762019-05-03 14:46:38 +01003931 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003932
3933#if defined(MBEDTLS_SSL_PROTO_DTLS)
3934 /* In case of DTLS, double-check that we don't exceed
3935 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003936 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003937 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003938 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003939 if( ret < 0 )
3940 return( ret );
3941
3942 if( protected_record_size > (size_t) ret )
3943 {
3944 /* Should never happen */
3945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3946 }
3947 }
3948#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003949
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003950 /* Now write the potentially updated record content type. */
3951 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003954 "version = [%d:%d], msglen = %d",
3955 ssl->out_hdr[0], ssl->out_hdr[1],
3956 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003959 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003960
3961 ssl->out_left += protected_record_size;
3962 ssl->out_hdr += protected_record_size;
3963 ssl_update_out_pointers( ssl, ssl->transform_out );
3964
Hanno Becker04484622018-08-06 09:49:38 +01003965 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3966 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3967 break;
3968
3969 /* The loop goes to its end iff the counter is wrapping */
3970 if( i == ssl_ep_len( ssl ) )
3971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3973 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3974 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003975 }
3976
Hanno Becker67bc7c32018-08-06 11:33:50 +01003977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003978 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003979 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003980 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003981 size_t remaining;
3982 ret = ssl_get_remaining_payload_in_datagram( ssl );
3983 if( ret < 0 )
3984 {
3985 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3986 ret );
3987 return( ret );
3988 }
3989
3990 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003991 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003992 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003993 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003994 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003995 else
3996 {
Hanno Becker513815a2018-08-20 11:56:09 +01003997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003998 }
3999 }
4000#endif /* MBEDTLS_SSL_PROTO_DTLS */
4001
4002 if( ( flush == SSL_FORCE_FLUSH ) &&
4003 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 return( ret );
4007 }
4008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
4011 return( 0 );
4012}
4013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004015
4016static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4017{
4018 if( ssl->in_msglen < ssl->in_hslen ||
4019 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4020 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4021 {
4022 return( 1 );
4023 }
4024 return( 0 );
4025}
Hanno Becker44650b72018-08-16 12:51:11 +01004026
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004027static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004028{
4029 return( ( ssl->in_msg[9] << 16 ) |
4030 ( ssl->in_msg[10] << 8 ) |
4031 ssl->in_msg[11] );
4032}
4033
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004034static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004035{
4036 return( ( ssl->in_msg[6] << 16 ) |
4037 ( ssl->in_msg[7] << 8 ) |
4038 ssl->in_msg[8] );
4039}
4040
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004041static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004042{
4043 uint32_t msg_len, frag_off, frag_len;
4044
4045 msg_len = ssl_get_hs_total_len( ssl );
4046 frag_off = ssl_get_hs_frag_off( ssl );
4047 frag_len = ssl_get_hs_frag_len( ssl );
4048
4049 if( frag_off > msg_len )
4050 return( -1 );
4051
4052 if( frag_len > msg_len - frag_off )
4053 return( -1 );
4054
4055 if( frag_len + 12 > ssl->in_msglen )
4056 return( -1 );
4057
4058 return( 0 );
4059}
4060
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004061/*
4062 * Mark bits in bitmask (used for DTLS HS reassembly)
4063 */
4064static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4065{
4066 unsigned int start_bits, end_bits;
4067
4068 start_bits = 8 - ( offset % 8 );
4069 if( start_bits != 8 )
4070 {
4071 size_t first_byte_idx = offset / 8;
4072
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004073 /* Special case */
4074 if( len <= start_bits )
4075 {
4076 for( ; len != 0; len-- )
4077 mask[first_byte_idx] |= 1 << ( start_bits - len );
4078
4079 /* Avoid potential issues with offset or len becoming invalid */
4080 return;
4081 }
4082
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004083 offset += start_bits; /* Now offset % 8 == 0 */
4084 len -= start_bits;
4085
4086 for( ; start_bits != 0; start_bits-- )
4087 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4088 }
4089
4090 end_bits = len % 8;
4091 if( end_bits != 0 )
4092 {
4093 size_t last_byte_idx = ( offset + len ) / 8;
4094
4095 len -= end_bits; /* Now len % 8 == 0 */
4096
4097 for( ; end_bits != 0; end_bits-- )
4098 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4099 }
4100
4101 memset( mask + offset / 8, 0xFF, len / 8 );
4102}
4103
4104/*
4105 * Check that bitmask is full
4106 */
4107static int ssl_bitmask_check( unsigned char *mask, size_t len )
4108{
4109 size_t i;
4110
4111 for( i = 0; i < len / 8; i++ )
4112 if( mask[i] != 0xFF )
4113 return( -1 );
4114
4115 for( i = 0; i < len % 8; i++ )
4116 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4117 return( -1 );
4118
4119 return( 0 );
4120}
4121
Hanno Becker56e205e2018-08-16 09:06:12 +01004122/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004123static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004124 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004125{
Hanno Becker56e205e2018-08-16 09:06:12 +01004126 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004127
Hanno Becker56e205e2018-08-16 09:06:12 +01004128 alloc_len = 12; /* Handshake header */
4129 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004130
Hanno Beckerd07df862018-08-16 09:14:58 +01004131 if( add_bitmap )
4132 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004133
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004134 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004135}
Hanno Becker56e205e2018-08-16 09:06:12 +01004136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004138
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004139static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004140{
4141 return( ( ssl->in_msg[1] << 16 ) |
4142 ( ssl->in_msg[2] << 8 ) |
4143 ssl->in_msg[3] );
4144}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004145
Simon Butcher99000142016-10-13 17:21:01 +01004146int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004151 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004153 }
4154
Hanno Becker12555c62018-08-16 12:47:53 +01004155 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004158 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004159 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004162 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004163 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004164 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004165 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004166
Hanno Becker44650b72018-08-16 12:51:11 +01004167 if( ssl_check_hs_header( ssl ) != 0 )
4168 {
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4170 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4171 }
4172
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004173 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004174 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4175 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4176 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4177 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004178 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004179 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4180 {
4181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4182 recv_msg_seq,
4183 ssl->handshake->in_msg_seq ) );
4184 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4185 }
4186
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004187 /* Retransmit only on last message from previous flight, to avoid
4188 * too many retransmissions.
4189 * Besides, No sane server ever retransmits HelloVerifyRequest */
4190 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004194 "message_seq = %d, start_of_flight = %d",
4195 recv_msg_seq,
4196 ssl->handshake->in_flight_start_seq ) );
4197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004201 return( ret );
4202 }
4203 }
4204 else
4205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004207 "message_seq = %d, expected = %d",
4208 recv_msg_seq,
4209 ssl->handshake->in_msg_seq ) );
4210 }
4211
Hanno Becker90333da2017-10-10 11:27:13 +01004212 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004213 }
4214 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004215
Hanno Becker6d97ef52018-08-16 13:09:04 +01004216 /* Message reassembly is handled alongside buffering of future
4217 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004218 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004219 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004220 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004223 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004224 }
4225 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004226 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004228#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004229 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004230 /* With TLS we don't handle fragmentation (for now) */
4231 if( ssl->in_msglen < ssl->in_hslen )
4232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4234 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4235 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004236 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004237#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004238
Simon Butcher99000142016-10-13 17:21:01 +01004239 return( 0 );
4240}
4241
4242void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4243{
Hanno Becker0271f962018-08-16 13:23:47 +01004244 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004245
Hanno Becker0271f962018-08-16 13:23:47 +01004246 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004247 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004248 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004249 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004250
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004251 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004253 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004254 ssl->handshake != NULL )
4255 {
Hanno Becker0271f962018-08-16 13:23:47 +01004256 unsigned offset;
4257 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004258
Hanno Becker0271f962018-08-16 13:23:47 +01004259 /* Increment handshake sequence number */
4260 hs->in_msg_seq++;
4261
4262 /*
4263 * Clear up handshake buffering and reassembly structure.
4264 */
4265
4266 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004267 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004268
4269 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004270 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4271 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004272 offset++, hs_buf++ )
4273 {
4274 *hs_buf = *(hs_buf + 1);
4275 }
4276
4277 /* Create a fresh last entry */
4278 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004279 }
4280#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004281}
4282
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004283/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004284 * DTLS anti-replay: RFC 6347 4.1.2.6
4285 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004286 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4287 * Bit n is set iff record number in_window_top - n has been seen.
4288 *
4289 * Usually, in_window_top is the last record number seen and the lsb of
4290 * in_window is set. The only exception is the initial state (record number 0
4291 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4294static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004295{
4296 ssl->in_window_top = 0;
4297 ssl->in_window = 0;
4298}
4299
4300static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4301{
4302 return( ( (uint64_t) buf[0] << 40 ) |
4303 ( (uint64_t) buf[1] << 32 ) |
4304 ( (uint64_t) buf[2] << 24 ) |
4305 ( (uint64_t) buf[3] << 16 ) |
4306 ( (uint64_t) buf[4] << 8 ) |
4307 ( (uint64_t) buf[5] ) );
4308}
4309
4310/*
4311 * Return 0 if sequence number is acceptable, -1 otherwise
4312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004314{
4315 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4316 uint64_t bit;
4317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004318 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004319 return( 0 );
4320
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321 if( rec_seqnum > ssl->in_window_top )
4322 return( 0 );
4323
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004324 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004325
4326 if( bit >= 64 )
4327 return( -1 );
4328
4329 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4330 return( -1 );
4331
4332 return( 0 );
4333}
4334
4335/*
4336 * Update replay window on new validated record
4337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339{
4340 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4341
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004342 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004343 return;
4344
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345 if( rec_seqnum > ssl->in_window_top )
4346 {
4347 /* Update window_top and the contents of the window */
4348 uint64_t shift = rec_seqnum - ssl->in_window_top;
4349
4350 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004351 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004352 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004353 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004354 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004355 ssl->in_window |= 1;
4356 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004357
4358 ssl->in_window_top = rec_seqnum;
4359 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004360 else
4361 {
4362 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004363 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004364
4365 if( bit < 64 ) /* Always true, but be extra sure */
4366 ssl->in_window |= (uint64_t) 1 << bit;
4367 }
4368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004370
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004371#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004372/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004373static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4374
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004375/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004376 * Without any SSL context, check if a datagram looks like a ClientHello with
4377 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004378 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004379 *
4380 * - if cookie is valid, return 0
4381 * - if ClientHello looks superficially valid but cookie is not,
4382 * fill obuf and set olen, then
4383 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4384 * - otherwise return a specific error code
4385 */
4386static int ssl_check_dtls_clihlo_cookie(
4387 mbedtls_ssl_cookie_write_t *f_cookie_write,
4388 mbedtls_ssl_cookie_check_t *f_cookie_check,
4389 void *p_cookie,
4390 const unsigned char *cli_id, size_t cli_id_len,
4391 const unsigned char *in, size_t in_len,
4392 unsigned char *obuf, size_t buf_len, size_t *olen )
4393{
4394 size_t sid_len, cookie_len;
4395 unsigned char *p;
4396
4397 if( f_cookie_write == NULL || f_cookie_check == NULL )
4398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4399
4400 /*
4401 * Structure of ClientHello with record and handshake headers,
4402 * and expected values. We don't need to check a lot, more checks will be
4403 * done when actually parsing the ClientHello - skipping those checks
4404 * avoids code duplication and does not make cookie forging any easier.
4405 *
4406 * 0-0 ContentType type; copied, must be handshake
4407 * 1-2 ProtocolVersion version; copied
4408 * 3-4 uint16 epoch; copied, must be 0
4409 * 5-10 uint48 sequence_number; copied
4410 * 11-12 uint16 length; (ignored)
4411 *
4412 * 13-13 HandshakeType msg_type; (ignored)
4413 * 14-16 uint24 length; (ignored)
4414 * 17-18 uint16 message_seq; copied
4415 * 19-21 uint24 fragment_offset; copied, must be 0
4416 * 22-24 uint24 fragment_length; (ignored)
4417 *
4418 * 25-26 ProtocolVersion client_version; (ignored)
4419 * 27-58 Random random; (ignored)
4420 * 59-xx SessionID session_id; 1 byte len + sid_len content
4421 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4422 * ...
4423 *
4424 * Minimum length is 61 bytes.
4425 */
4426 if( in_len < 61 ||
4427 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4428 in[3] != 0 || in[4] != 0 ||
4429 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4430 {
4431 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4432 }
4433
4434 sid_len = in[59];
4435 if( sid_len > in_len - 61 )
4436 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4437
4438 cookie_len = in[60 + sid_len];
4439 if( cookie_len > in_len - 60 )
4440 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4441
4442 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4443 cli_id, cli_id_len ) == 0 )
4444 {
4445 /* Valid cookie */
4446 return( 0 );
4447 }
4448
4449 /*
4450 * If we get here, we've got an invalid cookie, let's prepare HVR.
4451 *
4452 * 0-0 ContentType type; copied
4453 * 1-2 ProtocolVersion version; copied
4454 * 3-4 uint16 epoch; copied
4455 * 5-10 uint48 sequence_number; copied
4456 * 11-12 uint16 length; olen - 13
4457 *
4458 * 13-13 HandshakeType msg_type; hello_verify_request
4459 * 14-16 uint24 length; olen - 25
4460 * 17-18 uint16 message_seq; copied
4461 * 19-21 uint24 fragment_offset; copied
4462 * 22-24 uint24 fragment_length; olen - 25
4463 *
4464 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4465 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4466 *
4467 * Minimum length is 28.
4468 */
4469 if( buf_len < 28 )
4470 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4471
4472 /* Copy most fields and adapt others */
4473 memcpy( obuf, in, 25 );
4474 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4475 obuf[25] = 0xfe;
4476 obuf[26] = 0xff;
4477
4478 /* Generate and write actual cookie */
4479 p = obuf + 28;
4480 if( f_cookie_write( p_cookie,
4481 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4482 {
4483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4484 }
4485
4486 *olen = p - obuf;
4487
4488 /* Go back and fill length fields */
4489 obuf[27] = (unsigned char)( *olen - 28 );
4490
4491 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4492 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4493 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4494
4495 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4496 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4497
4498 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4499}
4500
4501/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004502 * Handle possible client reconnect with the same UDP quadruplet
4503 * (RFC 6347 Section 4.2.8).
4504 *
4505 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4506 * that looks like a ClientHello.
4507 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004508 * - if the input looks like a ClientHello without cookies,
4509 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004511 * - if the input looks like a ClientHello with a valid cookie,
4512 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004513 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004514 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004515 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004516 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004517 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4518 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004519 */
4520static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4521{
4522 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004524
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004525 ret = ssl_check_dtls_clihlo_cookie(
4526 ssl->conf->f_cookie_write,
4527 ssl->conf->f_cookie_check,
4528 ssl->conf->p_cookie,
4529 ssl->cli_id, ssl->cli_id_len,
4530 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004531 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004532
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004533 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4534
4535 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004536 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004537 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004538 * If the error is permanent we'll catch it later,
4539 * if it's not, then hopefully it'll work next time. */
4540 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4541
4542 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004543 }
4544
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004545 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004546 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004547 /* Got a valid cookie, partially reset context */
4548 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4549 {
4550 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4551 return( ret );
4552 }
4553
4554 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004555 }
4556
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004557 return( ret );
4558}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004559#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004560
Hanno Becker46483f12019-05-03 13:25:54 +01004561static int ssl_check_record_type( uint8_t record_type )
4562{
4563 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4564 record_type != MBEDTLS_SSL_MSG_ALERT &&
4565 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4566 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4567 {
4568 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4569 }
4570
4571 return( 0 );
4572}
4573
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004574/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004575 * ContentType type;
4576 * ProtocolVersion version;
4577 * uint16 epoch; // DTLS only
4578 * uint48 sequence_number; // DTLS only
4579 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004580 *
4581 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004582 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004583 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4584 *
4585 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004586 * 1. proceed with the record if this function returns 0
4587 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4588 * 3. return CLIENT_RECONNECT if this function return that value
4589 * 4. drop the whole datagram if this function returns anything else.
4590 * Point 2 is needed when the peer is resending, and we have already received
4591 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004595 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004596 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004597
Hanno Becker8b09b732019-05-08 12:03:28 +01004598 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004599
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004601 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004602
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004603 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004604#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004605 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004606 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4607 ssl->conf->cid_len != 0 )
4608 {
4609 /* Shift pointers to account for record header including CID
4610 * struct {
4611 * ContentType special_type = tls12_cid;
4612 * ProtocolVersion version;
4613 * uint16 epoch;
4614 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004615 * opaque cid[cid_length]; // Additional field compared to
4616 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004617 * uint16 length;
4618 * opaque enc_content[DTLSCiphertext.length];
4619 * } DTLSCiphertext;
4620 */
4621
4622 /* So far, we only support static CID lengths
4623 * fixed in the configuration. */
4624 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4625 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4626 }
4627 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004628#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004629 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004632
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004633#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004634 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004635 * Section 4.1.2.7, that is, send alert only with TLS */
4636 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4637 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004638 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4639 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004640 }
4641#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004644 }
4645
4646 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004647 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 }
4652
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004653 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 }
4658
Hanno Becker8b09b732019-05-08 12:03:28 +01004659 /* Now that the total length of the record header is known, ensure
4660 * that the current datagram is large enough to hold it.
4661 * This would fail, for example, if we received a datagram of
4662 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4663 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4664 if( ret != 0 )
4665 {
4666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4667 return( ret );
4668 }
4669 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4670
4671 /* Parse and validate record length
4672 * This must happen after the CID parsing because
4673 * its position in the record header depends on
4674 * the presence of a CID. */
4675
4676 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004677 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004678 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4681 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004682 }
4683
Hanno Becker8b09b732019-05-08 12:03:28 +01004684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004685 "version = [%d:%d], msglen = %d",
4686 ssl->in_msgtype,
4687 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004688
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004689 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004690 * DTLS-related tests.
4691 * Check epoch before checking length constraint because
4692 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4693 * message gets duplicated before the corresponding Finished message,
4694 * the second ChangeCipherSpec should be discarded because it belongs
4695 * to an old epoch, but not because its length is shorter than
4696 * the minimum record length for packets using the new record transform.
4697 * Note that these two kinds of failures are handled differently,
4698 * as an unexpected record is silently skipped but an invalid
4699 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004700 */
4701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004702 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004703 {
4704 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4705
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004706 /* Check epoch (and sequence number) with DTLS */
4707 if( rec_epoch != ssl->in_epoch )
4708 {
4709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4710 "expected %d, received %d",
4711 ssl->in_epoch, rec_epoch ) );
4712
4713#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4714 /*
4715 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4716 * access the first byte of record content (handshake type), as we
4717 * have an active transform (possibly iv_len != 0), so use the
4718 * fact that the record header len is 13 instead.
4719 */
4720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4721 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4722 rec_epoch == 0 &&
4723 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4724 ssl->in_left > 13 &&
4725 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4726 {
4727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4728 "from the same port" ) );
4729 return( ssl_handle_possible_reconnect( ssl ) );
4730 }
4731 else
4732#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004733 {
4734 /* Consider buffering the record. */
4735 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4736 {
4737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4738 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4739 }
4740
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004741 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004742 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004743 }
4744
4745#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4746 /* Replay detection only works for the current epoch */
4747 if( rec_epoch == ssl->in_epoch &&
4748 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4751 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4752 }
4753#endif
4754 }
4755#endif /* MBEDTLS_SSL_PROTO_DTLS */
4756
Hanno Becker52c6dc62017-05-26 16:07:36 +01004757
4758 /* Check length against bounds of the current transform and version */
4759 if( ssl->transform_in == NULL )
4760 {
4761 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004762 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4765 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4766 }
4767 }
4768 else
4769 {
4770 if( ssl->in_msglen < ssl->transform_in->minlen )
4771 {
4772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4773 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4774 }
4775
4776#if defined(MBEDTLS_SSL_PROTO_SSL3)
4777 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004778 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004779 {
4780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4781 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4782 }
4783#endif
4784#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4785 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4786 /*
4787 * TLS encrypted messages can have up to 256 bytes of padding
4788 */
4789 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4790 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004791 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004792 {
4793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4794 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4795 }
4796#endif
4797 }
4798
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004799 return( 0 );
4800}
Paul Bakker5121ce52009-01-03 21:22:43 +00004801
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004802/*
4803 * If applicable, decrypt (and decompress) record content
4804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004806{
4807 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004810 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4813 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 ret = mbedtls_ssl_hw_record_read( ssl );
4818 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4821 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004822 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004823
4824 if( ret == 0 )
4825 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004828 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004830 mbedtls_record rec;
4831
4832 rec.buf = ssl->in_iv;
4833 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4834 - ( ssl->in_iv - ssl->in_buf );
4835 rec.data_len = ssl->in_msglen;
4836 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004837#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004838 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004839 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004840#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004841
4842 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4843 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4844 ssl->conf->transport, rec.ver );
4845 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004846 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4847 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004850
Hanno Beckera5a2b082019-05-15 14:03:01 +01004851#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004852 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4853 ssl->conf->ignore_unexpected_cid
4854 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4855 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004857 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004858 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004859#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004860
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 return( ret );
4862 }
4863
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004864 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004865 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004866 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4867 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004869
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004870 /* The record content type may change during decryption,
4871 * so re-read it. */
4872 ssl->in_msgtype = rec.type;
4873 /* Also update the input buffer, because unfortunately
4874 * the server-side ssl_parse_client_hello() reparses the
4875 * record header when receiving a ClientHello initiating
4876 * a renegotiation. */
4877 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004878 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004879 ssl->in_msglen = rec.data_len;
4880 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4881 ssl->in_len[1] = (unsigned char)( rec.data_len );
4882
Paul Bakker5121ce52009-01-03 21:22:43 +00004883 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4884 ssl->in_msg, ssl->in_msglen );
4885
Hanno Beckera5a2b082019-05-15 14:03:01 +01004886#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004887 /* We have already checked the record content type
4888 * in ssl_parse_record_header(), failing or silently
4889 * dropping the record in the case of an unknown type.
4890 *
4891 * Since with the use of CIDs, the record content type
4892 * might change during decryption, re-check the record
4893 * content type, but treat a failure as fatal this time. */
4894 if( ssl_check_record_type( ssl->in_msgtype ) )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4897 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4898 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004899#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004900
Angus Grattond8213d02016-05-25 20:56:48 +10004901 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004905 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004906 else if( ssl->in_msglen == 0 )
4907 {
4908#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4909 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4910 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4911 {
4912 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4914 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4915 }
4916#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4917
4918 ssl->nb_zero++;
4919
4920 /*
4921 * Three or more empty messages may be a DoS attack
4922 * (excessive CPU consumption).
4923 */
4924 if( ssl->nb_zero > 3 )
4925 {
4926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004927 "messages, possible DoS attack" ) );
4928 /* Treat the records as if they were not properly authenticated,
4929 * thereby failing the connection if we see more than allowed
4930 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004931 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4932 }
4933 }
4934 else
4935 ssl->nb_zero = 0;
4936
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004937 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4938#if defined(MBEDTLS_SSL_PROTO_TLS)
4939 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004940 {
4941 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004942 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004943 if( ++ssl->in_ctr[i - 1] != 0 )
4944 break;
4945
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004946 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004947 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004948 {
4949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4950 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4951 }
4952 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004953#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004954
Paul Bakker5121ce52009-01-03 21:22:43 +00004955 }
4956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004958 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004960 {
4961 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004964 return( ret );
4965 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004970 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004973 }
4974#endif
4975
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004976 return( 0 );
4977}
4978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004980
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004981/*
4982 * Read a record.
4983 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004984 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4985 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4986 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004987 */
Hanno Becker1097b342018-08-15 14:09:41 +01004988
4989/* Helper functions for mbedtls_ssl_read_record(). */
4990static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004991static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4992static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004993
Hanno Becker327c93b2018-08-15 13:56:18 +01004994int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004995 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004996{
4997 int ret;
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005000
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005001 if( ssl->keep_current_message == 0 )
5002 {
5003 do {
Simon Butcher99000142016-10-13 17:21:01 +01005004
Hanno Becker26994592018-08-15 14:14:59 +01005005 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005006 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005007 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005008
Hanno Beckere74d5562018-08-15 14:26:08 +01005009 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005010 {
Hanno Becker40f50842018-08-15 14:48:01 +01005011#if defined(MBEDTLS_SSL_PROTO_DTLS)
5012 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005013
Hanno Becker40f50842018-08-15 14:48:01 +01005014 /* We only check for buffered messages if the
5015 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005016 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005017 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005018 {
Hanno Becker40f50842018-08-15 14:48:01 +01005019 if( ssl_load_buffered_message( ssl ) == 0 )
5020 have_buffered = 1;
5021 }
5022
5023 if( have_buffered == 0 )
5024#endif /* MBEDTLS_SSL_PROTO_DTLS */
5025 {
5026 ret = ssl_get_next_record( ssl );
5027 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5028 continue;
5029
5030 if( ret != 0 )
5031 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005032 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005033 return( ret );
5034 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005035 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005036 }
5037
5038 ret = mbedtls_ssl_handle_message_type( ssl );
5039
Hanno Becker40f50842018-08-15 14:48:01 +01005040#if defined(MBEDTLS_SSL_PROTO_DTLS)
5041 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5042 {
5043 /* Buffer future message */
5044 ret = ssl_buffer_message( ssl );
5045 if( ret != 0 )
5046 return( ret );
5047
5048 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5049 }
5050#endif /* MBEDTLS_SSL_PROTO_DTLS */
5051
Hanno Becker90333da2017-10-10 11:27:13 +01005052 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5053 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005054
5055 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005056 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005057 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005058 return( ret );
5059 }
5060
Hanno Becker327c93b2018-08-15 13:56:18 +01005061 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005062 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005063 {
5064 mbedtls_ssl_update_handshake_status( ssl );
5065 }
Simon Butcher99000142016-10-13 17:21:01 +01005066 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005067 else
Simon Butcher99000142016-10-13 17:21:01 +01005068 {
Hanno Becker02f59072018-08-15 14:00:24 +01005069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005070 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005071 }
5072
5073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5074
5075 return( 0 );
5076}
5077
Hanno Becker40f50842018-08-15 14:48:01 +01005078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005079static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005080{
Hanno Becker40f50842018-08-15 14:48:01 +01005081 if( ssl->in_left > ssl->next_record_offset )
5082 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005083
Hanno Becker40f50842018-08-15 14:48:01 +01005084 return( 0 );
5085}
5086
5087static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5088{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005089 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005090 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005091 int ret = 0;
5092
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 if( hs == NULL )
5094 return( -1 );
5095
Hanno Beckere00ae372018-08-20 09:39:42 +01005096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5097
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005098 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5099 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5100 {
5101 /* Check if we have seen a ChangeCipherSpec before.
5102 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005103 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005104 {
5105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5106 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005107 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005108 }
5109
Hanno Becker39b8bc92018-08-28 17:17:13 +01005110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005111 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5112 ssl->in_msglen = 1;
5113 ssl->in_msg[0] = 1;
5114
5115 /* As long as they are equal, the exact value doesn't matter. */
5116 ssl->in_left = 0;
5117 ssl->next_record_offset = 0;
5118
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005119 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005120 goto exit;
5121 }
Hanno Becker37f95322018-08-16 13:55:32 +01005122
Hanno Beckerb8f50142018-08-28 10:01:34 +01005123#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005124 /* Debug only */
5125 {
5126 unsigned offset;
5127 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5128 {
5129 hs_buf = &hs->buffering.hs[offset];
5130 if( hs_buf->is_valid == 1 )
5131 {
5132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5133 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005134 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005135 }
5136 }
5137 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005138#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005139
5140 /* Check if we have buffered and/or fully reassembled the
5141 * next handshake message. */
5142 hs_buf = &hs->buffering.hs[0];
5143 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5144 {
5145 /* Synthesize a record containing the buffered HS message. */
5146 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5147 ( hs_buf->data[2] << 8 ) |
5148 hs_buf->data[3];
5149
5150 /* Double-check that we haven't accidentally buffered
5151 * a message that doesn't fit into the input buffer. */
5152 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5153 {
5154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5156 }
5157
5158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5159 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5160 hs_buf->data, msg_len + 12 );
5161
5162 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5163 ssl->in_hslen = msg_len + 12;
5164 ssl->in_msglen = msg_len + 12;
5165 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5166
5167 ret = 0;
5168 goto exit;
5169 }
5170 else
5171 {
5172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5173 hs->in_msg_seq ) );
5174 }
5175
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005176 ret = -1;
5177
5178exit:
5179
5180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5181 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005182}
5183
Hanno Beckera02b0b42018-08-21 17:20:27 +01005184static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5185 size_t desired )
5186{
5187 int offset;
5188 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5190 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191
Hanno Becker01315ea2018-08-21 17:22:17 +01005192 /* Get rid of future records epoch first, if such exist. */
5193 ssl_free_buffered_record( ssl );
5194
5195 /* Check if we have enough space available now. */
5196 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5197 hs->buffering.total_bytes_buffered ) )
5198 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005200 return( 0 );
5201 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005202
Hanno Becker4f432ad2018-08-28 10:02:32 +01005203 /* We don't have enough space to buffer the next expected handshake
5204 * message. Remove buffers used for future messages to gain space,
5205 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005206 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5207 offset >= 0; offset-- )
5208 {
5209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5210 offset ) );
5211
Hanno Beckerb309b922018-08-23 13:18:05 +01005212 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005213
5214 /* Check if we have enough space available now. */
5215 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5216 hs->buffering.total_bytes_buffered ) )
5217 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005219 return( 0 );
5220 }
5221 }
5222
5223 return( -1 );
5224}
5225
Hanno Becker40f50842018-08-15 14:48:01 +01005226static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5227{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005228 int ret = 0;
5229 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5230
5231 if( hs == NULL )
5232 return( 0 );
5233
5234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5235
5236 switch( ssl->in_msgtype )
5237 {
5238 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005240
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005241 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005242 break;
5243
5244 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005245 {
5246 unsigned recv_msg_seq_offset;
5247 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5248 mbedtls_ssl_hs_buffer *hs_buf;
5249 size_t msg_len = ssl->in_hslen - 12;
5250
5251 /* We should never receive an old handshake
5252 * message - double-check nonetheless. */
5253 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5254 {
5255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5257 }
5258
5259 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5260 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5261 {
5262 /* Silently ignore -- message too far in the future */
5263 MBEDTLS_SSL_DEBUG_MSG( 2,
5264 ( "Ignore future HS message with sequence number %u, "
5265 "buffering window %u - %u",
5266 recv_msg_seq, ssl->handshake->in_msg_seq,
5267 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5268
5269 goto exit;
5270 }
5271
5272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5273 recv_msg_seq, recv_msg_seq_offset ) );
5274
5275 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5276
5277 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005278 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005279 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005280 size_t reassembly_buf_sz;
5281
Hanno Becker37f95322018-08-16 13:55:32 +01005282 hs_buf->is_fragmented =
5283 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5284
5285 /* We copy the message back into the input buffer
5286 * after reassembly, so check that it's not too large.
5287 * This is an implementation-specific limitation
5288 * and not one from the standard, hence it is not
5289 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005290 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005291 {
5292 /* Ignore message */
5293 goto exit;
5294 }
5295
Hanno Beckere0b150f2018-08-21 15:51:03 +01005296 /* Check if we have enough space to buffer the message. */
5297 if( hs->buffering.total_bytes_buffered >
5298 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5299 {
5300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5302 }
5303
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005304 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5305 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005306
5307 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5308 hs->buffering.total_bytes_buffered ) )
5309 {
5310 if( recv_msg_seq_offset > 0 )
5311 {
5312 /* If we can't buffer a future message because
5313 * of space limitations -- ignore. */
5314 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",
5315 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5316 (unsigned) hs->buffering.total_bytes_buffered ) );
5317 goto exit;
5318 }
Hanno Beckere1801392018-08-21 16:51:05 +01005319 else
5320 {
5321 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",
5322 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5323 (unsigned) hs->buffering.total_bytes_buffered ) );
5324 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005325
Hanno Beckera02b0b42018-08-21 17:20:27 +01005326 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005327 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005328 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",
5329 (unsigned) msg_len,
5330 (unsigned) reassembly_buf_sz,
5331 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005332 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005333 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5334 goto exit;
5335 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005336 }
5337
5338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5339 msg_len ) );
5340
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005341 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5342 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005343 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005344 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005345 goto exit;
5346 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005347 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005348
5349 /* Prepare final header: copy msg_type, length and message_seq,
5350 * then add standardised fragment_offset and fragment_length */
5351 memcpy( hs_buf->data, ssl->in_msg, 6 );
5352 memset( hs_buf->data + 6, 0, 3 );
5353 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5354
5355 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005356
5357 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005358 }
5359 else
5360 {
5361 /* Make sure msg_type and length are consistent */
5362 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5363 {
5364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5365 /* Ignore */
5366 goto exit;
5367 }
5368 }
5369
Hanno Becker4422bbb2018-08-20 09:40:19 +01005370 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005371 {
5372 size_t frag_len, frag_off;
5373 unsigned char * const msg = hs_buf->data + 12;
5374
5375 /*
5376 * Check and copy current fragment
5377 */
5378
5379 /* Validation of header fields already done in
5380 * mbedtls_ssl_prepare_handshake_record(). */
5381 frag_off = ssl_get_hs_frag_off( ssl );
5382 frag_len = ssl_get_hs_frag_len( ssl );
5383
5384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5385 frag_off, frag_len ) );
5386 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5387
5388 if( hs_buf->is_fragmented )
5389 {
5390 unsigned char * const bitmask = msg + msg_len;
5391 ssl_bitmask_set( bitmask, frag_off, frag_len );
5392 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5393 msg_len ) == 0 );
5394 }
5395 else
5396 {
5397 hs_buf->is_complete = 1;
5398 }
5399
5400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5401 hs_buf->is_complete ? "" : "not yet " ) );
5402 }
5403
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005404 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005405 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005406
5407 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005408 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005409 break;
5410 }
5411
5412exit:
5413
5414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5415 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005416}
5417#endif /* MBEDTLS_SSL_PROTO_DTLS */
5418
Hanno Becker1097b342018-08-15 14:09:41 +01005419static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005420{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005421 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005422 * Consume last content-layer message and potentially
5423 * update in_msglen which keeps track of the contents'
5424 * consumption state.
5425 *
5426 * (1) Handshake messages:
5427 * Remove last handshake message, move content
5428 * and adapt in_msglen.
5429 *
5430 * (2) Alert messages:
5431 * Consume whole record content, in_msglen = 0.
5432 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005433 * (3) Change cipher spec:
5434 * Consume whole record content, in_msglen = 0.
5435 *
5436 * (4) Application data:
5437 * Don't do anything - the record layer provides
5438 * the application data as a stream transport
5439 * and consumes through mbedtls_ssl_read only.
5440 *
5441 */
5442
5443 /* Case (1): Handshake messages */
5444 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005445 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005446 /* Hard assertion to be sure that no application data
5447 * is in flight, as corrupting ssl->in_msglen during
5448 * ssl->in_offt != NULL is fatal. */
5449 if( ssl->in_offt != NULL )
5450 {
5451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5453 }
5454
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005455 /*
5456 * Get next Handshake message in the current record
5457 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005458
Hanno Becker4a810fb2017-05-24 16:27:30 +01005459 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005460 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005461 * current handshake content: If DTLS handshake
5462 * fragmentation is used, that's the fragment
5463 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005464 * size here is faulty and should be changed at
5465 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005466 * (2) While it doesn't seem to cause problems, one
5467 * has to be very careful not to assume that in_hslen
5468 * is always <= in_msglen in a sensible communication.
5469 * Again, it's wrong for DTLS handshake fragmentation.
5470 * The following check is therefore mandatory, and
5471 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005472 * Additionally, ssl->in_hslen might be arbitrarily out of
5473 * bounds after handling a DTLS message with an unexpected
5474 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005475 */
5476 if( ssl->in_hslen < ssl->in_msglen )
5477 {
5478 ssl->in_msglen -= ssl->in_hslen;
5479 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5480 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005481
Hanno Becker4a810fb2017-05-24 16:27:30 +01005482 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5483 ssl->in_msg, ssl->in_msglen );
5484 }
5485 else
5486 {
5487 ssl->in_msglen = 0;
5488 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005489
Hanno Becker4a810fb2017-05-24 16:27:30 +01005490 ssl->in_hslen = 0;
5491 }
5492 /* Case (4): Application data */
5493 else if( ssl->in_offt != NULL )
5494 {
5495 return( 0 );
5496 }
5497 /* Everything else (CCS & Alerts) */
5498 else
5499 {
5500 ssl->in_msglen = 0;
5501 }
5502
Hanno Becker1097b342018-08-15 14:09:41 +01005503 return( 0 );
5504}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005505
Hanno Beckere74d5562018-08-15 14:26:08 +01005506static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5507{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005508 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005509 return( 1 );
5510
5511 return( 0 );
5512}
5513
Hanno Becker5f066e72018-08-16 14:56:31 +01005514#if defined(MBEDTLS_SSL_PROTO_DTLS)
5515
5516static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5517{
5518 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5519 if( hs == NULL )
5520 return;
5521
Hanno Becker01315ea2018-08-21 17:22:17 +01005522 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005523 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005524 hs->buffering.total_bytes_buffered -=
5525 hs->buffering.future_record.len;
5526
5527 mbedtls_free( hs->buffering.future_record.data );
5528 hs->buffering.future_record.data = NULL;
5529 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005530}
5531
5532static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5533{
5534 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5535 unsigned char * rec;
5536 size_t rec_len;
5537 unsigned rec_epoch;
5538
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005539 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005540 return( 0 );
5541
5542 if( hs == NULL )
5543 return( 0 );
5544
Hanno Becker5f066e72018-08-16 14:56:31 +01005545 rec = hs->buffering.future_record.data;
5546 rec_len = hs->buffering.future_record.len;
5547 rec_epoch = hs->buffering.future_record.epoch;
5548
5549 if( rec == NULL )
5550 return( 0 );
5551
Hanno Becker4cb782d2018-08-20 11:19:05 +01005552 /* Only consider loading future records if the
5553 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005554 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005555 return( 0 );
5556
Hanno Becker5f066e72018-08-16 14:56:31 +01005557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5558
5559 if( rec_epoch != ssl->in_epoch )
5560 {
5561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5562 goto exit;
5563 }
5564
5565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5566
5567 /* Double-check that the record is not too large */
5568 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5569 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5573 }
5574
5575 memcpy( ssl->in_hdr, rec, rec_len );
5576 ssl->in_left = rec_len;
5577 ssl->next_record_offset = 0;
5578
5579 ssl_free_buffered_record( ssl );
5580
5581exit:
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5583 return( 0 );
5584}
5585
5586static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5587{
5588 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5589 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005590 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005591
5592 /* Don't buffer future records outside handshakes. */
5593 if( hs == NULL )
5594 return( 0 );
5595
5596 /* Only buffer handshake records (we are only interested
5597 * in Finished messages). */
5598 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5599 return( 0 );
5600
5601 /* Don't buffer more than one future epoch record. */
5602 if( hs->buffering.future_record.data != NULL )
5603 return( 0 );
5604
Hanno Becker01315ea2018-08-21 17:22:17 +01005605 /* Don't buffer record if there's not enough buffering space remaining. */
5606 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5607 hs->buffering.total_bytes_buffered ) )
5608 {
5609 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",
5610 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5611 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005612 return( 0 );
5613 }
5614
Hanno Becker5f066e72018-08-16 14:56:31 +01005615 /* Buffer record */
5616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5617 ssl->in_epoch + 1 ) );
5618 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5619 rec_hdr_len + ssl->in_msglen );
5620
5621 /* ssl_parse_record_header() only considers records
5622 * of the next epoch as candidates for buffering. */
5623 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005624 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005625
5626 hs->buffering.future_record.data =
5627 mbedtls_calloc( 1, hs->buffering.future_record.len );
5628 if( hs->buffering.future_record.data == NULL )
5629 {
5630 /* If we run out of RAM trying to buffer a
5631 * record from the next epoch, just ignore. */
5632 return( 0 );
5633 }
5634
Hanno Becker01315ea2018-08-21 17:22:17 +01005635 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005636
Hanno Becker01315ea2018-08-21 17:22:17 +01005637 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005638 return( 0 );
5639}
5640
5641#endif /* MBEDTLS_SSL_PROTO_DTLS */
5642
Hanno Beckere74d5562018-08-15 14:26:08 +01005643static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005644{
5645 int ret;
5646
Hanno Becker5f066e72018-08-16 14:56:31 +01005647#if defined(MBEDTLS_SSL_PROTO_DTLS)
5648 /* We might have buffered a future record; if so,
5649 * and if the epoch matches now, load it.
5650 * On success, this call will set ssl->in_left to
5651 * the length of the buffered record, so that
5652 * the calls to ssl_fetch_input() below will
5653 * essentially be no-ops. */
5654 ret = ssl_load_buffered_record( ssl );
5655 if( ret != 0 )
5656 return( ret );
5657#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005658
Hanno Becker8b09b732019-05-08 12:03:28 +01005659 /* Reset in pointers to default state for TLS/DTLS records,
5660 * assuming no CID and no offset between record content and
5661 * record plaintext. */
5662 ssl_update_in_pointers( ssl );
5663
5664 /* Ensure that we have enough space available for the default form
5665 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5666 * with no space for CIDs counted in). */
5667 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5668 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005671 return( ret );
5672 }
5673
5674 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005677 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005678 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005679 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005680 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5681 {
5682 ret = ssl_buffer_future_record( ssl );
5683 if( ret != 0 )
5684 return( ret );
5685
5686 /* Fall through to handling of unexpected records */
5687 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5688 }
5689
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005690 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5691 {
5692 /* Skip unexpected record (but not whole datagram) */
5693 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005694 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005695
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5697 "(header)" ) );
5698 }
5699 else
5700 {
5701 /* Skip invalid record and the rest of the datagram */
5702 ssl->next_record_offset = 0;
5703 ssl->in_left = 0;
5704
5705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5706 "(header)" ) );
5707 }
5708
5709 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005710 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005711 }
5712#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005713 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005714 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005715
5716 /*
5717 * Read and optionally decrypt the message contents
5718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005720 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005723 return( ret );
5724 }
5725
5726 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005728 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005729 {
Hanno Becker43395762019-05-03 14:46:38 +01005730 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005731 if( ssl->next_record_offset < ssl->in_left )
5732 {
5733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5734 }
5735 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005736 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005737#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005738#if defined(MBEDTLS_SSL_PROTO_TLS)
5739 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005740 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005741 }
5742#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005743
5744 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005747 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005748 {
5749 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005750 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005751 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005752 /* Except when waiting for Finished as a bad mac here
5753 * probably means something went wrong in the handshake
5754 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5755 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5756 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5757 {
5758#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5759 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5760 {
5761 mbedtls_ssl_send_alert_message( ssl,
5762 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5763 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5764 }
5765#endif
5766 return( ret );
5767 }
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005770 if( ssl->conf->badmac_limit != 0 &&
5771 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5774 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005775 }
5776#endif
5777
Hanno Becker4a810fb2017-05-24 16:27:30 +01005778 /* As above, invalid records cause
5779 * dismissal of the whole datagram. */
5780
5781 ssl->next_record_offset = 0;
5782 ssl->in_left = 0;
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005785 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005786 }
5787
5788 return( ret );
5789 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005790 MBEDTLS_SSL_TRANSPORT_ELSE
5791#endif /* MBEDTLS_SSL_PROTO_DTLS */
5792#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005793 {
5794 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5796 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 mbedtls_ssl_send_alert_message( ssl,
5799 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5800 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005801 }
5802#endif
5803 return( ret );
5804 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005805#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005806 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005807
Simon Butcher99000142016-10-13 17:21:01 +01005808 return( 0 );
5809}
5810
5811int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5812{
5813 int ret;
5814
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005815 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005816 * Handle particular types of records
5817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 {
Simon Butcher99000142016-10-13 17:21:01 +01005820 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5821 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005822 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 }
5825
Hanno Beckere678eaa2018-08-21 14:57:46 +01005826 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005827 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005828 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005829 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5831 ssl->in_msglen ) );
5832 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005833 }
5834
Hanno Beckere678eaa2018-08-21 14:57:46 +01005835 if( ssl->in_msg[0] != 1 )
5836 {
5837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5838 ssl->in_msg[0] ) );
5839 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5840 }
5841
5842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005843 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005844 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5845 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5846 {
5847 if( ssl->handshake == NULL )
5848 {
5849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5850 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5851 }
5852
5853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5854 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5855 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005856#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005857 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005861 if( ssl->in_msglen != 2 )
5862 {
5863 /* Note: Standard allows for more than one 2 byte alert
5864 to be packed in a single message, but Mbed TLS doesn't
5865 currently support this. */
5866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5867 ssl->in_msglen ) );
5868 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5869 }
5870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005872 ssl->in_msg[0], ssl->in_msg[1] ) );
5873
5874 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005875 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005880 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 }
5883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5885 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5888 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005890
5891#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5892 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5893 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5894 {
Hanno Becker90333da2017-10-10 11:27:13 +01005895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005896 /* Will be handled when trying to parse ServerHello */
5897 return( 0 );
5898 }
5899#endif
5900
5901#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5902 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5903 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5904 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5905 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5906 {
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5908 /* Will be handled in mbedtls_ssl_parse_certificate() */
5909 return( 0 );
5910 }
5911#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5912
5913 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005914 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005915 }
5916
Hanno Beckerc76c6192017-06-06 10:03:17 +01005917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005918 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005919 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005920 /* Drop unexpected ApplicationData records,
5921 * except at the beginning of renegotiations */
5922 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5923 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5924#if defined(MBEDTLS_SSL_RENEGOTIATION)
5925 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5926 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005927#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005928 )
5929 {
5930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5931 return( MBEDTLS_ERR_SSL_NON_FATAL );
5932 }
5933
5934 if( ssl->handshake != NULL &&
5935 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5936 {
5937 ssl_handshake_wrapup_free_hs_transform( ssl );
5938 }
5939 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005940#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005941
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 return( 0 );
5943}
5944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005946{
5947 int ret;
5948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5950 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5951 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005952 {
5953 return( ret );
5954 }
5955
5956 return( 0 );
5957}
5958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005960 unsigned char level,
5961 unsigned char message )
5962{
5963 int ret;
5964
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005965 if( ssl == NULL || ssl->conf == NULL )
5966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005972 ssl->out_msglen = 2;
5973 ssl->out_msg[0] = level;
5974 ssl->out_msg[1] = message;
5975
Hanno Becker67bc7c32018-08-06 11:33:50 +01005976 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005979 return( ret );
5980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005982
5983 return( 0 );
5984}
5985
Paul Bakker5121ce52009-01-03 21:22:43 +00005986/*
5987 * Handshake functions
5988 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00005989#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005990/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005992{
Hanno Becker8759e162017-12-27 21:34:08 +00005993 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005996
Hanno Becker5097cba2019-02-05 13:36:46 +00005997 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006000 ssl->state++;
6001 return( 0 );
6002 }
6003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6005 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006006}
6007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006009{
Hanno Becker8759e162017-12-27 21:34:08 +00006010 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006013
Hanno Becker5097cba2019-02-05 13:36:46 +00006014 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006017 ssl->state++;
6018 return( 0 );
6019 }
6020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006023}
Gilles Peskinef9828522017-05-03 12:28:43 +02006024
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006025#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006026/* Some certificate support -> implement write and parse */
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006031 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006033 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006036
Hanno Becker5097cba2019-02-05 13:36:46 +00006037 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040 ssl->state++;
6041 return( 0 );
6042 }
6043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006045 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 {
6047 if( ssl->client_auth == 0 )
6048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 ssl->state++;
6051 return( 0 );
6052 }
6053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 /*
6056 * If using SSLv3 and got no cert, send an Alert message
6057 * (otherwise an empty Certificate message will be sent).
6058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6060 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 {
6062 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6064 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6065 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 goto write_msg;
6069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072#endif /* MBEDTLS_SSL_CLI_C */
6073#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006074 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6079 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006080 }
6081 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006085
6086 /*
6087 * 0 . 0 handshake type
6088 * 1 . 3 handshake length
6089 * 4 . 6 length of all certs
6090 * 7 . 9 length of cert. 1
6091 * 10 . n-1 peer certificate
6092 * n . n+2 length of cert. 2
6093 * n+3 . ... upper level cert, etc.
6094 */
6095 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
Paul Bakker29087132010-03-21 21:03:34 +00006098 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
6100 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006101 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006104 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 }
6107
6108 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6109 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6110 ssl->out_msg[i + 2] = (unsigned char)( n );
6111
6112 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6113 i += n; crt = crt->next;
6114 }
6115
6116 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6117 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6118 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6119
6120 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6122 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006123
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006124#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006125write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006127
6128 ssl->state++;
6129
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006130 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 return( ret );
6134 }
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
Paul Bakkered27a042013-04-18 22:46:23 +02006138 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006139}
6140
Hanno Becker285ff0c2019-01-31 07:44:03 +00006141#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006142
6143#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006144static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6145 unsigned char *crt_buf,
6146 size_t crt_buf_len )
6147{
6148 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6149
6150 if( peer_crt == NULL )
6151 return( -1 );
6152
6153 if( peer_crt->raw.len != crt_buf_len )
6154 return( -1 );
6155
Hanno Becker68b856d2019-02-08 14:00:04 +00006156 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006157}
Hanno Beckerdf759382019-02-05 17:02:46 +00006158#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6159static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6160 unsigned char *crt_buf,
6161 size_t crt_buf_len )
6162{
6163 int ret;
6164 unsigned char const * const peer_cert_digest =
6165 ssl->session->peer_cert_digest;
6166 mbedtls_md_type_t const peer_cert_digest_type =
6167 ssl->session->peer_cert_digest_type;
6168 mbedtls_md_info_t const * const digest_info =
6169 mbedtls_md_info_from_type( peer_cert_digest_type );
6170 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6171 size_t digest_len;
6172
6173 if( peer_cert_digest == NULL || digest_info == NULL )
6174 return( -1 );
6175
6176 digest_len = mbedtls_md_get_size( digest_info );
6177 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6178 return( -1 );
6179
6180 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6181 if( ret != 0 )
6182 return( -1 );
6183
6184 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6185}
6186#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006187#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006188
Hanno Becker22141592019-02-05 12:38:15 +00006189static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6190{
6191 if( session->peer_cert != NULL )
6192 {
6193 mbedtls_x509_crt_free( session->peer_cert );
6194 mbedtls_free( session->peer_cert );
6195 session->peer_cert = NULL;
6196 }
Hanno Becker9fb6e2e2019-02-05 17:00:50 +00006197
6198#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6199 if( session->peer_cert_digest != NULL )
6200 {
6201 /* Zeroization is not necessary. */
6202 mbedtls_free( session->peer_cert_digest );
6203 session->peer_cert_digest = NULL;
6204 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6205 session->peer_cert_digest_len = 0;
6206 }
6207#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker22141592019-02-05 12:38:15 +00006208}
6209
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006210/*
6211 * Once the certificate message is read, parse it into a cert chain and
6212 * perform basic checks, but leave actual verification to the caller
6213 */
Hanno Becker35e41772019-02-05 15:37:23 +00006214static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6215 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006216{
Hanno Becker35e41772019-02-05 15:37:23 +00006217 int ret;
6218#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6219 int crt_cnt=0;
6220#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006221 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006222 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006227 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6228 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 }
6231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6233 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006236 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6237 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006239 }
6240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006242
Paul Bakker5121ce52009-01-03 21:22:43 +00006243 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006245 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006246 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006247
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006248 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006252 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6253 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 }
6256
Hanno Becker33c3dc82019-01-30 14:46:46 +00006257 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6258 i += 3;
6259
Hanno Becker33c3dc82019-01-30 14:46:46 +00006260 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006261 while( i < ssl->in_hslen )
6262 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006263 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006264 if ( i + 3 > ssl->in_hslen ) {
6265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006266 mbedtls_ssl_send_alert_message( ssl,
6267 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6268 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006269 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6270 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006271 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6272 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006273 if( ssl->in_msg[i] != 0 )
6274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006276 mbedtls_ssl_send_alert_message( ssl,
6277 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6278 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006280 }
6281
Hanno Becker33c3dc82019-01-30 14:46:46 +00006282 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006283 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6284 | (unsigned int) ssl->in_msg[i + 2];
6285 i += 3;
6286
6287 if( n < 128 || i + n > ssl->in_hslen )
6288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006290 mbedtls_ssl_send_alert_message( ssl,
6291 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6292 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006294 }
6295
Hanno Becker33c3dc82019-01-30 14:46:46 +00006296 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006297#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6298 if( crt_cnt++ == 0 &&
6299 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6300 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006301 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006302 /* During client-side renegotiation, check that the server's
6303 * end-CRTs hasn't changed compared to the initial handshake,
6304 * mitigating the triple handshake attack. On success, reuse
6305 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6307 if( ssl_check_peer_crt_unchanged( ssl,
6308 &ssl->in_msg[i],
6309 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006310 {
Hanno Becker35e41772019-02-05 15:37:23 +00006311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6312 mbedtls_ssl_send_alert_message( ssl,
6313 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6314 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6315 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006316 }
Hanno Becker35e41772019-02-05 15:37:23 +00006317
6318 /* Now we can safely free the original chain. */
6319 ssl_clear_peer_cert( ssl->session );
6320 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006321#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6322
Hanno Becker33c3dc82019-01-30 14:46:46 +00006323 /* Parse the next certificate in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006324 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006325 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006326 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006327 case 0: /*ok*/
6328 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6329 /* Ignore certificate with an unknown algorithm: maybe a
6330 prior certificate was already trusted. */
6331 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006332
Hanno Becker33c3dc82019-01-30 14:46:46 +00006333 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6334 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6335 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006336
Hanno Becker33c3dc82019-01-30 14:46:46 +00006337 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6338 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6339 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006340
Hanno Becker33c3dc82019-01-30 14:46:46 +00006341 default:
6342 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6343 crt_parse_der_failed:
6344 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6345 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6346 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 }
6348
6349 i += n;
6350 }
6351
Hanno Becker35e41772019-02-05 15:37:23 +00006352 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006353 return( 0 );
6354}
6355
Hanno Beckerb8a08572019-02-05 12:49:06 +00006356#if defined(MBEDTLS_SSL_SRV_C)
6357static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6358{
6359 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6360 return( -1 );
6361
6362#if defined(MBEDTLS_SSL_PROTO_SSL3)
6363 /*
6364 * Check if the client sent an empty certificate
6365 */
6366 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6367 {
6368 if( ssl->in_msglen == 2 &&
6369 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6370 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6371 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6372 {
6373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6374 return( 0 );
6375 }
6376
6377 return( -1 );
6378 }
6379#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6380
6381#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6382 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6383 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6384 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6385 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6386 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6387 {
6388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6389 return( 0 );
6390 }
6391
6392 return( -1 );
6393#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6394 MBEDTLS_SSL_PROTO_TLS1_2 */
6395}
6396#endif /* MBEDTLS_SSL_SRV_C */
6397
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006398/* Check if a certificate message is expected.
6399 * Return either
6400 * - SSL_CERTIFICATE_EXPECTED, or
6401 * - SSL_CERTIFICATE_SKIP
6402 * indicating whether a Certificate message is expected or not.
6403 */
6404#define SSL_CERTIFICATE_EXPECTED 0
6405#define SSL_CERTIFICATE_SKIP 1
6406static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6407 int authmode )
6408{
6409 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6410 ssl->handshake->ciphersuite_info;
6411
6412 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6413 return( SSL_CERTIFICATE_SKIP );
6414
6415#if defined(MBEDTLS_SSL_SRV_C)
6416 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6417 {
6418 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6419 return( SSL_CERTIFICATE_SKIP );
6420
6421 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6422 {
6423 /* NOTE: Is it intentional that we set verify_result
6424 * to SKIP_VERIFY on server-side only? */
6425 ssl->session_negotiate->verify_result =
6426 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6427 return( SSL_CERTIFICATE_SKIP );
6428 }
6429 }
6430#endif /* MBEDTLS_SSL_SRV_C */
6431
6432 return( SSL_CERTIFICATE_EXPECTED );
6433}
6434
Hanno Becker3cf50612019-02-05 14:36:34 +00006435static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6436 int authmode,
6437 mbedtls_x509_crt *chain,
6438 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006439{
Hanno Becker613d4902019-02-05 13:11:17 +00006440 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006441 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6442 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006443 mbedtls_x509_crt *ca_chain;
6444 mbedtls_x509_crl *ca_crl;
6445
6446 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6447 return( 0 );
6448
6449#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6450 if( ssl->handshake->sni_ca_chain != NULL )
6451 {
6452 ca_chain = ssl->handshake->sni_ca_chain;
6453 ca_crl = ssl->handshake->sni_ca_crl;
6454 }
6455 else
6456#endif
6457 {
6458 ca_chain = ssl->conf->ca_chain;
6459 ca_crl = ssl->conf->ca_crl;
6460 }
6461
6462 /*
6463 * Main check: verify certificate
6464 */
6465 ret = mbedtls_x509_crt_verify_restartable(
6466 chain,
6467 ca_chain, ca_crl,
6468 ssl->conf->cert_profile,
6469 ssl->hostname,
6470 &ssl->session_negotiate->verify_result,
6471 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6472
6473 if( ret != 0 )
6474 {
6475 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6476 }
6477
6478#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6479 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6480 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6481#endif
6482
6483 /*
6484 * Secondary checks: always done, but change 'ret' only if it was 0
6485 */
6486
6487#if defined(MBEDTLS_ECP_C)
6488 {
6489 const mbedtls_pk_context *pk = &chain->pk;
6490
6491 /* If certificate uses an EC key, make sure the curve is OK */
6492 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6493 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6494 {
6495 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6496
6497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6498 if( ret == 0 )
6499 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6500 }
6501 }
6502#endif /* MBEDTLS_ECP_C */
6503
6504 if( mbedtls_ssl_check_cert_usage( chain,
6505 ciphersuite_info,
6506 ! ssl->conf->endpoint,
6507 &ssl->session_negotiate->verify_result ) != 0 )
6508 {
6509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6510 if( ret == 0 )
6511 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6512 }
6513
6514 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6515 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6516 * with details encoded in the verification flags. All other kinds
6517 * of error codes, including those from the user provided f_vrfy
6518 * functions, are treated as fatal and lead to a failure of
6519 * ssl_parse_certificate even if verification was optional. */
6520 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6521 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6522 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6523 {
6524 ret = 0;
6525 }
6526
6527 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6528 {
6529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6530 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6531 }
6532
6533 if( ret != 0 )
6534 {
6535 uint8_t alert;
6536
6537 /* The certificate may have been rejected for several reasons.
6538 Pick one and send the corresponding alert. Which alert to send
6539 may be a subject of debate in some cases. */
6540 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6541 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6542 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6543 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6544 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6545 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6546 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6547 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6548 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6549 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6550 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6551 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6552 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6553 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6554 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6555 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6556 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6557 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6558 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6559 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6560 else
6561 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6562 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6563 alert );
6564 }
6565
6566#if defined(MBEDTLS_DEBUG_C)
6567 if( ssl->session_negotiate->verify_result != 0 )
6568 {
6569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6570 ssl->session_negotiate->verify_result ) );
6571 }
6572 else
6573 {
6574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6575 }
6576#endif /* MBEDTLS_DEBUG_C */
6577
6578 return( ret );
6579}
6580
6581int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6582{
6583 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006584 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006585#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6586 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6587 ? ssl->handshake->sni_authmode
6588 : ssl->conf->authmode;
6589#else
6590 const int authmode = ssl->conf->authmode;
6591#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006592 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006593 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006594
6595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6596
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006597 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6598 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006599 {
6600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006601 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006602 }
6603
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006604#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6605 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006606 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006607 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006608 chain = ssl->handshake->ecrs_peer_cert;
6609 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006610 goto crt_verify;
6611 }
6612#endif
6613
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006614 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006615 {
6616 /* mbedtls_ssl_read_record may have sent an alert already. We
6617 let it decide whether to alert. */
6618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006619 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006620 }
6621
Hanno Beckerb8a08572019-02-05 12:49:06 +00006622#if defined(MBEDTLS_SSL_SRV_C)
6623 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6624 {
6625 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006626
6627 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006628 ret = 0;
6629 else
6630 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006631
Hanno Becker613d4902019-02-05 13:11:17 +00006632 goto exit;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006633 }
6634#endif /* MBEDTLS_SSL_SRV_C */
6635
Hanno Becker35e41772019-02-05 15:37:23 +00006636 /* Clear existing peer CRT structure in case we tried to
6637 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006638 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006639
6640 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6641 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006642 {
6643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6644 sizeof( mbedtls_x509_crt ) ) );
6645 mbedtls_ssl_send_alert_message( ssl,
6646 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6647 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006648
Hanno Beckere4aeb762019-02-05 17:19:52 +00006649 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6650 goto exit;
6651 }
6652 mbedtls_x509_crt_init( chain );
6653
6654 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006655 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006656 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006657
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006658#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6659 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006660 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006661
6662crt_verify:
6663 if( ssl->handshake->ecrs_enabled)
6664 rs_ctx = &ssl->handshake->ecrs_ctx;
6665#endif
6666
Hanno Becker3cf50612019-02-05 14:36:34 +00006667 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006668 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006669 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006670 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
Hanno Becker3008d282019-02-05 17:02:28 +00006672#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckere4aeb762019-02-05 17:19:52 +00006673
Hanno Becker3008d282019-02-05 17:02:28 +00006674 /* Remember digest of the peer's end-CRT. */
6675 ssl->session_negotiate->peer_cert_digest =
6676 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6677 if( ssl->session_negotiate->peer_cert_digest == NULL )
6678 {
6679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6680 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6681 mbedtls_ssl_send_alert_message( ssl,
6682 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6683 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006684
6685 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6686 goto exit;
Hanno Becker3008d282019-02-05 17:02:28 +00006687 }
6688 ret = mbedtls_md( mbedtls_md_info_from_type(
Hanno Beckere4aeb762019-02-05 17:19:52 +00006689 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6690 chain->raw.p, chain->raw.len,
Hanno Becker3008d282019-02-05 17:02:28 +00006691 ssl->session_negotiate->peer_cert_digest );
6692 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006693 goto exit;
Hanno Becker3008d282019-02-05 17:02:28 +00006694
6695 ssl->session_negotiate->peer_cert_digest_type =
6696 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6697 ssl->session_negotiate->peer_cert_digest_len =
6698 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6699#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6700
Hanno Beckercf291d62019-02-06 16:19:04 +00006701#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6702 /* Make a copy of the peer's raw public key. */
6703 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6704 {
6705 unsigned char *p, *end;
6706 p = chain->pk_raw.p;
6707 end = p + chain->pk_raw.len;
6708 ret = mbedtls_pk_parse_subpubkey( &p, end,
6709 &ssl->handshake->peer_pubkey );
6710 if( ret != 0 )
6711 {
6712 /* We should have parsed the public key before. */
6713 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6714 goto exit;
6715 }
6716 }
6717#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6718
Hanno Beckere4aeb762019-02-05 17:19:52 +00006719 ssl->session_negotiate->peer_cert = chain;
6720 chain = NULL;
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006723
Hanno Becker613d4902019-02-05 13:11:17 +00006724exit:
6725
Hanno Beckere4aeb762019-02-05 17:19:52 +00006726 if( ret == 0 )
6727 ssl->state++;
6728
6729#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6730 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6731 {
6732 ssl->handshake->ecrs_peer_cert = chain;
6733 chain = NULL;
6734 }
6735#endif
6736
6737 if( chain != NULL )
6738 {
6739 mbedtls_x509_crt_free( chain );
6740 mbedtls_free( chain );
6741 }
6742
Paul Bakker5121ce52009-01-03 21:22:43 +00006743 return( ret );
6744}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006745#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006748{
6749 int ret;
6750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006754 ssl->out_msglen = 1;
6755 ssl->out_msg[0] = 1;
6756
Paul Bakker5121ce52009-01-03 21:22:43 +00006757 ssl->state++;
6758
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006759 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006760 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006762 return( ret );
6763 }
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006766
6767 return( 0 );
6768}
6769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006771{
6772 int ret;
6773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775
Hanno Becker327c93b2018-08-15 13:56:18 +01006776 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 return( ret );
6780 }
6781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006785 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6786 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006788 }
6789
Hanno Beckere678eaa2018-08-21 14:57:46 +01006790 /* CCS records are only accepted if they have length 1 and content '1',
6791 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006792
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006793 /*
6794 * Switch to our negotiated transform and session parameters for inbound
6795 * data.
6796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006798 ssl->transform_in = ssl->transform_negotiate;
6799 ssl->session_in = ssl->session_negotiate;
6800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006802 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006805 ssl_dtls_replay_reset( ssl );
6806#endif
6807
6808 /* Increment epoch */
6809 if( ++ssl->in_epoch == 0 )
6810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006812 /* This is highly unlikely to happen for legitimate reasons, so
6813 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006815 }
6816 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006817 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006819#if defined(MBEDTLS_SSL_PROTO_TLS)
6820 {
6821 memset( ssl->in_ctr, 0, 8 );
6822 }
6823#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006824
Hanno Beckerf5970a02019-05-08 09:38:41 +01006825 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6828 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006833 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6834 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006836 }
6837 }
6838#endif
6839
Paul Bakker5121ce52009-01-03 21:22:43 +00006840 ssl->state++;
6841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006843
6844 return( 0 );
6845}
6846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6848 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006849{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006850 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6854 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006855 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006856 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6859#if defined(MBEDTLS_SHA512_C)
6860 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006861 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6862 else
6863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864#if defined(MBEDTLS_SHA256_C)
6865 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006866 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006867 else
6868#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006872 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006873 }
Paul Bakker380da532012-04-18 16:10:25 +00006874}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006877{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6879 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006880 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6881 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6884#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006885 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006888 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006891}
6892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006894 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006895{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6897 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006898 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6899 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6902#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006903 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006906 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006909}
6910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6912 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6913static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006914 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006915{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006916 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6917 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006918}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006919#endif
Paul Bakker380da532012-04-18 16:10:25 +00006920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6922#if defined(MBEDTLS_SHA256_C)
6923static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006924 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006925{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006926 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006927}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006928#endif
Paul Bakker380da532012-04-18 16:10:25 +00006929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930#if defined(MBEDTLS_SHA512_C)
6931static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006932 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006933{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006934 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006935}
Paul Bakker769075d2012-11-24 11:26:46 +01006936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006940static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006942{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006943 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006944 mbedtls_md5_context md5;
6945 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006946
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 unsigned char padbuf[48];
6948 unsigned char md5sum[16];
6949 unsigned char sha1sum[20];
6950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006952 if( !session )
6953 session = ssl->session;
6954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006956
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006957 mbedtls_md5_init( &md5 );
6958 mbedtls_sha1_init( &sha1 );
6959
6960 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6961 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006962
6963 /*
6964 * SSLv3:
6965 * hash =
6966 * MD5( master + pad2 +
6967 * MD5( handshake + sender + master + pad1 ) )
6968 * + SHA1( master + pad2 +
6969 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006970 */
6971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006973 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6974 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006975#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006978 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6979 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006980#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006983 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006984
Paul Bakker1ef83d62012-04-11 12:09:53 +00006985 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006986
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006987 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6988 mbedtls_md5_update_ret( &md5, session->master, 48 );
6989 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6990 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006991
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006992 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6993 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6994 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6995 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006996
Paul Bakker1ef83d62012-04-11 12:09:53 +00006997 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006998
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006999 mbedtls_md5_starts_ret( &md5 );
7000 mbedtls_md5_update_ret( &md5, session->master, 48 );
7001 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7002 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7003 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007004
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007005 mbedtls_sha1_starts_ret( &sha1 );
7006 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7007 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7008 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7009 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007012
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007013 mbedtls_md5_free( &md5 );
7014 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007015
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007016 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7017 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7018 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007021}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007025static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007027{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007028 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007029 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007030 mbedtls_md5_context md5;
7031 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007032 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007035 if( !session )
7036 session = ssl->session;
7037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007039
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007040 mbedtls_md5_init( &md5 );
7041 mbedtls_sha1_init( &sha1 );
7042
7043 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7044 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007045
Paul Bakker1ef83d62012-04-11 12:09:53 +00007046 /*
7047 * TLSv1:
7048 * hash = PRF( master, finished_label,
7049 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7050 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007053 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7054 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007055#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007058 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7059 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007060#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007063 ? "client finished"
7064 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007065
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007066 mbedtls_md5_finish_ret( &md5, padbuf );
7067 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007068
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007069 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007070 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007073
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007074 mbedtls_md5_free( &md5 );
7075 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007076
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007077 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7084#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007085static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007087{
7088 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007089 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007090 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007091 unsigned char padbuf[32];
7092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007094 if( !session )
7095 session = ssl->session;
7096
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007097 mbedtls_sha256_init( &sha256 );
7098
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007100
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007101 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007102
7103 /*
7104 * TLSv1.2:
7105 * hash = PRF( master, finished_label,
7106 * Hash( handshake ) )[0.11]
7107 */
7108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109#if !defined(MBEDTLS_SHA256_ALT)
7110 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007111 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007112#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007115 ? "client finished"
7116 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007117
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007118 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007119
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007120 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007121 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007124
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007125 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007126
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007127 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007134static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007136{
7137 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007138 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007139 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007140 unsigned char padbuf[48];
7141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007143 if( !session )
7144 session = ssl->session;
7145
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007146 mbedtls_sha512_init( &sha512 );
7147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007149
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007150 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007151
7152 /*
7153 * TLSv1.2:
7154 * hash = PRF( master, finished_label,
7155 * Hash( handshake ) )[0.11]
7156 */
7157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007159 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7160 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007161#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007164 ? "client finished"
7165 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007166
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007167 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007168
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007169 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007170 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007173
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007174 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007175
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007176 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180#endif /* MBEDTLS_SHA512_C */
7181#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007186
7187 /*
7188 * Free our handshake params
7189 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007190 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007192 ssl->handshake = NULL;
7193
7194 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007195 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007196 */
7197 if( ssl->transform )
7198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199 mbedtls_ssl_transform_free( ssl->transform );
7200 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007201 }
7202 ssl->transform = ssl->transform_negotiate;
7203 ssl->transform_negotiate = NULL;
7204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007206}
7207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007209{
7210 int resume = ssl->handshake->resume;
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214#if defined(MBEDTLS_SSL_RENEGOTIATION)
7215 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007218 ssl->renego_records_seen = 0;
7219 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007220#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007221
7222 /*
7223 * Free the previous session and switch in the current one
7224 */
Paul Bakker0a597072012-09-25 21:55:46 +00007225 if( ssl->session )
7226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007228 /* RFC 7366 3.1: keep the EtM state */
7229 ssl->session_negotiate->encrypt_then_mac =
7230 ssl->session->encrypt_then_mac;
7231#endif
7232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233 mbedtls_ssl_session_free( ssl->session );
7234 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007235 }
7236 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007237 ssl->session_negotiate = NULL;
7238
Paul Bakker0a597072012-09-25 21:55:46 +00007239 /*
7240 * Add cache entry
7241 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007242 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007243 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007244 resume == 0 )
7245 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007246 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007248 }
Paul Bakker0a597072012-09-25 21:55:46 +00007249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007251 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007252 ssl->handshake->flight != NULL )
7253 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007254 /* Cancel handshake timer */
7255 ssl_set_timer( ssl, 0 );
7256
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007257 /* Keep last flight around in case we need to resend it:
7258 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007260 }
7261 else
7262#endif
7263 ssl_handshake_wrapup_free_hs_transform( ssl );
7264
Paul Bakker48916f92012-09-16 19:57:18 +00007265 ssl->state++;
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007268}
7269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007271{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007272 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007275
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007276 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007277
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007278 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007279
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007280 /*
7281 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7282 * may define some other value. Currently (early 2016), no defined
7283 * ciphersuite does this (and this is unlikely to change as activity has
7284 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007289 ssl->verify_data_len = hash_len;
7290 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007291#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007292
Paul Bakker5121ce52009-01-03 21:22:43 +00007293 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7295 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007296
7297 /*
7298 * In case of session resuming, invert the client and server
7299 * ChangeCipherSpec messages order.
7300 */
Paul Bakker0a597072012-09-25 21:55:46 +00007301 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007304 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007306#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007308 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007310#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007311 }
7312 else
7313 ssl->state++;
7314
Paul Bakker48916f92012-09-16 19:57:18 +00007315 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007316 * Switch to our negotiated transform and session parameters for outbound
7317 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007322 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007323 {
7324 unsigned char i;
7325
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007326 /* Remember current epoch settings for resending */
7327 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007328 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007329
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007330 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007331 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007332
7333 /* Increment epoch */
7334 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007335 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007336 break;
7337
7338 /* The loop goes to its end iff the counter is wrapping */
7339 if( i == 0 )
7340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7342 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007343 }
7344 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007345 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007347#if defined(MBEDTLS_SSL_PROTO_TLS)
7348 {
7349 memset( ssl->cur_out_ctr, 0, 8 );
7350 }
7351#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007352
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007353 ssl->transform_out = ssl->transform_negotiate;
7354 ssl->session_out = ssl->session_negotiate;
7355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7357 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7362 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007363 }
7364 }
7365#endif
7366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007368 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007370#endif
7371
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007372 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007373 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007375 return( ret );
7376 }
7377
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007378#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007379 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007380 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7381 {
7382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7383 return( ret );
7384 }
7385#endif
7386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007388
7389 return( 0 );
7390}
7391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007393#define SSL_MAX_HASH_LEN 36
7394#else
7395#define SSL_MAX_HASH_LEN 12
7396#endif
7397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007399{
Paul Bakker23986e52011-04-24 08:57:21 +00007400 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007401 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007402 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007405
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007406 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007407
Hanno Becker327c93b2018-08-15 13:56:18 +01007408 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007411 return( ret );
7412 }
7413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007417 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7418 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007420 }
7421
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007422 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#if defined(MBEDTLS_SSL_PROTO_SSL3)
7424 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007425 hash_len = 36;
7426 else
7427#endif
7428 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7431 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007434 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7435 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007437 }
7438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007440 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7444 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007446 }
7447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007449 ssl->verify_data_len = hash_len;
7450 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007451#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007452
Paul Bakker0a597072012-09-25 21:55:46 +00007453 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007456 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007458#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007460 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007462#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007463 }
7464 else
7465 ssl->state++;
7466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007468 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007470#endif
7471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007473
7474 return( 0 );
7475}
7476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007478{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7482 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7483 mbedtls_md5_init( &handshake->fin_md5 );
7484 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007485 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7486 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007487#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7489#if defined(MBEDTLS_SHA256_C)
7490 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007491 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007492#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#if defined(MBEDTLS_SHA512_C)
7494 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007495 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007496#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007498
7499 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007500
7501#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7502 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7503 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7504#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if defined(MBEDTLS_DHM_C)
7507 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509#if defined(MBEDTLS_ECDH_C)
7510 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007511#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007512#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007513 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007514#if defined(MBEDTLS_SSL_CLI_C)
7515 handshake->ecjpake_cache = NULL;
7516 handshake->ecjpake_cache_len = 0;
7517#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007518#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007519
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007520#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007521 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007522#endif
7523
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007524#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7525 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7526#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007527
7528#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7529 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7530 mbedtls_pk_init( &handshake->peer_pubkey );
7531#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007532}
7533
Hanno Becker611a83b2018-01-03 14:27:32 +00007534void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007535{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7539 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007540
Hanno Becker92231322018-01-03 15:32:51 +00007541#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 mbedtls_md_init( &transform->md_ctx_enc );
7543 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007544#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007545}
7546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007550}
7551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007553{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007554 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007555 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007557 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007559 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007560 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007561
7562 /*
7563 * Either the pointers are now NULL or cleared properly and can be freed.
7564 * Now allocate missing structures.
7565 */
7566 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007567 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007568 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007569 }
Paul Bakker48916f92012-09-16 19:57:18 +00007570
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007571 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007572 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007573 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007574 }
Paul Bakker48916f92012-09-16 19:57:18 +00007575
Paul Bakker82788fb2014-10-20 13:59:19 +02007576 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007577 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007578 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007579 }
Paul Bakker48916f92012-09-16 19:57:18 +00007580
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007581 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007582 if( ssl->handshake == NULL ||
7583 ssl->transform_negotiate == NULL ||
7584 ssl->session_negotiate == NULL )
7585 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 mbedtls_free( ssl->handshake );
7589 mbedtls_free( ssl->transform_negotiate );
7590 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007591
7592 ssl->handshake = NULL;
7593 ssl->transform_negotiate = NULL;
7594 ssl->session_negotiate = NULL;
7595
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007596 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007597 }
7598
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007599 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007601 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007602 ssl_handshake_params_init( ssl->handshake );
7603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007605 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007606 {
7607 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007608
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007609 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7610 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7611 else
7612 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007613
7614 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007615 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007616#endif
7617
Paul Bakker48916f92012-09-16 19:57:18 +00007618 return( 0 );
7619}
7620
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007621#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007622/* Dummy cookie callbacks for defaults */
7623static int ssl_cookie_write_dummy( void *ctx,
7624 unsigned char **p, unsigned char *end,
7625 const unsigned char *cli_id, size_t cli_id_len )
7626{
7627 ((void) ctx);
7628 ((void) p);
7629 ((void) end);
7630 ((void) cli_id);
7631 ((void) cli_id_len);
7632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007634}
7635
7636static int ssl_cookie_check_dummy( void *ctx,
7637 const unsigned char *cookie, size_t cookie_len,
7638 const unsigned char *cli_id, size_t cli_id_len )
7639{
7640 ((void) ctx);
7641 ((void) cookie);
7642 ((void) cookie_len);
7643 ((void) cli_id);
7644 ((void) cli_id_len);
7645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007647}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007648#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007649
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007650/* Once ssl->out_hdr as the address of the beginning of the
7651 * next outgoing record is set, deduce the other pointers.
7652 *
7653 * Note: For TLS, we save the implicit record sequence number
7654 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7655 * and the caller has to make sure there's space for this.
7656 */
7657
7658static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7659 mbedtls_ssl_transform *transform )
7660{
7661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007662 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007663 {
7664 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007665#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007666 ssl->out_cid = ssl->out_ctr + 8;
7667 ssl->out_len = ssl->out_cid;
7668 if( transform != NULL )
7669 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007670#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007671 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007672#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007673 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007674 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007675 MBEDTLS_SSL_TRANSPORT_ELSE
7676#endif /* MBEDTLS_SSL_PROTO_DTLS */
7677#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007678 {
7679 ssl->out_ctr = ssl->out_hdr - 8;
7680 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007681#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007682 ssl->out_cid = ssl->out_len;
7683#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007684 ssl->out_iv = ssl->out_hdr + 5;
7685 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007686#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007687
7688 /* Adjust out_msg to make space for explicit IV, if used. */
7689 if( transform != NULL &&
7690 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7691 {
7692 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7693 }
7694 else
7695 ssl->out_msg = ssl->out_iv;
7696}
7697
7698/* Once ssl->in_hdr as the address of the beginning of the
7699 * next incoming record is set, deduce the other pointers.
7700 *
7701 * Note: For TLS, we save the implicit record sequence number
7702 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7703 * and the caller has to make sure there's space for this.
7704 */
7705
Hanno Beckerf5970a02019-05-08 09:38:41 +01007706static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007707{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007708 /* This function sets the pointers to match the case
7709 * of unprotected TLS/DTLS records, with both ssl->in_iv
7710 * and ssl->in_msg pointing to the beginning of the record
7711 * content.
7712 *
7713 * When decrypting a protected record, ssl->in_msg
7714 * will be shifted to point to the beginning of the
7715 * record plaintext.
7716 */
7717
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007719 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007720 {
Hanno Becker70e79282019-05-03 14:34:53 +01007721 /* This sets the header pointers to match records
7722 * without CID. When we receive a record containing
7723 * a CID, the fields are shifted accordingly in
7724 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007725 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007726#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007727 ssl->in_cid = ssl->in_ctr + 8;
7728 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007729#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007730 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007731#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007732 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007733 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007734 MBEDTLS_SSL_TRANSPORT_ELSE
7735#endif /* MBEDTLS_SSL_PROTO_DTLS */
7736#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007737 {
7738 ssl->in_ctr = ssl->in_hdr - 8;
7739 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007740#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007741 ssl->in_cid = ssl->in_len;
7742#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007743 ssl->in_iv = ssl->in_hdr + 5;
7744 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007745#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007746
Hanno Beckerf5970a02019-05-08 09:38:41 +01007747 /* This will be adjusted at record decryption time. */
7748 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007749}
7750
Paul Bakker5121ce52009-01-03 21:22:43 +00007751/*
7752 * Initialize an SSL context
7753 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007754void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7755{
7756 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7757}
7758
7759/*
7760 * Setup an SSL context
7761 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007762
7763static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7764{
7765 /* Set the incoming and outgoing record pointers. */
7766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007767 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007768 {
7769 ssl->out_hdr = ssl->out_buf;
7770 ssl->in_hdr = ssl->in_buf;
7771 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007772 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007773#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007774#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007775 {
7776 ssl->out_hdr = ssl->out_buf + 8;
7777 ssl->in_hdr = ssl->in_buf + 8;
7778 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007779#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007780
7781 /* Derive other internal pointers. */
7782 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007783 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007784}
7785
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007786int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007787 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007788{
Paul Bakker48916f92012-09-16 19:57:18 +00007789 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007790
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007791 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007792
7793 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007794 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007795 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007796
7797 /* Set to NULL in case of an error condition */
7798 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007799
Angus Grattond8213d02016-05-25 20:56:48 +10007800 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7801 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007802 {
Angus Grattond8213d02016-05-25 20:56:48 +10007803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007804 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007805 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007806 }
7807
7808 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7809 if( ssl->out_buf == NULL )
7810 {
7811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007812 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007813 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007814 }
7815
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007816 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007817
Paul Bakker48916f92012-09-16 19:57:18 +00007818 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007819 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007820
7821 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007822
7823error:
7824 mbedtls_free( ssl->in_buf );
7825 mbedtls_free( ssl->out_buf );
7826
7827 ssl->conf = NULL;
7828
7829 ssl->in_buf = NULL;
7830 ssl->out_buf = NULL;
7831
7832 ssl->in_hdr = NULL;
7833 ssl->in_ctr = NULL;
7834 ssl->in_len = NULL;
7835 ssl->in_iv = NULL;
7836 ssl->in_msg = NULL;
7837
7838 ssl->out_hdr = NULL;
7839 ssl->out_ctr = NULL;
7840 ssl->out_len = NULL;
7841 ssl->out_iv = NULL;
7842 ssl->out_msg = NULL;
7843
k-stachowiak9f7798e2018-07-31 16:52:32 +02007844 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007845}
7846
7847/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007848 * Reset an initialized and used SSL context for re-use while retaining
7849 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007850 *
7851 * If partial is non-zero, keep data in the input buffer and client ID.
7852 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007853 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007854static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007855{
Paul Bakker48916f92012-09-16 19:57:18 +00007856 int ret;
7857
Hanno Becker7e772132018-08-10 12:38:21 +01007858#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7859 !defined(MBEDTLS_SSL_SRV_C)
7860 ((void) partial);
7861#endif
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007864
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007865 /* Cancel any possibly running timer */
7866 ssl_set_timer( ssl, 0 );
7867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_RENEGOTIATION)
7869 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007870 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007871
7872 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7874 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007875#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007877
Paul Bakker7eb013f2011-10-06 12:37:39 +00007878 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007879 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007880
7881 ssl->in_msgtype = 0;
7882 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007884 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007885 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007888 ssl_dtls_replay_reset( ssl );
7889#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007890
7891 ssl->in_hslen = 0;
7892 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007893
7894 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007895
7896 ssl->out_msgtype = 0;
7897 ssl->out_msglen = 0;
7898 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7900 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007901 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007902#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007903
Hanno Becker19859472018-08-06 09:40:20 +01007904 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7905
Paul Bakker48916f92012-09-16 19:57:18 +00007906 ssl->transform_in = NULL;
7907 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007908
Hanno Becker78640902018-08-13 16:35:15 +01007909 ssl->session_in = NULL;
7910 ssl->session_out = NULL;
7911
Angus Grattond8213d02016-05-25 20:56:48 +10007912 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007913
7914#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007915 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007916#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7917 {
7918 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007919 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007920 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7923 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7926 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7929 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007930 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007931 }
7932#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007933
Paul Bakker48916f92012-09-16 19:57:18 +00007934 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936 mbedtls_ssl_transform_free( ssl->transform );
7937 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007938 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007939 }
Paul Bakker48916f92012-09-16 19:57:18 +00007940
Paul Bakkerc0463502013-02-14 11:19:38 +01007941 if( ssl->session )
7942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 mbedtls_ssl_session_free( ssl->session );
7944 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007945 ssl->session = NULL;
7946 }
7947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007949 ssl->alpn_chosen = NULL;
7950#endif
7951
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007952#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007953#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007954 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007955#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007956 {
7957 mbedtls_free( ssl->cli_id );
7958 ssl->cli_id = NULL;
7959 ssl->cli_id_len = 0;
7960 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007961#endif
7962
Paul Bakker48916f92012-09-16 19:57:18 +00007963 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7964 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007965
7966 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007967}
7968
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007969/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007970 * Reset an initialized and used SSL context for re-use while retaining
7971 * all application-set variables, function pointers and data.
7972 */
7973int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7974{
7975 return( ssl_session_reset_int( ssl, 0 ) );
7976}
7977
7978/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007979 * SSL set accessors
7980 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007981void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007982{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007983 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007984}
7985
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007986void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007987{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007988 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007989}
7990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007992void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007994 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007995}
7996#endif
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007999void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008000{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008001 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008002}
8003#endif
8004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008006
Hanno Becker1841b0a2018-08-24 11:13:57 +01008007void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8008 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008009{
8010 ssl->disable_datagram_packing = !allow_packing;
8011}
8012
8013void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8014 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008015{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008016 conf->hs_timeout_min = min;
8017 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008018}
8019#endif
8020
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008021void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008022{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008023 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008024}
8025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008027void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008028 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008029 void *p_vrfy )
8030{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008031 conf->f_vrfy = f_vrfy;
8032 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008035
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008036void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008037 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008038 void *p_rng )
8039{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008040 conf->f_rng = f_rng;
8041 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008042}
8043
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008044void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008045 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008046 void *p_dbg )
8047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008048 conf->f_dbg = f_dbg;
8049 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008050}
8051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008053 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008054 mbedtls_ssl_send_t *f_send,
8055 mbedtls_ssl_recv_t *f_recv,
8056 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008057{
8058 ssl->p_bio = p_bio;
8059 ssl->f_send = f_send;
8060 ssl->f_recv = f_recv;
8061 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008062}
8063
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008064#if defined(MBEDTLS_SSL_PROTO_DTLS)
8065void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8066{
8067 ssl->mtu = mtu;
8068}
8069#endif
8070
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008071void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008072{
8073 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008074}
8075
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008076void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8077 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008078 mbedtls_ssl_set_timer_t *f_set_timer,
8079 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008080{
8081 ssl->p_timer = p_timer;
8082 ssl->f_set_timer = f_set_timer;
8083 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008084
8085 /* Make sure we start with no timer running */
8086 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008087}
8088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008090void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008091 void *p_cache,
8092 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8093 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008094{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008095 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008096 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008097 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101#if defined(MBEDTLS_SSL_CLI_C)
8102int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008103{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008104 int ret;
8105
8106 if( ssl == NULL ||
8107 session == NULL ||
8108 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008109 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008112 }
8113
Hanno Becker58fccf22019-02-06 14:30:46 +00008114 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8115 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008116 return( ret );
8117
Paul Bakker0a597072012-09-25 21:55:46 +00008118 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008119
8120 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008121}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008123
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008124void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008125 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008127 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8128 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8129 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8130 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008131}
8132
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008133void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008134 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008135 int major, int minor )
8136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008138 return;
8139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008141 return;
8142
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008143 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008144}
8145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008147void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008148 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008149{
8150 conf->cert_profile = profile;
8151}
8152
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008153/* Append a new keycert entry to a (possibly empty) list */
8154static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8155 mbedtls_x509_crt *cert,
8156 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008157{
niisato8ee24222018-06-25 19:05:48 +09008158 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008159
niisato8ee24222018-06-25 19:05:48 +09008160 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8161 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008162 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008163
niisato8ee24222018-06-25 19:05:48 +09008164 new_cert->cert = cert;
8165 new_cert->key = key;
8166 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008167
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008168 /* Update head is the list was null, else add to the end */
8169 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008170 {
niisato8ee24222018-06-25 19:05:48 +09008171 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008172 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008173 else
8174 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008175 mbedtls_ssl_key_cert *cur = *head;
8176 while( cur->next != NULL )
8177 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008178 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008179 }
8180
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008181 return( 0 );
8182}
8183
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008184int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008185 mbedtls_x509_crt *own_cert,
8186 mbedtls_pk_context *pk_key )
8187{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008188 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008189}
8190
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008191void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008192 mbedtls_x509_crt *ca_chain,
8193 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008194{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008195 conf->ca_chain = ca_chain;
8196 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008199
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008200#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8201int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8202 mbedtls_x509_crt *own_cert,
8203 mbedtls_pk_context *pk_key )
8204{
8205 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8206 own_cert, pk_key ) );
8207}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008208
8209void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8210 mbedtls_x509_crt *ca_chain,
8211 mbedtls_x509_crl *ca_crl )
8212{
8213 ssl->handshake->sni_ca_chain = ca_chain;
8214 ssl->handshake->sni_ca_crl = ca_crl;
8215}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008216
8217void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8218 int authmode )
8219{
8220 ssl->handshake->sni_authmode = authmode;
8221}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008222#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8223
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008224#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008225/*
8226 * Set EC J-PAKE password for current handshake
8227 */
8228int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8229 const unsigned char *pw,
8230 size_t pw_len )
8231{
8232 mbedtls_ecjpake_role role;
8233
Janos Follath8eb64132016-06-03 15:40:57 +01008234 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8236
8237 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8238 role = MBEDTLS_ECJPAKE_SERVER;
8239 else
8240 role = MBEDTLS_ECJPAKE_CLIENT;
8241
8242 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8243 role,
8244 MBEDTLS_MD_SHA256,
8245 MBEDTLS_ECP_DP_SECP256R1,
8246 pw, pw_len ) );
8247}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008248#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008251int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008252 const unsigned char *psk, size_t psk_len,
8253 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008254{
Paul Bakker6db455e2013-09-18 17:29:31 +02008255 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008260
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008261 /* Identity len will be encoded on two bytes */
8262 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008263 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008264 {
8265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8266 }
8267
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008268 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008269 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008270 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008271
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008272 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008273 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008274 conf->psk_len = 0;
8275 }
8276 if( conf->psk_identity != NULL )
8277 {
8278 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008279 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008280 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008281 }
8282
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008283 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8284 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008285 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008286 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008287 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008288 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008289 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008290 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008291 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008292
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008293 conf->psk_len = psk_len;
8294 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008295
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008296 memcpy( conf->psk, psk, conf->psk_len );
8297 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008298
8299 return( 0 );
8300}
8301
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008302int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8303 const unsigned char *psk, size_t psk_len )
8304{
8305 if( psk == NULL || ssl->handshake == NULL )
8306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8307
8308 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8310
8311 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008312 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008313 mbedtls_platform_zeroize( ssl->handshake->psk,
8314 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008315 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008316 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008317 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008318
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008319 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008320 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008321
8322 ssl->handshake->psk_len = psk_len;
8323 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8324
8325 return( 0 );
8326}
8327
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008328void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008330 size_t),
8331 void *p_psk )
8332{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008333 conf->f_psk = f_psk;
8334 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008337
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008338#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008339
8340#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008341int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008342{
8343 int ret;
8344
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008345 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8346 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8347 {
8348 mbedtls_mpi_free( &conf->dhm_P );
8349 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008350 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008351 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008352
8353 return( 0 );
8354}
Hanno Becker470a8c42017-10-04 15:28:46 +01008355#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008356
Hanno Beckera90658f2017-10-04 15:29:08 +01008357int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8358 const unsigned char *dhm_P, size_t P_len,
8359 const unsigned char *dhm_G, size_t G_len )
8360{
8361 int ret;
8362
8363 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8364 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8365 {
8366 mbedtls_mpi_free( &conf->dhm_P );
8367 mbedtls_mpi_free( &conf->dhm_G );
8368 return( ret );
8369 }
8370
8371 return( 0 );
8372}
Paul Bakker5121ce52009-01-03 21:22:43 +00008373
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008374int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008375{
8376 int ret;
8377
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008378 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8379 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8380 {
8381 mbedtls_mpi_free( &conf->dhm_P );
8382 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008383 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008384 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008385
8386 return( 0 );
8387}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008388#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008389
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008390#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8391/*
8392 * Set the minimum length for Diffie-Hellman parameters
8393 */
8394void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8395 unsigned int bitlen )
8396{
8397 conf->dhm_min_bitlen = bitlen;
8398}
8399#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8400
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008401#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008402/*
8403 * Set allowed/preferred hashes for handshake signatures
8404 */
8405void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8406 const int *hashes )
8407{
8408 conf->sig_hashes = hashes;
8409}
Hanno Becker947194e2017-04-07 13:25:49 +01008410#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008411
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008412#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008413/*
8414 * Set the allowed elliptic curves
8415 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008416void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008417 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008418{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008419 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008420}
Hanno Becker947194e2017-04-07 13:25:49 +01008421#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008422
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008423#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008425{
Hanno Becker947194e2017-04-07 13:25:49 +01008426 /* Initialize to suppress unnecessary compiler warning */
8427 size_t hostname_len = 0;
8428
8429 /* Check if new hostname is valid before
8430 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008431 if( hostname != NULL )
8432 {
8433 hostname_len = strlen( hostname );
8434
8435 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8437 }
8438
8439 /* Now it's clear that we will overwrite the old hostname,
8440 * so we can free it safely */
8441
8442 if( ssl->hostname != NULL )
8443 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008444 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008445 mbedtls_free( ssl->hostname );
8446 }
8447
8448 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008449
Paul Bakker5121ce52009-01-03 21:22:43 +00008450 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008451 {
8452 ssl->hostname = NULL;
8453 }
8454 else
8455 {
8456 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008457 if( ssl->hostname == NULL )
8458 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008459
Hanno Becker947194e2017-04-07 13:25:49 +01008460 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008461
Hanno Becker947194e2017-04-07 13:25:49 +01008462 ssl->hostname[hostname_len] = '\0';
8463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008464
8465 return( 0 );
8466}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008467#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008468
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008469#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008470void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008471 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008472 const unsigned char *, size_t),
8473 void *p_sni )
8474{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008475 conf->f_sni = f_sni;
8476 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008480#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008481int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008482{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008483 size_t cur_len, tot_len;
8484 const char **p;
8485
8486 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008487 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8488 * MUST NOT be truncated."
8489 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008490 */
8491 tot_len = 0;
8492 for( p = protos; *p != NULL; p++ )
8493 {
8494 cur_len = strlen( *p );
8495 tot_len += cur_len;
8496
8497 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008499 }
8500
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008501 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008502
8503 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008504}
8505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008507{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008508 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008511
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008512void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008513{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008514 conf->max_major_ver = major;
8515 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008516}
8517
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008518void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008519{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008520 conf->min_major_ver = major;
8521 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008522}
8523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008525void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008526{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008527 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008528}
8529#endif
8530
Janos Follath088ce432017-04-10 12:42:31 +01008531#if defined(MBEDTLS_SSL_SRV_C)
8532void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8533 char cert_req_ca_list )
8534{
8535 conf->cert_req_ca_list = cert_req_ca_list;
8536}
8537#endif
8538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008540void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008541{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008542 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008543}
8544#endif
8545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008546#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008547void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008549 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008550}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008551
8552void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008553 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008554{
8555 conf->enforce_extended_master_secret = ems_enf;
8556}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008557#endif
8558
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008559#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008560void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008561{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008562 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008563}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008564#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008567int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008570 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008573 }
8574
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008575 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008576
8577 return( 0 );
8578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008582void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008583{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008584 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008585}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008589void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008590{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008591 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008592}
8593#endif
8594
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008595void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008596{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008597 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008598}
8599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008601void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008602{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008603 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008604}
8605
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008606void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008608 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008609}
8610
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008611void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008612 const unsigned char period[8] )
8613{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008614 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008619#if defined(MBEDTLS_SSL_CLI_C)
8620void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008621{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008622 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008623}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008624#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008625
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008626#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008627void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8628 mbedtls_ssl_ticket_write_t *f_ticket_write,
8629 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8630 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008631{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008632 conf->f_ticket_write = f_ticket_write;
8633 conf->f_ticket_parse = f_ticket_parse;
8634 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008635}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008636#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008638
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008639#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8640void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8641 mbedtls_ssl_export_keys_t *f_export_keys,
8642 void *p_export_keys )
8643{
8644 conf->f_export_keys = f_export_keys;
8645 conf->p_export_keys = p_export_keys;
8646}
8647#endif
8648
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008649#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008650void mbedtls_ssl_conf_async_private_cb(
8651 mbedtls_ssl_config *conf,
8652 mbedtls_ssl_async_sign_t *f_async_sign,
8653 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8654 mbedtls_ssl_async_resume_t *f_async_resume,
8655 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008656 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008657{
8658 conf->f_async_sign_start = f_async_sign;
8659 conf->f_async_decrypt_start = f_async_decrypt;
8660 conf->f_async_resume = f_async_resume;
8661 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008662 conf->p_async_config_data = async_config_data;
8663}
8664
Gilles Peskine8f97af72018-04-26 11:46:10 +02008665void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8666{
8667 return( conf->p_async_config_data );
8668}
8669
Gilles Peskine1febfef2018-04-30 11:54:39 +02008670void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008671{
8672 if( ssl->handshake == NULL )
8673 return( NULL );
8674 else
8675 return( ssl->handshake->user_async_ctx );
8676}
8677
Gilles Peskine1febfef2018-04-30 11:54:39 +02008678void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008679 void *ctx )
8680{
8681 if( ssl->handshake != NULL )
8682 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008683}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008684#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008685
Paul Bakker5121ce52009-01-03 21:22:43 +00008686/*
8687 * SSL get accessors
8688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008690{
8691 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8692}
8693
Hanno Becker8b170a02017-10-10 11:51:19 +01008694int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8695{
8696 /*
8697 * Case A: We're currently holding back
8698 * a message for further processing.
8699 */
8700
8701 if( ssl->keep_current_message == 1 )
8702 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008704 return( 1 );
8705 }
8706
8707 /*
8708 * Case B: Further records are pending in the current datagram.
8709 */
8710
8711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008712 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008713 ssl->in_left > ssl->next_record_offset )
8714 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008716 return( 1 );
8717 }
8718#endif /* MBEDTLS_SSL_PROTO_DTLS */
8719
8720 /*
8721 * Case C: A handshake message is being processed.
8722 */
8723
Hanno Becker8b170a02017-10-10 11:51:19 +01008724 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8725 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008726 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008727 return( 1 );
8728 }
8729
8730 /*
8731 * Case D: An application data message is being processed
8732 */
8733 if( ssl->in_offt != NULL )
8734 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008736 return( 1 );
8737 }
8738
8739 /*
8740 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008741 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008742 * we implement support for multiple alerts in single records.
8743 */
8744
8745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8746 return( 0 );
8747}
8748
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008749uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008750{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008751 if( ssl->session != NULL )
8752 return( ssl->session->verify_result );
8753
8754 if( ssl->session_negotiate != NULL )
8755 return( ssl->session_negotiate->verify_result );
8756
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008757 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008758}
8759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008761{
Paul Bakker926c8e42013-03-06 10:23:34 +01008762 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008763 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008766}
8767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008769{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008771 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008772 {
8773 switch( ssl->minor_ver )
8774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008776 return( "DTLSv1.0" );
8777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008779 return( "DTLSv1.2" );
8780
8781 default:
8782 return( "unknown (DTLS)" );
8783 }
8784 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008785 MBEDTLS_SSL_TRANSPORT_ELSE
8786#endif /* MBEDTLS_SSL_PROTO_DTLS */
8787#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008788 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008789 switch( ssl->minor_ver )
8790 {
8791 case MBEDTLS_SSL_MINOR_VERSION_0:
8792 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008793
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008794 case MBEDTLS_SSL_MINOR_VERSION_1:
8795 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008796
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008797 case MBEDTLS_SSL_MINOR_VERSION_2:
8798 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008799
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008800 case MBEDTLS_SSL_MINOR_VERSION_3:
8801 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008802
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008803 default:
8804 return( "unknown" );
8805 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008806 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008807#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008808}
8809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008811{
Hanno Becker3136ede2018-08-17 15:28:19 +01008812 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008814 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008815
Hanno Becker43395762019-05-03 14:46:38 +01008816 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8817
Hanno Becker78640902018-08-13 16:35:15 +01008818 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008819 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821#if defined(MBEDTLS_ZLIB_SUPPORT)
8822 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8823 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008824#endif
8825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008826 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828 case MBEDTLS_MODE_GCM:
8829 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008830 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008832 transform_expansion = transform->minlen;
8833 break;
8834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008836
8837 block_size = mbedtls_cipher_get_block_size(
8838 &transform->cipher_ctx_enc );
8839
Hanno Becker3136ede2018-08-17 15:28:19 +01008840 /* Expansion due to the addition of the MAC. */
8841 transform_expansion += transform->maclen;
8842
8843 /* Expansion due to the addition of CBC padding;
8844 * Theoretically up to 256 bytes, but we never use
8845 * more than the block size of the underlying cipher. */
8846 transform_expansion += block_size;
8847
8848 /* For TLS 1.1 or higher, an explicit IV is added
8849 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008850#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8851 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008852 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008853#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008854
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008855 break;
8856
8857 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008860 }
8861
Hanno Beckera5a2b082019-05-15 14:03:01 +01008862#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008863 if( transform->out_cid_len != 0 )
8864 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008865#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008866
Hanno Becker43395762019-05-03 14:46:38 +01008867 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008868}
8869
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008870#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8871size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8872{
8873 size_t max_len;
8874
8875 /*
8876 * Assume mfl_code is correct since it was checked when set
8877 */
Angus Grattond8213d02016-05-25 20:56:48 +10008878 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008879
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008880 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008881 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008882 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008883 {
Angus Grattond8213d02016-05-25 20:56:48 +10008884 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008885 }
8886
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008887 /* During a handshake, use the value being negotiated */
8888 if( ssl->session_negotiate != NULL &&
8889 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8890 {
8891 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8892 }
8893
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008894 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008895}
8896#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8897
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008898#if defined(MBEDTLS_SSL_PROTO_DTLS)
8899static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8900{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008901 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8902 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8903 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8904 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8905 return ( 0 );
8906
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008907 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8908 return( ssl->mtu );
8909
8910 if( ssl->mtu == 0 )
8911 return( ssl->handshake->mtu );
8912
8913 return( ssl->mtu < ssl->handshake->mtu ?
8914 ssl->mtu : ssl->handshake->mtu );
8915}
8916#endif /* MBEDTLS_SSL_PROTO_DTLS */
8917
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008918int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8919{
8920 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8921
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008922#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8923 !defined(MBEDTLS_SSL_PROTO_DTLS)
8924 (void) ssl;
8925#endif
8926
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008927#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8928 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8929
8930 if( max_len > mfl )
8931 max_len = mfl;
8932#endif
8933
8934#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008935 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008936 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008937 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008938 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8939 const size_t overhead = (size_t) ret;
8940
8941 if( ret < 0 )
8942 return( ret );
8943
8944 if( mtu <= overhead )
8945 {
8946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8947 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8948 }
8949
8950 if( max_len > mtu - overhead )
8951 max_len = mtu - overhead;
8952 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008953#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008954
Hanno Becker0defedb2018-08-10 12:35:02 +01008955#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8956 !defined(MBEDTLS_SSL_PROTO_DTLS)
8957 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008958#endif
8959
8960 return( (int) max_len );
8961}
8962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963#if defined(MBEDTLS_X509_CRT_PARSE_C)
8964const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008965{
8966 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008967 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008968
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008969 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008970}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00008974int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8975 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008976{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008977 if( ssl == NULL ||
8978 dst == NULL ||
8979 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008980 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008983 }
8984
Hanno Becker58fccf22019-02-06 14:30:46 +00008985 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008988
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008989const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8990{
8991 if( ssl == NULL )
8992 return( NULL );
8993
8994 return( ssl->session );
8995}
8996
Paul Bakker5121ce52009-01-03 21:22:43 +00008997/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008998 * Define ticket header determining Mbed TLS version
8999 * and structure of the ticket.
9000 */
9001
Hanno Becker41527622019-05-16 12:50:45 +01009002/*
Hanno Becker26829e92019-05-28 14:30:45 +01009003 * Define bitflag determining compile-time settings influencing
9004 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009005 */
9006
Hanno Becker26829e92019-05-28 14:30:45 +01009007#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009008#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009009#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009010#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009011#endif /* MBEDTLS_HAVE_TIME */
9012
9013#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009014#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009015#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009016#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009017#endif /* MBEDTLS_X509_CRT_PARSE_C */
9018
9019#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009020#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009021#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009022#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009023#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9024
9025#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009026#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009027#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009028#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009029#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9030
9031#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009032#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009033#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009034#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009035#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9036
9037#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009038#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009039#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009040#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009041#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9042
Hanno Becker41527622019-05-16 12:50:45 +01009043#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9044#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9045#else
9046#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9047#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9048
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009049#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9050#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9051#else
9052#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9053#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9054
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009055#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9056#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9057#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9058#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9059#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9060#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9061#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009062#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009063
Hanno Becker26829e92019-05-28 14:30:45 +01009064#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009065 ( (uint16_t) ( \
9066 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9067 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9068 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9069 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9070 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9071 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009072 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9073 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009074
Hanno Becker557fe9f2019-05-16 12:41:07 +01009075static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009076 MBEDTLS_VERSION_MAJOR,
9077 MBEDTLS_VERSION_MINOR,
9078 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009079 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9080 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009081};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009082
9083/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009084 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009085 * (in the presentation language of TLS, RFC 8446 section 3)
9086 *
Hanno Becker26829e92019-05-28 14:30:45 +01009087 * opaque mbedtls_version[3]; // major, minor, patch
9088 * opaque session_format[2]; // version-specific 16-bit field determining
9089 * // the format of the remaining
9090 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009091 *
9092 * Note: When updating the format, remember to keep
9093 * these version+format bytes.
9094 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009095 * // In this version, `session_format` determines
9096 * // the setting of those compile-time
9097 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009098 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009099 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009100 * uint8 ciphersuite[2]; // defined by the standard
9101 * uint8 compression; // 0 or 1
9102 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009103 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009104 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009105 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01009106 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009107 * uint8_t peer_cert_digest_type;
9108 * opaque peer_cert_digest<0..2^8-1>;
Hanno Becker26829e92019-05-28 14:30:45 +01009109 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009110 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009111 * uint8 mfl_code; // up to 255 according to standard
9112 * uint8 trunc_hmac; // 0 or 1
9113 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009114 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009115 * The order is the same as in the definition of the structure, except
9116 * verify_result is put before peer_cert so that all mandatory fields come
9117 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009118 */
9119int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9120 unsigned char *buf,
9121 size_t buf_len,
9122 size_t *olen )
9123{
9124 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009125 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009126#if defined(MBEDTLS_HAVE_TIME)
9127 uint64_t start;
9128#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009129#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009130#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009131 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009132#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009133#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009134
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009135 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009136 * Add version identifier
9137 */
9138
9139 used += sizeof( ssl_serialized_session_header );
9140
9141 if( used <= buf_len )
9142 {
9143 memcpy( p, ssl_serialized_session_header,
9144 sizeof( ssl_serialized_session_header ) );
9145 p += sizeof( ssl_serialized_session_header );
9146 }
9147
9148 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009149 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009150 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009151#if defined(MBEDTLS_HAVE_TIME)
9152 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009153
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009154 if( used <= buf_len )
9155 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009156 start = (uint64_t) session->start;
9157
9158 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9159 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9160 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9161 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9162 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9163 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9164 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9165 *p++ = (unsigned char)( ( start ) & 0xFF );
9166 }
9167#endif /* MBEDTLS_HAVE_TIME */
9168
9169 /*
9170 * Basic mandatory fields
9171 */
9172 used += 2 /* ciphersuite */
9173 + 1 /* compression */
9174 + 1 /* id_len */
9175 + sizeof( session->id )
9176 + sizeof( session->master )
9177 + 4; /* verify_result */
9178
9179 if( used <= buf_len )
9180 {
9181 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9182 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9183
9184 *p++ = (unsigned char)( session->compression & 0xFF );
9185
9186 *p++ = (unsigned char)( session->id_len & 0xFF );
9187 memcpy( p, session->id, 32 );
9188 p += 32;
9189
9190 memcpy( p, session->master, 48 );
9191 p += 48;
9192
9193 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9194 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9195 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9196 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009197 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009198
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009199 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009200 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009201 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009202#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009203#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009204 if( session->peer_cert == NULL )
9205 cert_len = 0;
9206 else
9207 cert_len = session->peer_cert->raw.len;
9208
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009209 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009210
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009211 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009212 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009213 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9214 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9215 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9216
9217 if( session->peer_cert != NULL )
9218 {
9219 memcpy( p, session->peer_cert->raw.p, cert_len );
9220 p += cert_len;
9221 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009222 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009223
Hanno Becker2e6d3472019-02-06 15:40:27 +00009224#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009225 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009226 if( session->peer_cert_digest != NULL )
9227 {
9228 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9229 if( used <= buf_len )
9230 {
9231 *p++ = (unsigned char) session->peer_cert_digest_type;
9232 *p++ = (unsigned char) session->peer_cert_digest_len;
9233 memcpy( p, session->peer_cert_digest,
9234 session->peer_cert_digest_len );
9235 p += session->peer_cert_digest_len;
9236 }
9237 }
9238 else
9239 {
9240 used += 2;
9241 if( used <= buf_len )
9242 {
9243 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9244 *p++ = 0;
9245 }
9246 }
9247#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009248#endif /* MBEDTLS_X509_CRT_PARSE_C */
9249
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009250 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009251 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009252 */
9253#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009254 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009255
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009256 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009257 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009258 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9259 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9260 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9261
9262 if( session->ticket != NULL )
9263 {
9264 memcpy( p, session->ticket, session->ticket_len );
9265 p += session->ticket_len;
9266 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009267
9268 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9269 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9270 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9271 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009272 }
9273#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9274
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009275 /*
9276 * Misc extension-related info
9277 */
9278#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9279 used += 1;
9280
9281 if( used <= buf_len )
9282 *p++ = session->mfl_code;
9283#endif
9284
9285#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9286 used += 1;
9287
9288 if( used <= buf_len )
9289 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9290#endif
9291
9292#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9293 used += 1;
9294
9295 if( used <= buf_len )
9296 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9297#endif
9298
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009299 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009300 *olen = used;
9301
9302 if( used > buf_len )
9303 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009304
9305 return( 0 );
9306}
9307
9308/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009309 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009310 *
9311 * This internal version is wrapped by a public function that cleans up in
9312 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009313 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009314static int ssl_session_load( mbedtls_ssl_session *session,
9315 const unsigned char *buf,
9316 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009317{
9318 const unsigned char *p = buf;
9319 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009320#if defined(MBEDTLS_HAVE_TIME)
9321 uint64_t start;
9322#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009323#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009324#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009325 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009326#endif
9327#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009328
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009329 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009330 * Check version identifier
9331 */
9332
9333 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009335
9336 if( memcmp( p, ssl_serialized_session_header,
9337 sizeof( ssl_serialized_session_header ) ) != 0 )
9338 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009339 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009340 }
9341 p += sizeof( ssl_serialized_session_header );
9342
9343 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009344 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009345 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009346#if defined(MBEDTLS_HAVE_TIME)
9347 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9349
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009350 start = ( (uint64_t) p[0] << 56 ) |
9351 ( (uint64_t) p[1] << 48 ) |
9352 ( (uint64_t) p[2] << 40 ) |
9353 ( (uint64_t) p[3] << 32 ) |
9354 ( (uint64_t) p[4] << 24 ) |
9355 ( (uint64_t) p[5] << 16 ) |
9356 ( (uint64_t) p[6] << 8 ) |
9357 ( (uint64_t) p[7] );
9358 p += 8;
9359
9360 session->start = (time_t) start;
9361#endif /* MBEDTLS_HAVE_TIME */
9362
9363 /*
9364 * Basic mandatory fields
9365 */
9366 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9368
9369 session->ciphersuite = ( p[0] << 8 ) | p[1];
9370 p += 2;
9371
9372 session->compression = *p++;
9373
9374 session->id_len = *p++;
9375 memcpy( session->id, p, 32 );
9376 p += 32;
9377
9378 memcpy( session->master, p, 48 );
9379 p += 48;
9380
9381 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9382 ( (uint32_t) p[1] << 16 ) |
9383 ( (uint32_t) p[2] << 8 ) |
9384 ( (uint32_t) p[3] );
9385 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009386
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009387 /* Immediately clear invalid pointer values that have been read, in case
9388 * we exit early before we replaced them with valid ones. */
9389#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009390#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009391 session->peer_cert = NULL;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009392#else
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009393 session->peer_cert_digest = NULL;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009394#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009395#endif
9396#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9397 session->ticket = NULL;
9398#endif
9399
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009400 /*
9401 * Peer certificate
9402 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009403#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009404#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009405 if( 3 > (size_t)( end - p ) )
9406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9407
9408 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9409 p += 3;
9410
9411 if( cert_len == 0 )
9412 {
9413 session->peer_cert = NULL;
9414 }
9415 else
9416 {
9417 int ret;
9418
9419 if( cert_len > (size_t)( end - p ) )
9420 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9421
9422 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9423
9424 if( session->peer_cert == NULL )
9425 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9426
9427 mbedtls_x509_crt_init( session->peer_cert );
9428
9429 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9430 p, cert_len ) ) != 0 )
9431 {
9432 mbedtls_x509_crt_free( session->peer_cert );
9433 mbedtls_free( session->peer_cert );
9434 session->peer_cert = NULL;
9435 return( ret );
9436 }
9437
9438 p += cert_len;
9439 }
Hanno Becker2e6d3472019-02-06 15:40:27 +00009440#else /* defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009441 /* Deserialize CRT digest from the end of the ticket. */
9442 if( 2 > (size_t)( end - p ) )
9443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9444
9445 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9446 session->peer_cert_digest_len = (size_t) *p++;
9447
9448 if( session->peer_cert_digest_len != 0 )
9449 {
9450 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9452
9453 session->peer_cert_digest =
9454 mbedtls_calloc( 1, session->peer_cert_digest_len );
9455 if( session->peer_cert_digest == NULL )
9456 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9457
9458 memcpy( session->peer_cert_digest, p,
9459 session->peer_cert_digest_len );
9460 p += session->peer_cert_digest_len;
9461 }
9462#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009463#endif /* MBEDTLS_X509_CRT_PARSE_C */
9464
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009465 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009466 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009467 */
9468#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9469 if( 3 > (size_t)( end - p ) )
9470 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9471
9472 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9473 p += 3;
9474
9475 if( session->ticket_len != 0 )
9476 {
9477 if( session->ticket_len > (size_t)( end - p ) )
9478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9479
9480 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9481 if( session->ticket == NULL )
9482 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9483
9484 memcpy( session->ticket, p, session->ticket_len );
9485 p += session->ticket_len;
9486 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009487
9488 if( 4 > (size_t)( end - p ) )
9489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9490
9491 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9492 ( (uint32_t) p[1] << 16 ) |
9493 ( (uint32_t) p[2] << 8 ) |
9494 ( (uint32_t) p[3] );
9495 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009496#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9497
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009498 /*
9499 * Misc extension-related info
9500 */
9501#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9502 if( 1 > (size_t)( end - p ) )
9503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9504
9505 session->mfl_code = *p++;
9506#endif
9507
9508#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9509 if( 1 > (size_t)( end - p ) )
9510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9511
9512 session->trunc_hmac = *p++;
9513#endif
9514
9515#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9516 if( 1 > (size_t)( end - p ) )
9517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9518
9519 session->encrypt_then_mac = *p++;
9520#endif
9521
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009522 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009523 if( p != end )
9524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9525
9526 return( 0 );
9527}
9528
9529/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009530 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009531 */
9532int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9533 const unsigned char *buf,
9534 size_t len )
9535{
9536 int ret = ssl_session_load( session, buf, len );
9537
9538 if( ret != 0 )
9539 mbedtls_ssl_session_free( session );
9540
9541 return( ret );
9542}
9543
9544/*
Paul Bakker1961b702013-01-25 14:49:24 +01009545 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009550
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009551 if( ssl == NULL || ssl->conf == NULL )
9552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009555 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009557#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009561#endif
9562
Paul Bakker1961b702013-01-25 14:49:24 +01009563 return( ret );
9564}
9565
9566/*
9567 * Perform the SSL handshake
9568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009570{
9571 int ret = 0;
9572
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009573 if( ssl == NULL || ssl->conf == NULL )
9574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009581
9582 if( ret != 0 )
9583 break;
9584 }
9585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009587
9588 return( ret );
9589}
9590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591#if defined(MBEDTLS_SSL_RENEGOTIATION)
9592#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009593/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009594 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009597{
9598 int ret;
9599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009601
9602 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9604 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009605
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009606 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009607 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009609 return( ret );
9610 }
9611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009613
9614 return( 0 );
9615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009617
9618/*
9619 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 * - any side: calling mbedtls_ssl_renegotiate(),
9621 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9622 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009623 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009624 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009628{
9629 int ret;
9630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009632
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009633 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9634 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009635
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009636 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9637 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009639 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009641 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009642 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009643 ssl->handshake->out_msg_seq = 1;
9644 else
9645 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009646 }
9647#endif
9648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9650 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009655 return( ret );
9656 }
9657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009659
9660 return( 0 );
9661}
9662
9663/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009664 * Renegotiate current connection on client,
9665 * or request renegotiation on server
9666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009668{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009670
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009671 if( ssl == NULL || ssl->conf == NULL )
9672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009675 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009676 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009682
9683 /* Did we already try/start sending HelloRequest? */
9684 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009686
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009687 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009692 /*
9693 * On client, either start the renegotiation process or,
9694 * if already in progress, continue the handshake
9695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009700
9701 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009704 return( ret );
9705 }
9706 }
9707 else
9708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009712 return( ret );
9713 }
9714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009716
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009717 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009718}
9719
9720/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009721 * Check record counters and renegotiate if they're above the limit.
9722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009724{
Andres AG2196c7f2016-12-15 17:01:16 +00009725 size_t ep_len = ssl_ep_len( ssl );
9726 int in_ctr_cmp;
9727 int out_ctr_cmp;
9728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9730 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009731 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009732 {
9733 return( 0 );
9734 }
9735
Andres AG2196c7f2016-12-15 17:01:16 +00009736 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9737 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009738 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009739 ssl->conf->renego_period + ep_len, 8 - ep_len );
9740
9741 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009742 {
9743 return( 0 );
9744 }
9745
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009750
9751/*
9752 * Receive application data decrypted from the SSL layer
9753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009755{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009756 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009757 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009758
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009759 if( ssl == NULL || ssl->conf == NULL )
9760 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009765 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009768 return( ret );
9769
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009770 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009772 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009773 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009774 return( ret );
9775 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009776 }
9777#endif
9778
Hanno Becker4a810fb2017-05-24 16:27:30 +01009779 /*
9780 * Check if renegotiation is necessary and/or handshake is
9781 * in process. If yes, perform/continue, and fall through
9782 * if an unexpected packet is received while the client
9783 * is waiting for the ServerHello.
9784 *
9785 * (There is no equivalent to the last condition on
9786 * the server-side as it is not treated as within
9787 * a handshake while waiting for the ClientHello
9788 * after a renegotiation request.)
9789 */
9790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009792 ret = ssl_check_ctr_renegotiate( ssl );
9793 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9794 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009797 return( ret );
9798 }
9799#endif
9800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009804 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9805 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009808 return( ret );
9809 }
9810 }
9811
Hanno Beckere41158b2017-10-23 13:30:32 +01009812 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009813 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009814 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009815 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009816 if( ssl->f_get_timer != NULL &&
9817 ssl->f_get_timer( ssl->p_timer ) == -1 )
9818 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009819 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009820 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009821
Hanno Becker327c93b2018-08-15 13:56:18 +01009822 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009823 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009824 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9825 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009826
Hanno Becker4a810fb2017-05-24 16:27:30 +01009827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9828 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009829 }
9830
9831 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009833 {
9834 /*
9835 * OpenSSL sends empty messages to randomize the IV
9836 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009837 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009840 return( 0 );
9841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009843 return( ret );
9844 }
9845 }
9846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009850
Hanno Becker4a810fb2017-05-24 16:27:30 +01009851 /*
9852 * - For client-side, expect SERVER_HELLO_REQUEST.
9853 * - For server-side, expect CLIENT_HELLO.
9854 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9855 */
9856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009858 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009860 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009863
9864 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009866 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009867 {
9868 continue;
9869 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009870 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009871#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009872#if defined(MBEDTLS_SSL_PROTO_TLS)
9873 {
9874 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9875 }
9876#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009877 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009878#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009879
Hanno Becker4a810fb2017-05-24 16:27:30 +01009880#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009881 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009882 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009885
9886 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009888 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009889 {
9890 continue;
9891 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009892 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009893#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009894#if defined(MBEDTLS_SSL_PROTO_TLS)
9895 {
9896 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9897 }
9898#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009899 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009900#endif /* MBEDTLS_SSL_SRV_C */
9901
Hanno Becker21df7f92017-10-17 11:03:26 +01009902#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009903 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009904 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9905 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9906 ssl->conf->allow_legacy_renegotiation ==
9907 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9908 {
9909 /*
9910 * Accept renegotiation request
9911 */
Paul Bakker48916f92012-09-16 19:57:18 +00009912
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009913 /* DTLS clients need to know renego is server-initiated */
9914#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009915 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009916 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9917 {
9918 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9919 }
9920#endif
9921 ret = ssl_start_renegotiation( ssl );
9922 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9923 ret != 0 )
9924 {
9925 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9926 return( ret );
9927 }
9928 }
9929 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009930#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009931 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009932 /*
9933 * Refuse renegotiation
9934 */
9935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938#if defined(MBEDTLS_SSL_PROTO_SSL3)
9939 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009940 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009941 /* SSLv3 does not have a "no_renegotiation" warning, so
9942 we send a fatal alert and abort the connection. */
9943 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9944 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9945 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009946 }
9947 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9949#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9950 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9951 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9954 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9955 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009956 {
9957 return( ret );
9958 }
Paul Bakker48916f92012-09-16 19:57:18 +00009959 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009960 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9962 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009966 }
Paul Bakker48916f92012-09-16 19:57:18 +00009967 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009968
Hanno Becker90333da2017-10-10 11:27:13 +01009969 /* At this point, we don't know whether the renegotiation has been
9970 * completed or not. The cases to consider are the following:
9971 * 1) The renegotiation is complete. In this case, no new record
9972 * has been read yet.
9973 * 2) The renegotiation is incomplete because the client received
9974 * an application data record while awaiting the ServerHello.
9975 * 3) The renegotiation is incomplete because the client received
9976 * a non-handshake, non-application data message while awaiting
9977 * the ServerHello.
9978 * In each of these case, looping will be the proper action:
9979 * - For 1), the next iteration will read a new record and check
9980 * if it's application data.
9981 * - For 2), the loop condition isn't satisfied as application data
9982 * is present, hence continue is the same as break
9983 * - For 3), the loop condition is satisfied and read_record
9984 * will re-deliver the message that was held back by the client
9985 * when expecting the ServerHello.
9986 */
9987 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009988 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009989#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009991 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009992 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009993 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009994 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009997 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009999 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010000 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010001 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10005 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010008 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010009 }
10010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10014 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010015 }
10016
10017 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010018
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010019 /* We're going to return something now, cancel timer,
10020 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010022 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010023
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010025 /* If we requested renego but received AppData, resend HelloRequest.
10026 * Do it now, after setting in_offt, to avoid taking this branch
10027 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010029 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010031 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010032 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010035 return( ret );
10036 }
10037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010039#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010040 }
10041
10042 n = ( len < ssl->in_msglen )
10043 ? len : ssl->in_msglen;
10044
10045 memcpy( buf, ssl->in_offt, n );
10046 ssl->in_msglen -= n;
10047
10048 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010049 {
10050 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010051 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010052 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010053 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010054 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010055 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010056 /* more data available */
10057 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010058 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010061
Paul Bakker23986e52011-04-24 08:57:21 +000010062 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010063}
10064
10065/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010066 * Send application data to be encrypted by the SSL layer, taking care of max
10067 * fragment length and buffer size.
10068 *
10069 * According to RFC 5246 Section 6.2.1:
10070 *
10071 * Zero-length fragments of Application data MAY be sent as they are
10072 * potentially useful as a traffic analysis countermeasure.
10073 *
10074 * Therefore, it is possible that the input message length is 0 and the
10075 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010076 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010077static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010078 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010079{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010080 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10081 const size_t max_len = (size_t) ret;
10082
10083 if( ret < 0 )
10084 {
10085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10086 return( ret );
10087 }
10088
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010089 if( len > max_len )
10090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010092 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010095 "maximum fragment length: %d > %d",
10096 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010098 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010099 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010100#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010101#if defined(MBEDTLS_SSL_PROTO_TLS)
10102 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010103 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010104 }
10105#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010106 }
Paul Bakker887bd502011-06-08 13:10:54 +000010107
Paul Bakker5121ce52009-01-03 21:22:43 +000010108 if( ssl->out_left != 0 )
10109 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010110 /*
10111 * The user has previously tried to send the data and
10112 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10113 * written. In this case, we expect the high-level write function
10114 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010119 return( ret );
10120 }
10121 }
Paul Bakker887bd502011-06-08 13:10:54 +000010122 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010123 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010124 /*
10125 * The user is trying to send a message the first time, so we need to
10126 * copy the data into the internal buffers and setup the data structure
10127 * to keep track of partial writes
10128 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010129 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010131 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010132
Hanno Becker67bc7c32018-08-06 11:33:50 +010010133 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010135 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010136 return( ret );
10137 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010138 }
10139
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010140 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010141}
10142
10143/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010144 * Write application data, doing 1/n-1 splitting if necessary.
10145 *
10146 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010147 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010148 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010150#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010151static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010152 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010153{
10154 int ret;
10155
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010156 if( ssl->conf->cbc_record_splitting ==
10157 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010158 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10160 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10161 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010162 {
10163 return( ssl_write_real( ssl, buf, len ) );
10164 }
10165
10166 if( ssl->split_done == 0 )
10167 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010168 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010169 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010170 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010171 }
10172
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010173 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10174 return( ret );
10175 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010176
10177 return( ret + 1 );
10178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010180
10181/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010182 * Write application data (public-facing wrapper)
10183 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010184int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010185{
10186 int ret;
10187
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010189
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010190 if( ssl == NULL || ssl->conf == NULL )
10191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10192
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010193#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010194 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10195 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010196 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010197 return( ret );
10198 }
10199#endif
10200
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010201 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010202 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010203 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010204 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010206 return( ret );
10207 }
10208 }
10209
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010210#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010211 ret = ssl_write_split( ssl, buf, len );
10212#else
10213 ret = ssl_write_real( ssl, buf, len );
10214#endif
10215
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010217
10218 return( ret );
10219}
10220
10221/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010222 * Notify the peer that the connection is being closed
10223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010225{
10226 int ret;
10227
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010228 if( ssl == NULL || ssl->conf == NULL )
10229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010232
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010233 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010238 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10239 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10240 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010243 return( ret );
10244 }
10245 }
10246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010248
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010249 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010250}
10251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010253{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010254 if( transform == NULL )
10255 return;
10256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010258 deflateEnd( &transform->ctx_deflate );
10259 inflateEnd( &transform->ctx_inflate );
10260#endif
10261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010262 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10263 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010264
Hanno Becker92231322018-01-03 15:32:51 +000010265#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266 mbedtls_md_free( &transform->md_ctx_enc );
10267 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010268#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010269
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010270 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010271}
10272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010273#if defined(MBEDTLS_X509_CRT_PARSE_C)
10274static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010276 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010277
10278 while( cur != NULL )
10279 {
10280 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010281 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010282 cur = next;
10283 }
10284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010286
Hanno Becker0271f962018-08-16 13:23:47 +010010287#if defined(MBEDTLS_SSL_PROTO_DTLS)
10288
10289static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10290{
10291 unsigned offset;
10292 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10293
10294 if( hs == NULL )
10295 return;
10296
Hanno Becker283f5ef2018-08-24 09:34:47 +010010297 ssl_free_buffered_record( ssl );
10298
Hanno Becker0271f962018-08-16 13:23:47 +010010299 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010300 ssl_buffering_free_slot( ssl, offset );
10301}
10302
10303static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10304 uint8_t slot )
10305{
10306 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10307 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010308
10309 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10310 return;
10311
Hanno Beckere605b192018-08-21 15:59:07 +010010312 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010313 {
Hanno Beckere605b192018-08-21 15:59:07 +010010314 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010315 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010316 mbedtls_free( hs_buf->data );
10317 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010318 }
10319}
10320
10321#endif /* MBEDTLS_SSL_PROTO_DTLS */
10322
Gilles Peskine9b562d52018-04-25 20:32:43 +020010323void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010324{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010325 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10326
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010327 if( handshake == NULL )
10328 return;
10329
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010330#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10331 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10332 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010333 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010334 handshake->async_in_progress = 0;
10335 }
10336#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10337
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010338#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10339 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10340 mbedtls_md5_free( &handshake->fin_md5 );
10341 mbedtls_sha1_free( &handshake->fin_sha1 );
10342#endif
10343#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10344#if defined(MBEDTLS_SHA256_C)
10345 mbedtls_sha256_free( &handshake->fin_sha256 );
10346#endif
10347#if defined(MBEDTLS_SHA512_C)
10348 mbedtls_sha512_free( &handshake->fin_sha512 );
10349#endif
10350#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352#if defined(MBEDTLS_DHM_C)
10353 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010354#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355#if defined(MBEDTLS_ECDH_C)
10356 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010357#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010358#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010359 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010360#if defined(MBEDTLS_SSL_CLI_C)
10361 mbedtls_free( handshake->ecjpake_cache );
10362 handshake->ecjpake_cache = NULL;
10363 handshake->ecjpake_cache_len = 0;
10364#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010365#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010366
Janos Follath4ae5c292016-02-10 11:27:43 +000010367#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10368 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010369 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010371#endif
10372
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010373#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10374 if( handshake->psk != NULL )
10375 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010376 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010377 mbedtls_free( handshake->psk );
10378 }
10379#endif
10380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10382 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010383 /*
10384 * Free only the linked list wrapper, not the keys themselves
10385 * since the belong to the SNI callback
10386 */
10387 if( handshake->sni_key_cert != NULL )
10388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010389 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010390
10391 while( cur != NULL )
10392 {
10393 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010395 cur = next;
10396 }
10397 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010399
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010400#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010401 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010402 if( handshake->ecrs_peer_cert != NULL )
10403 {
10404 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10405 mbedtls_free( handshake->ecrs_peer_cert );
10406 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010407#endif
10408
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010409#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10410 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10411 mbedtls_pk_free( &handshake->peer_pubkey );
10412#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010414#if defined(MBEDTLS_SSL_PROTO_DTLS)
10415 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010416 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010417 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010418#endif
10419
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010420 mbedtls_platform_zeroize( handshake,
10421 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010422}
10423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010425{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010426 if( session == NULL )
10427 return;
10428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010430 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010431#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010432
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010433#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010435#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010436
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010437 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010438}
10439
Paul Bakker5121ce52009-01-03 21:22:43 +000010440/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010441 * Serialize a full SSL context
10442 */
10443int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10444 unsigned char *buf,
10445 size_t buf_len,
10446 size_t *olen )
10447{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010448 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010449 (void) ssl;
10450
10451 if( buf != NULL )
10452 memset( buf, 0, buf_len );
10453
10454 *olen = 0;
10455
10456 return( 0 );
10457}
10458
10459/*
10460 * Deserialize a full SSL context
10461 */
10462int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10463 const unsigned char *buf,
10464 size_t len )
10465{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010466 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010467 (void) ssl;
10468 (void) buf;
10469 (void) len;
10470
10471 return( 0 );
10472}
10473
10474/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010475 * Free an SSL context
10476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010477void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010478{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010479 if( ssl == NULL )
10480 return;
10481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010483
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010484 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010485 {
Angus Grattond8213d02016-05-25 20:56:48 +100010486 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010488 }
10489
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010490 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010491 {
Angus Grattond8213d02016-05-25 20:56:48 +100010492 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010493 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010494 }
10495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010497 if( ssl->compress_buf != NULL )
10498 {
Angus Grattond8213d02016-05-25 20:56:48 +100010499 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010501 }
10502#endif
10503
Paul Bakker48916f92012-09-16 19:57:18 +000010504 if( ssl->transform )
10505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010506 mbedtls_ssl_transform_free( ssl->transform );
10507 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010508 }
10509
10510 if( ssl->handshake )
10511 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010512 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010513 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10514 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516 mbedtls_free( ssl->handshake );
10517 mbedtls_free( ssl->transform_negotiate );
10518 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010519 }
10520
Paul Bakkerc0463502013-02-14 11:19:38 +010010521 if( ssl->session )
10522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010523 mbedtls_ssl_session_free( ssl->session );
10524 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010525 }
10526
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010527#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010528 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010529 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010530 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010531 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010532 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010533#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010535#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10536 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10539 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010540 }
10541#endif
10542
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010543#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010544 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010545#endif
10546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010548
Paul Bakker86f04f42013-02-14 11:20:09 +010010549 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010550 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010551}
10552
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010553/*
10554 * Initialze mbedtls_ssl_config
10555 */
10556void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10557{
10558 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010559
10560#if !defined(MBEDTLS_SSL_PROTO_TLS)
10561 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10562#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010563}
10564
Simon Butcherc97b6972015-12-27 23:48:17 +000010565#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010566static int ssl_preset_default_hashes[] = {
10567#if defined(MBEDTLS_SHA512_C)
10568 MBEDTLS_MD_SHA512,
10569 MBEDTLS_MD_SHA384,
10570#endif
10571#if defined(MBEDTLS_SHA256_C)
10572 MBEDTLS_MD_SHA256,
10573 MBEDTLS_MD_SHA224,
10574#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010575#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010576 MBEDTLS_MD_SHA1,
10577#endif
10578 MBEDTLS_MD_NONE
10579};
Simon Butcherc97b6972015-12-27 23:48:17 +000010580#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010581
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010582static int ssl_preset_suiteb_ciphersuites[] = {
10583 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10584 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10585 0
10586};
10587
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010588#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010589static int ssl_preset_suiteb_hashes[] = {
10590 MBEDTLS_MD_SHA256,
10591 MBEDTLS_MD_SHA384,
10592 MBEDTLS_MD_NONE
10593};
10594#endif
10595
10596#if defined(MBEDTLS_ECP_C)
10597static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10598 MBEDTLS_ECP_DP_SECP256R1,
10599 MBEDTLS_ECP_DP_SECP384R1,
10600 MBEDTLS_ECP_DP_NONE
10601};
10602#endif
10603
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010604/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010605 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010606 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010607int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010608 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010609{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010610#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010611 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010612#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010613
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010614 /* Use the functions here so that they are covered in tests,
10615 * but otherwise access member directly for efficiency */
10616 mbedtls_ssl_conf_endpoint( conf, endpoint );
10617 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010618
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010619 /*
10620 * Things that are common to all presets
10621 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010622#if defined(MBEDTLS_SSL_CLI_C)
10623 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10624 {
10625 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10626#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10627 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10628#endif
10629 }
10630#endif
10631
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010632#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010633 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010634#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010635
10636#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10637 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10638#endif
10639
10640#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10641 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010642 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010643 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010644#endif
10645
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010646#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10647 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10648#endif
10649
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010650#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010651 conf->f_cookie_write = ssl_cookie_write_dummy;
10652 conf->f_cookie_check = ssl_cookie_check_dummy;
10653#endif
10654
10655#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10656 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10657#endif
10658
Janos Follath088ce432017-04-10 12:42:31 +010010659#if defined(MBEDTLS_SSL_SRV_C)
10660 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10661#endif
10662
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010663#if defined(MBEDTLS_SSL_PROTO_DTLS)
10664 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10665 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10666#endif
10667
10668#if defined(MBEDTLS_SSL_RENEGOTIATION)
10669 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010670 memset( conf->renego_period, 0x00, 2 );
10671 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010672#endif
10673
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010674#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10675 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10676 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010677 const unsigned char dhm_p[] =
10678 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10679 const unsigned char dhm_g[] =
10680 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10681
Hanno Beckera90658f2017-10-04 15:29:08 +010010682 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10683 dhm_p, sizeof( dhm_p ),
10684 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010685 {
10686 return( ret );
10687 }
10688 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010689#endif
10690
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010691 /*
10692 * Preset-specific defaults
10693 */
10694 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010695 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010696 /*
10697 * NSA Suite B
10698 */
10699 case MBEDTLS_SSL_PRESET_SUITEB:
10700 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10701 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10702 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10703 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10704
10705 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10706 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10707 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10708 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10709 ssl_preset_suiteb_ciphersuites;
10710
10711#if defined(MBEDTLS_X509_CRT_PARSE_C)
10712 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010713#endif
10714
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010715#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010716 conf->sig_hashes = ssl_preset_suiteb_hashes;
10717#endif
10718
10719#if defined(MBEDTLS_ECP_C)
10720 conf->curve_list = ssl_preset_suiteb_curves;
10721#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010722 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010723
10724 /*
10725 * Default
10726 */
10727 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010728 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10729 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10730 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10731 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10732 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10733 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10734 MBEDTLS_SSL_MIN_MINOR_VERSION :
10735 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010736 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10737 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10738
10739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010740 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010741 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10742#endif
10743
10744 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10745 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10746 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10747 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10748 mbedtls_ssl_list_ciphersuites();
10749
10750#if defined(MBEDTLS_X509_CRT_PARSE_C)
10751 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10752#endif
10753
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010754#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010755 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010756#endif
10757
10758#if defined(MBEDTLS_ECP_C)
10759 conf->curve_list = mbedtls_ecp_grp_id_list();
10760#endif
10761
10762#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10763 conf->dhm_min_bitlen = 1024;
10764#endif
10765 }
10766
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010767 return( 0 );
10768}
10769
10770/*
10771 * Free mbedtls_ssl_config
10772 */
10773void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10774{
10775#if defined(MBEDTLS_DHM_C)
10776 mbedtls_mpi_free( &conf->dhm_P );
10777 mbedtls_mpi_free( &conf->dhm_G );
10778#endif
10779
10780#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10781 if( conf->psk != NULL )
10782 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010783 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010784 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010785 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010786 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010787 }
10788
10789 if( conf->psk_identity != NULL )
10790 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010791 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010792 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010793 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010794 conf->psk_identity_len = 0;
10795 }
10796#endif
10797
10798#if defined(MBEDTLS_X509_CRT_PARSE_C)
10799 ssl_key_cert_free( conf->key_cert );
10800#endif
10801
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010802 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010803}
10804
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010805#if defined(MBEDTLS_PK_C) && \
10806 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010807/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010808 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010810unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010811{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010812#if defined(MBEDTLS_RSA_C)
10813 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10814 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010815#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816#if defined(MBEDTLS_ECDSA_C)
10817 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10818 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010819#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010820 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010821}
10822
Hanno Becker7e5437a2017-04-28 17:15:26 +010010823unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10824{
10825 switch( type ) {
10826 case MBEDTLS_PK_RSA:
10827 return( MBEDTLS_SSL_SIG_RSA );
10828 case MBEDTLS_PK_ECDSA:
10829 case MBEDTLS_PK_ECKEY:
10830 return( MBEDTLS_SSL_SIG_ECDSA );
10831 default:
10832 return( MBEDTLS_SSL_SIG_ANON );
10833 }
10834}
10835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010837{
10838 switch( sig )
10839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010840#if defined(MBEDTLS_RSA_C)
10841 case MBEDTLS_SSL_SIG_RSA:
10842 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010843#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010844#if defined(MBEDTLS_ECDSA_C)
10845 case MBEDTLS_SSL_SIG_ECDSA:
10846 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010847#endif
10848 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010850 }
10851}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010852#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010853
Hanno Becker7e5437a2017-04-28 17:15:26 +010010854#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10855 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10856
10857/* Find an entry in a signature-hash set matching a given hash algorithm. */
10858mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10859 mbedtls_pk_type_t sig_alg )
10860{
10861 switch( sig_alg )
10862 {
10863 case MBEDTLS_PK_RSA:
10864 return( set->rsa );
10865 case MBEDTLS_PK_ECDSA:
10866 return( set->ecdsa );
10867 default:
10868 return( MBEDTLS_MD_NONE );
10869 }
10870}
10871
10872/* Add a signature-hash-pair to a signature-hash set */
10873void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10874 mbedtls_pk_type_t sig_alg,
10875 mbedtls_md_type_t md_alg )
10876{
10877 switch( sig_alg )
10878 {
10879 case MBEDTLS_PK_RSA:
10880 if( set->rsa == MBEDTLS_MD_NONE )
10881 set->rsa = md_alg;
10882 break;
10883
10884 case MBEDTLS_PK_ECDSA:
10885 if( set->ecdsa == MBEDTLS_MD_NONE )
10886 set->ecdsa = md_alg;
10887 break;
10888
10889 default:
10890 break;
10891 }
10892}
10893
10894/* Allow exactly one hash algorithm for each signature. */
10895void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10896 mbedtls_md_type_t md_alg )
10897{
10898 set->rsa = md_alg;
10899 set->ecdsa = md_alg;
10900}
10901
10902#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10903 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10904
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010905/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010906 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010909{
10910 switch( hash )
10911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912#if defined(MBEDTLS_MD5_C)
10913 case MBEDTLS_SSL_HASH_MD5:
10914 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010915#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916#if defined(MBEDTLS_SHA1_C)
10917 case MBEDTLS_SSL_HASH_SHA1:
10918 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010920#if defined(MBEDTLS_SHA256_C)
10921 case MBEDTLS_SSL_HASH_SHA224:
10922 return( MBEDTLS_MD_SHA224 );
10923 case MBEDTLS_SSL_HASH_SHA256:
10924 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010925#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926#if defined(MBEDTLS_SHA512_C)
10927 case MBEDTLS_SSL_HASH_SHA384:
10928 return( MBEDTLS_MD_SHA384 );
10929 case MBEDTLS_SSL_HASH_SHA512:
10930 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010931#endif
10932 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010933 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010934 }
10935}
10936
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010937/*
10938 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10939 */
10940unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10941{
10942 switch( md )
10943 {
10944#if defined(MBEDTLS_MD5_C)
10945 case MBEDTLS_MD_MD5:
10946 return( MBEDTLS_SSL_HASH_MD5 );
10947#endif
10948#if defined(MBEDTLS_SHA1_C)
10949 case MBEDTLS_MD_SHA1:
10950 return( MBEDTLS_SSL_HASH_SHA1 );
10951#endif
10952#if defined(MBEDTLS_SHA256_C)
10953 case MBEDTLS_MD_SHA224:
10954 return( MBEDTLS_SSL_HASH_SHA224 );
10955 case MBEDTLS_MD_SHA256:
10956 return( MBEDTLS_SSL_HASH_SHA256 );
10957#endif
10958#if defined(MBEDTLS_SHA512_C)
10959 case MBEDTLS_MD_SHA384:
10960 return( MBEDTLS_SSL_HASH_SHA384 );
10961 case MBEDTLS_MD_SHA512:
10962 return( MBEDTLS_SSL_HASH_SHA512 );
10963#endif
10964 default:
10965 return( MBEDTLS_SSL_HASH_NONE );
10966 }
10967}
10968
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010969#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010970/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010971 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010972 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010973 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010974int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010975{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010976 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010977
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010978 if( ssl->conf->curve_list == NULL )
10979 return( -1 );
10980
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010981 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010982 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010983 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010984
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010985 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010986}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010987#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010988
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010989#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010990/*
10991 * Check if a hash proposed by the peer is in our list.
10992 * Return 0 if we're willing to use it, -1 otherwise.
10993 */
10994int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10995 mbedtls_md_type_t md )
10996{
10997 const int *cur;
10998
10999 if( ssl->conf->sig_hashes == NULL )
11000 return( -1 );
11001
11002 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11003 if( *cur == (int) md )
11004 return( 0 );
11005
11006 return( -1 );
11007}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011008#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011010#if defined(MBEDTLS_X509_CRT_PARSE_C)
11011int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11012 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011013 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011014 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011015{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011016 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011018 int usage = 0;
11019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011021 const char *ext_oid;
11022 size_t ext_len;
11023#endif
11024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11026 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011027 ((void) cert);
11028 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011029 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011030#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11033 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011034 {
11035 /* Server part of the key exchange */
11036 switch( ciphersuite->key_exchange )
11037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038 case MBEDTLS_KEY_EXCHANGE_RSA:
11039 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011040 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011041 break;
11042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11044 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11045 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11046 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011047 break;
11048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11050 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011051 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011052 break;
11053
11054 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011055 case MBEDTLS_KEY_EXCHANGE_NONE:
11056 case MBEDTLS_KEY_EXCHANGE_PSK:
11057 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11058 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011059 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011060 usage = 0;
11061 }
11062 }
11063 else
11064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011065 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11066 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011067 }
11068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011069 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011070 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011071 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011072 ret = -1;
11073 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011074#else
11075 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011078#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11079 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011081 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11082 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011083 }
11084 else
11085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011086 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11087 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011088 }
11089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011090 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011091 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011092 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011093 ret = -1;
11094 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011095#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011096
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011097 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011099#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011100
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011101/*
11102 * Convert version numbers to/from wire format
11103 * and, for DTLS, to/from TLS equivalent.
11104 *
11105 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011106 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011107 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11108 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011110void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011111 unsigned char ver[2] )
11112{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011113#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11114 ((void) transport);
11115#endif
11116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011118 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011120 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011121 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11122
11123 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11124 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11125 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011126 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011127#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011128#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011129 {
11130 ver[0] = (unsigned char) major;
11131 ver[1] = (unsigned char) minor;
11132 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011133#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011134}
11135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011136void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011137 const unsigned char ver[2] )
11138{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011139#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11140 ((void) transport);
11141#endif
11142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011144 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011145 {
11146 *major = 255 - ver[0] + 2;
11147 *minor = 255 - ver[1] + 1;
11148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011149 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011150 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11151 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011152 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011153#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011154#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011155 {
11156 *major = ver[0];
11157 *minor = ver[1];
11158 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011159#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011160}
11161
Simon Butcher99000142016-10-13 17:21:01 +010011162int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11163{
11164#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11165 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11166 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11167
11168 switch( md )
11169 {
11170#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11171#if defined(MBEDTLS_MD5_C)
11172 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011173 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011174#endif
11175#if defined(MBEDTLS_SHA1_C)
11176 case MBEDTLS_SSL_HASH_SHA1:
11177 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11178 break;
11179#endif
11180#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11181#if defined(MBEDTLS_SHA512_C)
11182 case MBEDTLS_SSL_HASH_SHA384:
11183 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11184 break;
11185#endif
11186#if defined(MBEDTLS_SHA256_C)
11187 case MBEDTLS_SSL_HASH_SHA256:
11188 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11189 break;
11190#endif
11191 default:
11192 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11193 }
11194
11195 return 0;
11196#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11197 (void) ssl;
11198 (void) md;
11199
11200 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11201#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11202}
11203
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011204#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11205 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11206int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11207 unsigned char *output,
11208 unsigned char *data, size_t data_len )
11209{
11210 int ret = 0;
11211 mbedtls_md5_context mbedtls_md5;
11212 mbedtls_sha1_context mbedtls_sha1;
11213
11214 mbedtls_md5_init( &mbedtls_md5 );
11215 mbedtls_sha1_init( &mbedtls_sha1 );
11216
11217 /*
11218 * digitally-signed struct {
11219 * opaque md5_hash[16];
11220 * opaque sha_hash[20];
11221 * };
11222 *
11223 * md5_hash
11224 * MD5(ClientHello.random + ServerHello.random
11225 * + ServerParams);
11226 * sha_hash
11227 * SHA(ClientHello.random + ServerHello.random
11228 * + ServerParams);
11229 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011230 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011231 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011233 goto exit;
11234 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011235 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011236 ssl->handshake->randbytes, 64 ) ) != 0 )
11237 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011239 goto exit;
11240 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011241 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011242 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011244 goto exit;
11245 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011246 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011247 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011249 goto exit;
11250 }
11251
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011252 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011253 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011255 goto exit;
11256 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011257 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011258 ssl->handshake->randbytes, 64 ) ) != 0 )
11259 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011261 goto exit;
11262 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011263 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011264 data_len ) ) != 0 )
11265 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011267 goto exit;
11268 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011269 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011270 output + 16 ) ) != 0 )
11271 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011273 goto exit;
11274 }
11275
11276exit:
11277 mbedtls_md5_free( &mbedtls_md5 );
11278 mbedtls_sha1_free( &mbedtls_sha1 );
11279
11280 if( ret != 0 )
11281 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11282 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11283
11284 return( ret );
11285
11286}
11287#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11288 MBEDTLS_SSL_PROTO_TLS1_1 */
11289
11290#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11291 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11292int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011293 unsigned char *hash, size_t *hashlen,
11294 unsigned char *data, size_t data_len,
11295 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011296{
11297 int ret = 0;
11298 mbedtls_md_context_t ctx;
11299 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011300 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011301
11302 mbedtls_md_init( &ctx );
11303
11304 /*
11305 * digitally-signed struct {
11306 * opaque client_random[32];
11307 * opaque server_random[32];
11308 * ServerDHParams params;
11309 * };
11310 */
11311 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11312 {
11313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11314 goto exit;
11315 }
11316 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11317 {
11318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11319 goto exit;
11320 }
11321 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11322 {
11323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11324 goto exit;
11325 }
11326 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11327 {
11328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11329 goto exit;
11330 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011331 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011332 {
11333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11334 goto exit;
11335 }
11336
11337exit:
11338 mbedtls_md_free( &ctx );
11339
11340 if( ret != 0 )
11341 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11342 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11343
11344 return( ret );
11345}
11346#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11347 MBEDTLS_SSL_PROTO_TLS1_2 */
11348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011349#endif /* MBEDTLS_SSL_TLS_C */