blob: 560ef4c07e841ef141679ea5ddb839c41e728530 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Hanno Becker58fccf22019-02-06 14:30:46 +0000371int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
372 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000378
379#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380 if( src->peer_cert != NULL )
381 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200382 int ret;
383
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200384 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200391 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 dst->peer_cert = NULL;
395 return( ret );
396 }
397 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100398#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000399 if( src->peer_cert_digest != NULL )
400 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000401 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000402 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000409 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000410 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200414
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200415#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200416 if( src->ticket != NULL )
417 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200418 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200419 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200421
422 memcpy( dst->ticket, src->ticket, src->ticket_len );
423 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200424#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200425
426 return( 0 );
427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
430int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200431 const unsigned char *key_enc, const unsigned char *key_dec,
432 size_t keylen,
433 const unsigned char *iv_enc, const unsigned char *iv_dec,
434 size_t ivlen,
435 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200436 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
438int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
441int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
442#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444/*
445 * Key material generation
446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200448static int ssl3_prf( const unsigned char *secret, size_t slen,
449 const char *label,
450 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000451 unsigned char *dstbuf, size_t dlen )
452{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100453 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000454 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200455 mbedtls_md5_context md5;
456 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000457 unsigned char padding[16];
458 unsigned char sha1sum[20];
459 ((void)label);
460
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200461 mbedtls_md5_init( &md5 );
462 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200463
Paul Bakker5f70b252012-09-13 14:23:06 +0000464 /*
465 * SSLv3:
466 * block =
467 * MD5( secret + SHA1( 'A' + secret + random ) ) +
468 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
469 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
470 * ...
471 */
472 for( i = 0; i < dlen / 16; i++ )
473 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200474 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000475
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100478 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100479 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100480 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100482 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100484 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100485 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100489 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100490 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000495 }
496
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200498 mbedtls_md5_free( &md5 );
499 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000500
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500501 mbedtls_platform_zeroize( padding, sizeof( padding ) );
502 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000503
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100504 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200509static int tls1_prf( const unsigned char *secret, size_t slen,
510 const char *label,
511 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000512 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000513{
Paul Bakker23986e52011-04-24 08:57:21 +0000514 size_t nb, hs;
515 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200516 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 unsigned char tmp[128];
518 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_md_info_t *md_info;
520 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100521 int ret;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 hs = ( slen + 1 ) / 2;
529 S1 = secret;
530 S2 = secret + slen - hs;
531
532 nb = strlen( label );
533 memcpy( tmp + 20, label, nb );
534 memcpy( tmp + 20 + nb, random, rlen );
535 nb += rlen;
536
537 /*
538 * First compute P_md5(secret,label+random)[0..dlen]
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544 return( ret );
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
547 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
548 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 for( i = 0; i < dlen; i += 16 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md_hmac_reset ( &md_ctx );
553 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
554 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_reset ( &md_ctx );
557 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
558 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
561
562 for( j = 0; j < k; j++ )
563 dstbuf[i + j] = h_i[j];
564 }
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 /*
569 * XOR out with P_sha1(secret,label+random)[0..dlen]
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100575 return( ret );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
578 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
579 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 for( i = 0; i < dlen; i += 20 )
582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_md_hmac_reset ( &md_ctx );
584 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
585 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_md_hmac_reset ( &md_ctx );
588 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
589 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
592
593 for( j = 0; j < k; j++ )
594 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
595 }
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500599 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
600 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602 return( 0 );
603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
607static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608 const unsigned char *secret, size_t slen,
609 const char *label,
610 const unsigned char *random, size_t rlen,
611 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612{
613 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100614 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
617 const mbedtls_md_info_t *md_info;
618 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 int ret;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627
628 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000630
631 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100632 memcpy( tmp + md_len, label, nb );
633 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634 nb += rlen;
635
636 /*
637 * Compute P_<hash>(secret, label + random)[0..dlen]
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100640 return( ret );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
643 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
644 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100645
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100646 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_md_hmac_reset ( &md_ctx );
649 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
650 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_md_hmac_reset ( &md_ctx );
653 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
654 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000655
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000657
658 for( j = 0; j < k; j++ )
659 dstbuf[i + j] = h_i[j];
660 }
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100663
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500664 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
665 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000666
667 return( 0 );
668}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100671static int tls_prf_sha256( const unsigned char *secret, size_t slen,
672 const char *label,
673 const unsigned char *random, size_t rlen,
674 unsigned char *dstbuf, size_t dlen )
675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100677 label, random, rlen, dstbuf, dlen ) );
678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200682static int tls_prf_sha384( const unsigned char *secret, size_t slen,
683 const char *label,
684 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000685 unsigned char *dstbuf, size_t dlen )
686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100688 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_SHA512_C */
691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
696 defined(MBEDTLS_SSL_PROTO_TLS1_1)
697static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200698#endif
Paul Bakker380da532012-04-18 16:10:25 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200701static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200703#endif
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200706static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200708#endif
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
711#if defined(MBEDTLS_SHA256_C)
712static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200713static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200715#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SHA512_C)
718static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200719static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000723
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200724/* Type for the TLS PRF */
725typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
726 const unsigned char *, size_t,
727 unsigned char *, size_t);
728
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200729/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200730 * Populate a transform structure with session keys and all the other
731 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200732 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200733 * Parameters:
734 * - [in/out]: transform: structure to populate
735 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200736 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200737 * - [in] ciphersuite
738 * - [in] master
739 * - [in] encrypt_then_mac
740 * - [in] trunc_hmac
741 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742 * - [in] tls_prf: pointer to PRF to use for key derivation
743 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200744 * - [in] minor_ver: SSL/TLS minor version
745 * - [in] endpoint: client or server
746 * - [in] ssl: optionally used for:
747 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
748 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
749 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200750 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200752 int ciphersuite,
753 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100754#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
756 int encrypt_then_mac,
757#endif
758#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
759 int trunc_hmac,
760#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100761#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200762#if defined(MBEDTLS_ZLIB_SUPPORT)
763 int compression,
764#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200765 ssl_tls_prf_t tls_prf,
766 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200767 int minor_ver,
768 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200769 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000770{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 unsigned char keyblk[256];
773 unsigned char *key1;
774 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100775 unsigned char *mac_enc;
776 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000777 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200778 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000779 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 const mbedtls_cipher_info_t *cipher_info;
782 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100783
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200784#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
785 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
786 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200787 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200788 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000789#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000790
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200791 /* Copy info about negotiated version and extensions */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100792#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
793 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200794 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000795#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200796 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 /*
799 * Get various info structures
800 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 if( ciphersuite_info == NULL )
803 {
804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200805 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
807 }
808
Hanno Becker8759e162017-12-27 21:34:08 +0000809 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100810 if( cipher_info == NULL )
811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000813 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100815 }
816
Hanno Becker8759e162017-12-27 21:34:08 +0000817 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100818 if( md_info == NULL )
819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000821 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100823 }
824
Hanno Beckera5a2b082019-05-15 14:03:01 +0100825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100826 /* Copy own and peer's CID if the use of the CID
827 * extension has been negotiated. */
828 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
829 {
830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100831
Hanno Becker4932f9f2019-05-03 15:23:51 +0100832 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100833 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100834 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100835 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100836
837 transform->out_cid_len = ssl->handshake->peer_cid_len;
838 memcpy( transform->out_cid, ssl->handshake->peer_cid,
839 ssl->handshake->peer_cid_len );
840 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
841 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100842 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100843#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100844
Paul Bakker5121ce52009-01-03 21:22:43 +0000845 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200846 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000847 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200848 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100849 if( ret != 0 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100852 return( ret );
853 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200856 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200857 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200858 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 /*
862 * Determine the appropriate key, IV and MAC length.
863 */
Paul Bakker68884e32013-01-07 18:20:04 +0100864
Hanno Beckere7f2df02017-12-27 08:17:40 +0000865 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
Hanno Beckerf1229442018-01-03 15:32:31 +0000867#if defined(MBEDTLS_GCM_C) || \
868 defined(MBEDTLS_CCM_C) || \
869 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871 cipher_info->mode == MBEDTLS_MODE_CCM ||
872 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 {
Hanno Becker8759e162017-12-27 21:34:08 +0000874 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200875
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000877 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000878 transform->taglen =
879 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200880
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200881 /* All modes haves 96-bit IVs;
882 * GCM and CCM has 4 implicit and 8 explicit bytes
883 * ChachaPoly has all 12 bytes implicit
884 */
Paul Bakker68884e32013-01-07 18:20:04 +0100885 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200886 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
887 transform->fixed_ivlen = 12;
888 else
889 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200891 /* Minimum length of encrypted record */
892 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000893 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100894 }
895 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000896#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
897#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
898 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
899 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200901 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
903 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100907 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 mac_key_len = mbedtls_md_get_size( md_info );
911 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200914 /*
915 * If HMAC is to be truncated, we shall keep the leftmost bytes,
916 * (rfc 6066 page 13 or rfc 2104 section 4),
917 * so we only need to adjust the length here.
918 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200919 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000922
923#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
924 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000925 * HMAC implementation which also truncates the key
926 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000927 mac_key_len = transform->maclen;
928#endif
929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931
932 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100933 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200935 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937 transform->minlen = transform->maclen;
938 else
Paul Bakker68884e32013-01-07 18:20:04 +0100939 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940 /*
941 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100942 * 1. if EtM is in use: one block plus MAC
943 * otherwise: * first multiple of blocklen greater than maclen
944 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200947 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100948 {
949 transform->minlen = transform->maclen
950 + cipher_info->block_size;
951 }
952 else
953#endif
954 {
955 transform->minlen = transform->maclen
956 + cipher_info->block_size
957 - transform->maclen % cipher_info->block_size;
958 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200961 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
962 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200963 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100964 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200967 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
968 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200969 {
970 transform->minlen += transform->ivlen;
971 }
972 else
973#endif
974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200977 }
Paul Bakker68884e32013-01-07 18:20:04 +0100978 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000980 else
981#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
985 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Hanno Beckere7f2df02017-12-27 08:17:40 +0000987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
988 (unsigned) keylen,
989 (unsigned) transform->minlen,
990 (unsigned) transform->ivlen,
991 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
993 /*
994 * Finally setup the cipher contexts, IVs and MAC secrets.
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200997 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001000 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker68884e32013-01-07 18:20:04 +01001002 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001005 /*
1006 * This is not used in TLS v1.1.
1007 */
Paul Bakker48916f92012-09-16 19:57:18 +00001008 iv_copy_len = ( transform->fixed_ivlen ) ?
1009 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001010 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1011 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 }
1014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_SSL_CLI_C */
1016#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001017 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001019 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker81c7b182017-11-09 18:39:33 +00001022 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001023 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001025 /*
1026 * This is not used in TLS v1.1.
1027 */
Paul Bakker48916f92012-09-16 19:57:18 +00001028 iv_copy_len = ( transform->fixed_ivlen ) ?
1029 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001030 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1031 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001032 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Hanno Becker92231322018-01-03 15:32:51 +00001041#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001043 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001044 {
Hanno Becker92231322018-01-03 15:32:51 +00001045 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001049 }
1050
Hanno Becker81c7b182017-11-09 18:39:33 +00001051 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1052 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001053 }
1054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1056#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1057 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001058 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001059 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001060 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1061 For AEAD-based ciphersuites, there is nothing to do here. */
1062 if( mac_key_len != 0 )
1063 {
1064 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1065 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1066 }
Paul Bakker68884e32013-01-07 18:20:04 +01001067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001068 else
1069#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001073 }
Hanno Becker92231322018-01-03 15:32:51 +00001074#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1077 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001078 {
1079 int ret = 0;
1080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001082
Hanno Beckere7f2df02017-12-27 08:17:40 +00001083 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001084 transform->iv_enc, transform->iv_dec,
1085 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001086 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001087 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001091 }
1092 }
Hanno Becker92231322018-01-03 15:32:51 +00001093#else
1094 ((void) mac_dec);
1095 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001097
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001098#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1099 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001101 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001102 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001103 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001104 iv_copy_len );
1105 }
1106#endif
1107
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001108 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 return( ret );
1113 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001114
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001115 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 cipher_info ) ) != 0 )
1117 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001119 return( ret );
1120 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001123 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127 return( ret );
1128 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001131 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001135 return( ret );
1136 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_CIPHER_MODE_CBC)
1139 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1142 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001145 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001146 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1149 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001152 return( ret );
1153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001157 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001159 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001161 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001164
Paul Bakker48916f92012-09-16 19:57:18 +00001165 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1166 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001168 if( deflateInit( &transform->ctx_deflate,
1169 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001170 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1173 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001174 }
1175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001177
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 return( 0 );
1179}
1180
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001181/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001182 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001183 *
1184 * Inputs:
1185 * - SSL/TLS minor version
1186 * - hash associated with the ciphersuite (only used by TLS 1.2)
1187 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001188 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001189 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1190 */
1191static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1192 int minor_ver,
1193 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001195#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1196 (void) hash;
1197#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001198
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001201 {
1202 handshake->tls_prf = ssl3_prf;
1203 handshake->calc_verify = ssl_calc_verify_ssl;
1204 handshake->calc_finished = ssl_calc_finished_ssl;
1205 }
1206 else
1207#endif
1208#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001209 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001210 {
1211 handshake->tls_prf = tls1_prf;
1212 handshake->calc_verify = ssl_calc_verify_tls;
1213 handshake->calc_finished = ssl_calc_finished_tls;
1214 }
1215 else
1216#endif
1217#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1218#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001219 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1220 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001221 {
1222 handshake->tls_prf = tls_prf_sha384;
1223 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1224 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1225 }
1226 else
1227#endif
1228#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001229 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001230 {
1231 handshake->tls_prf = tls_prf_sha256;
1232 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1233 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1234 }
1235 else
1236#endif
1237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1238 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1240 }
1241
1242 return( 0 );
1243}
1244
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001245/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001246 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001247 *
1248 * Parameters:
1249 * [in/out] handshake
1250 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1251 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 * [out] master
1253 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001255static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001257 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001258{
1259 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001260
1261#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001262 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001263 (void) ssl;
1264#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001265
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001266 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001267 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1269 return( 0 );
1270 }
1271
1272 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1273 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001274
1275#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001276 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001277 {
1278 unsigned char session_hash[48];
1279 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001280
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001281 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001282
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001283 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1284 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001285
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001286 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001287 "extended master secret",
1288 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001289 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001290 }
1291 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001292#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001293 {
1294 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1295 "master secret",
1296 handshake->randbytes, 64,
1297 master, 48 );
1298 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001299 if( ret != 0 )
1300 {
1301 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1302 return( ret );
1303 }
1304
1305 mbedtls_platform_zeroize( handshake->premaster,
1306 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001307
1308 return( 0 );
1309}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001310
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001311int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1312{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001313 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001314 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1315 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001316
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1318
1319 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001320 ret = ssl_set_handshake_prfs( ssl->handshake,
1321 ssl->minor_ver,
1322 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001323 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001324 {
1325 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001326 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001327 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001328
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001329 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001330 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001331 ssl->session_negotiate->master,
1332 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001333 if( ret != 0 )
1334 {
1335 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1336 return( ret );
1337 }
1338
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001339 /* Swap the client and server random values:
1340 * - MS derivation wanted client+server (RFC 5246 8.1)
1341 * - key derivation wants server+client (RFC 5246 6.3) */
1342 {
1343 unsigned char tmp[64];
1344 memcpy( tmp, ssl->handshake->randbytes, 64 );
1345 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1346 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1347 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1348 }
1349
1350 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001351 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001352 ssl->session_negotiate->ciphersuite,
1353 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001354#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001355#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1356 ssl->session_negotiate->encrypt_then_mac,
1357#endif
1358#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1359 ssl->session_negotiate->trunc_hmac,
1360#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001361#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001362#if defined(MBEDTLS_ZLIB_SUPPORT)
1363 ssl->session_negotiate->compression,
1364#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001365 ssl->handshake->tls_prf,
1366 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001367 ssl->minor_ver,
1368 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001369 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001370 if( ret != 0 )
1371 {
1372 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1373 return( ret );
1374 }
1375
1376 /* We no longer need Server/ClientHello.random values */
1377 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1378 sizeof( ssl->handshake->randbytes ) );
1379
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001380 /* Allocate compression buffer */
1381#if defined(MBEDTLS_ZLIB_SUPPORT)
1382 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1383 ssl->compress_buf == NULL )
1384 {
1385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1386 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1387 if( ssl->compress_buf == NULL )
1388 {
1389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001390 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001391 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1392 }
1393 }
1394#endif
1395
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1397
1398 return( 0 );
1399}
1400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001402void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1403 unsigned char hash[36],
1404 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001405{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001406 mbedtls_md5_context md5;
1407 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 unsigned char pad_1[48];
1409 unsigned char pad_2[48];
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001413 mbedtls_md5_init( &md5 );
1414 mbedtls_sha1_init( &sha1 );
1415
1416 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1417 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Paul Bakker380da532012-04-18 16:10:25 +00001419 memset( pad_1, 0x36, 48 );
1420 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001422 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1423 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1424 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001426 mbedtls_md5_starts_ret( &md5 );
1427 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1428 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1429 mbedtls_md5_update_ret( &md5, hash, 16 );
1430 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001432 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1433 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1434 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001435
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001436 mbedtls_sha1_starts_ret( &sha1 );
1437 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1438 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1439 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1440 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001441
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001442 *hlen = 36;
1443
1444 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001447 mbedtls_md5_free( &md5 );
1448 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001449
Paul Bakker380da532012-04-18 16:10:25 +00001450 return;
1451}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001455void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1456 unsigned char hash[36],
1457 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001458{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001459 mbedtls_md5_context md5;
1460 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001463
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001464 mbedtls_md5_init( &md5 );
1465 mbedtls_sha1_init( &sha1 );
1466
1467 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1468 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001469
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001470 mbedtls_md5_finish_ret( &md5, hash );
1471 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001473 *hlen = 36;
1474
1475 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001477
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001478 mbedtls_md5_free( &md5 );
1479 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001480
Paul Bakker380da532012-04-18 16:10:25 +00001481 return;
1482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1486#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001487void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1488 unsigned char hash[32],
1489 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001490{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001491 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001492
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001493 mbedtls_sha256_init( &sha256 );
1494
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001496
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001497 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001498 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001500 *hlen = 32;
1501
1502 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001504
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001505 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001506
Paul Bakker380da532012-04-18 16:10:25 +00001507 return;
1508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001512void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1513 unsigned char hash[48],
1514 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001515{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001516 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001517
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001518 mbedtls_sha512_init( &sha512 );
1519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001521
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001522 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001523 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001525 *hlen = 48;
1526
1527 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001530 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001531
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 return;
1533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#endif /* MBEDTLS_SHA512_C */
1535#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1538int 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 +02001539{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001540 unsigned char *p = ssl->handshake->premaster;
1541 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001542 const unsigned char *psk = ssl->conf->psk;
1543 size_t psk_len = ssl->conf->psk_len;
1544
1545 /* If the psk callback was called, use its result */
1546 if( ssl->handshake->psk != NULL )
1547 {
1548 psk = ssl->handshake->psk;
1549 psk_len = ssl->handshake->psk_len;
1550 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001551
1552 /*
1553 * PMS = struct {
1554 * opaque other_secret<0..2^16-1>;
1555 * opaque psk<0..2^16-1>;
1556 * };
1557 * with "other_secret" depending on the particular key exchange
1558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1560 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001561 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001562 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001564
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001565 *(p++) = (unsigned char)( psk_len >> 8 );
1566 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001567
1568 if( end < p || (size_t)( end - p ) < psk_len )
1569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1570
1571 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001572 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001573 }
1574 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1576#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1577 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001578 {
1579 /*
1580 * other_secret already set by the ClientKeyExchange message,
1581 * and is 48 bytes long
1582 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001583 if( end - p < 2 )
1584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1585
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001586 *p++ = 0;
1587 *p++ = 48;
1588 p += 48;
1589 }
1590 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1592#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1593 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001594 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001595 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001596 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001598 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001600 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001601 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604 return( ret );
1605 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001606 *(p++) = (unsigned char)( len >> 8 );
1607 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001608 p += len;
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 }
1612 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1614#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1615 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001617 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 size_t zlen;
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001621 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001622 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001625 return( ret );
1626 }
1627
1628 *(p++) = (unsigned char)( zlen >> 8 );
1629 *(p++) = (unsigned char)( zlen );
1630 p += zlen;
1631
Janos Follath3fbdada2018-08-15 10:26:53 +01001632 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1633 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634 }
1635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1639 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001640 }
1641
1642 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001643 if( end - p < 2 )
1644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001645
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001646 *(p++) = (unsigned char)( psk_len >> 8 );
1647 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001648
1649 if( end < p || (size_t)( end - p ) < psk_len )
1650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1651
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001652 memcpy( p, psk, psk_len );
1653 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001654
1655 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1656
1657 return( 0 );
1658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001662/*
1663 * SSLv3.0 MAC functions
1664 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001665#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001666static void ssl_mac( mbedtls_md_context_t *md_ctx,
1667 const unsigned char *secret,
1668 const unsigned char *buf, size_t len,
1669 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001670 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001671{
1672 unsigned char header[11];
1673 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001674 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1676 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001677
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001678 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001680 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001681 else
Paul Bakker68884e32013-01-07 18:20:04 +01001682 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
1684 memcpy( header, ctr, 8 );
1685 header[ 8] = (unsigned char) type;
1686 header[ 9] = (unsigned char)( len >> 8 );
1687 header[10] = (unsigned char)( len );
1688
Paul Bakker68884e32013-01-07 18:20:04 +01001689 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_md_starts( md_ctx );
1691 mbedtls_md_update( md_ctx, secret, md_size );
1692 mbedtls_md_update( md_ctx, padding, padlen );
1693 mbedtls_md_update( md_ctx, header, 11 );
1694 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001695 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
Paul Bakker68884e32013-01-07 18:20:04 +01001697 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_md_starts( md_ctx );
1699 mbedtls_md_update( md_ctx, secret, md_size );
1700 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001701 mbedtls_md_update( md_ctx, out, md_size );
1702 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001705
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001706/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001707 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001708#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001709 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1710 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1711 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1712/* This function makes sure every byte in the memory region is accessed
1713 * (in ascending addresses order) */
1714static void ssl_read_memory( unsigned char *p, size_t len )
1715{
1716 unsigned char acc = 0;
1717 volatile unsigned char force;
1718
1719 for( ; len != 0; p++, len-- )
1720 acc ^= *p;
1721
1722 force = acc;
1723 (void) force;
1724}
1725#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1726
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001727/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001728 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001729 */
Hanno Becker3307b532017-12-27 21:37:21 +00001730
Hanno Beckera5a2b082019-05-15 14:03:01 +01001731#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001732/* This functions transforms a DTLS plaintext fragment and a record content
1733 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001734 *
1735 * struct {
1736 * opaque content[DTLSPlaintext.length];
1737 * ContentType real_type;
1738 * uint8 zeros[length_of_padding];
1739 * } DTLSInnerPlaintext;
1740 *
1741 * Input:
1742 * - `content`: The beginning of the buffer holding the
1743 * plaintext to be wrapped.
1744 * - `*content_size`: The length of the plaintext in Bytes.
1745 * - `max_len`: The number of Bytes available starting from
1746 * `content`. This must be `>= *content_size`.
1747 * - `rec_type`: The desired record content type.
1748 *
1749 * Output:
1750 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1751 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1752 *
1753 * Returns:
1754 * - `0` on success.
1755 * - A negative error code if `max_len` didn't offer enough space
1756 * for the expansion.
1757 */
1758static int ssl_cid_build_inner_plaintext( unsigned char *content,
1759 size_t *content_size,
1760 size_t remaining,
1761 uint8_t rec_type )
1762{
1763 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001764 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1765 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1766 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001767
1768 /* Write real content type */
1769 if( remaining == 0 )
1770 return( -1 );
1771 content[ len ] = rec_type;
1772 len++;
1773 remaining--;
1774
1775 if( remaining < pad )
1776 return( -1 );
1777 memset( content + len, 0, pad );
1778 len += pad;
1779 remaining -= pad;
1780
1781 *content_size = len;
1782 return( 0 );
1783}
1784
Hanno Becker7dc25772019-05-20 15:08:01 +01001785/* This function parses a DTLSInnerPlaintext structure.
1786 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001787static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1788 size_t *content_size,
1789 uint8_t *rec_type )
1790{
1791 size_t remaining = *content_size;
1792
1793 /* Determine length of padding by skipping zeroes from the back. */
1794 do
1795 {
1796 if( remaining == 0 )
1797 return( -1 );
1798 remaining--;
1799 } while( content[ remaining ] == 0 );
1800
1801 *content_size = remaining;
1802 *rec_type = content[ remaining ];
1803
1804 return( 0 );
1805}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001806#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001807
Hanno Becker99abf512019-05-20 14:50:53 +01001808/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001809 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001810static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001811 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001812 mbedtls_record *rec )
1813{
Hanno Becker99abf512019-05-20 14:50:53 +01001814 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001815 *
1816 * additional_data = seq_num + TLSCompressed.type +
1817 * TLSCompressed.version + TLSCompressed.length;
1818 *
Hanno Becker99abf512019-05-20 14:50:53 +01001819 * For the CID extension, this is extended as follows
1820 * (quoting draft-ietf-tls-dtls-connection-id-05,
1821 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001822 *
1823 * additional_data = seq_num + DTLSPlaintext.type +
1824 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001825 * cid +
1826 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001827 * length_of_DTLSInnerPlaintext;
1828 */
1829
Hanno Becker3307b532017-12-27 21:37:21 +00001830 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1831 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001832 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001833
Hanno Beckera5a2b082019-05-15 14:03:01 +01001834#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001835 if( rec->cid_len != 0 )
1836 {
1837 memcpy( add_data + 11, rec->cid, rec->cid_len );
1838 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1839 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1840 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1841 *add_data_len = 13 + 1 + rec->cid_len;
1842 }
1843 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001844#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001845 {
1846 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1847 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1848 *add_data_len = 13;
1849 }
Hanno Becker3307b532017-12-27 21:37:21 +00001850}
1851
Hanno Becker611a83b2018-01-03 14:27:32 +00001852int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1853 mbedtls_ssl_transform *transform,
1854 mbedtls_record *rec,
1855 int (*f_rng)(void *, unsigned char *, size_t),
1856 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001857{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001859 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001860 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001861 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001862 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001863 size_t post_avail;
1864
1865 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001866#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001867 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001868 ((void) ssl);
1869#endif
1870
1871 /* The PRNG is used for dynamic IV generation that's used
1872 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1873#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1874 ( defined(MBEDTLS_AES_C) || \
1875 defined(MBEDTLS_ARIA_C) || \
1876 defined(MBEDTLS_CAMELLIA_C) ) && \
1877 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1878 ((void) f_rng);
1879 ((void) p_rng);
1880#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
Hanno Becker3307b532017-12-27 21:37:21 +00001884 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001885 {
Hanno Becker3307b532017-12-27 21:37:21 +00001886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1888 }
Hanno Becker505089d2019-05-01 09:45:57 +01001889 if( rec == NULL
1890 || rec->buf == NULL
1891 || rec->buf_len < rec->data_offset
1892 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001893#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001894 || rec->cid_len != 0
1895#endif
1896 )
Hanno Becker3307b532017-12-27 21:37:21 +00001897 {
1898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001900 }
1901
Hanno Becker3307b532017-12-27 21:37:21 +00001902 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001903 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001905 data, rec->data_len );
1906
1907 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1908
1909 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1910 {
1911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1912 (unsigned) rec->data_len,
1913 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1915 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001916
Hanno Beckera5a2b082019-05-15 14:03:01 +01001917#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001918 /*
1919 * Add CID information
1920 */
1921 rec->cid_len = transform->out_cid_len;
1922 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1923 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001924
1925 if( rec->cid_len != 0 )
1926 {
1927 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001928 * Wrap plaintext into DTLSInnerPlaintext structure.
1929 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001930 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001931 * Note that this changes `rec->data_len`, and hence
1932 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001933 */
1934 if( ssl_cid_build_inner_plaintext( data,
1935 &rec->data_len,
1936 post_avail,
1937 rec->type ) != 0 )
1938 {
1939 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1940 }
1941
1942 rec->type = MBEDTLS_SSL_MSG_CID;
1943 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001944#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001945
Hanno Becker92c930f2019-04-29 17:31:37 +01001946 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1947
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001949 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001951#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 if( mode == MBEDTLS_MODE_STREAM ||
1953 ( mode == MBEDTLS_MODE_CBC
1954#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001955 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001956#endif
1957 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 {
Hanno Becker3307b532017-12-27 21:37:21 +00001959 if( post_avail < transform->maclen )
1960 {
1961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1962 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1963 }
1964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001966 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001967 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001968 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001969 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1970 data, rec->data_len, rec->ctr, rec->type, mac );
1971 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001972 }
1973 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1976 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001977 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001978 {
Hanno Becker992b6872017-11-09 18:57:39 +00001979 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1980
Hanno Beckere83efe62019-04-29 13:52:53 +01001981 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001982
Hanno Becker3307b532017-12-27 21:37:21 +00001983 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001984 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001985 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1986 data, rec->data_len );
1987 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1988 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1989
1990 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001991 }
1992 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001993#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1996 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001997 }
1998
Hanno Becker3307b532017-12-27 21:37:21 +00001999 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2000 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002001
Hanno Becker3307b532017-12-27 21:37:21 +00002002 rec->data_len += transform->maclen;
2003 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002004 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002005 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002006#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002008 /*
2009 * Encrypt
2010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2012 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002014 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002015 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002017 "including %d bytes of padding",
2018 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
Hanno Becker3307b532017-12-27 21:37:21 +00002020 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2021 transform->iv_enc, transform->ivlen,
2022 data, rec->data_len,
2023 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002026 return( ret );
2027 }
2028
Hanno Becker3307b532017-12-27 21:37:21 +00002029 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2032 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 }
Paul Bakker68884e32013-01-07 18:20:04 +01002035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002037
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002038#if defined(MBEDTLS_GCM_C) || \
2039 defined(MBEDTLS_CCM_C) || \
2040 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002042 mode == MBEDTLS_MODE_CCM ||
2043 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002044 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002045 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002046 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002047 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002048
Hanno Becker3307b532017-12-27 21:37:21 +00002049 /* Check that there's space for both the authentication tag
2050 * and the explicit IV before and after the record content. */
2051 if( post_avail < transform->taglen ||
2052 rec->data_offset < explicit_iv_len )
2053 {
2054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2055 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2056 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002057
Paul Bakker68884e32013-01-07 18:20:04 +01002058 /*
2059 * Generate IV
2060 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002061 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2062 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002063 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002064 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002065 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2066 explicit_iv_len );
2067 /* Prefix record content with explicit IV. */
2068 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002069 }
2070 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2071 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002072 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002073 unsigned char i;
2074
2075 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2076
2077 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002078 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002079 }
2080 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002081 {
2082 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002085 }
2086
Hanno Beckere83efe62019-04-29 13:52:53 +01002087 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002088
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002089 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2090 iv, transform->ivlen );
2091 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002092 data - explicit_iv_len, explicit_iv_len );
2093 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002094 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002096 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002097 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002098
Paul Bakker68884e32013-01-07 18:20:04 +01002099 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002100 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002101 */
Hanno Becker3307b532017-12-27 21:37:21 +00002102
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002103 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002104 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002105 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002106 data, rec->data_len, /* source */
2107 data, &rec->data_len, /* destination */
2108 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002111 return( ret );
2112 }
2113
Hanno Becker3307b532017-12-27 21:37:21 +00002114 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2115 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002116
Hanno Becker3307b532017-12-27 21:37:21 +00002117 rec->data_len += transform->taglen + explicit_iv_len;
2118 rec->data_offset -= explicit_iv_len;
2119 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002120 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2124#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002125 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002128 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002129 size_t padlen, i;
2130 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131
Hanno Becker3307b532017-12-27 21:37:21 +00002132 /* Currently we're always using minimal padding
2133 * (up to 255 bytes would be allowed). */
2134 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2135 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 padlen = 0;
2137
Hanno Becker3307b532017-12-27 21:37:21 +00002138 /* Check there's enough space in the buffer for the padding. */
2139 if( post_avail < padlen + 1 )
2140 {
2141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2142 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2143 }
2144
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002146 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
Hanno Becker3307b532017-12-27 21:37:21 +00002148 rec->data_len += padlen + 1;
2149 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002152 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002153 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2154 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155 */
Hanno Becker3307b532017-12-27 21:37:21 +00002156 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002157 {
Hanno Becker3307b532017-12-27 21:37:21 +00002158 if( f_rng == NULL )
2159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2162 }
2163
2164 if( rec->data_offset < transform->ivlen )
2165 {
2166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2167 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2168 }
2169
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002170 /*
2171 * Generate IV
2172 */
Hanno Becker3307b532017-12-27 21:37:21 +00002173 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002174 if( ret != 0 )
2175 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002176
Hanno Becker3307b532017-12-27 21:37:21 +00002177 memcpy( data - transform->ivlen, transform->iv_enc,
2178 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002180 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002184 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002185 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002186 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
Hanno Becker3307b532017-12-27 21:37:21 +00002188 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2189 transform->iv_enc,
2190 transform->ivlen,
2191 data, rec->data_len,
2192 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002195 return( ret );
2196 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002197
Hanno Becker3307b532017-12-27 21:37:21 +00002198 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002202 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002205 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002206 {
2207 /*
2208 * Save IV in SSL3 and TLS1
2209 */
Hanno Becker3307b532017-12-27 21:37:21 +00002210 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2211 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 }
Hanno Becker3307b532017-12-27 21:37:21 +00002213 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002214#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002215 {
2216 data -= transform->ivlen;
2217 rec->data_offset -= transform->ivlen;
2218 rec->data_len += transform->ivlen;
2219 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002222 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002224 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2225
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002226 /*
2227 * MAC(MAC_write_key, seq_num +
2228 * TLSCipherText.type +
2229 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002230 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002231 * IV + // except for TLS 1.0
2232 * ENC(content + padding + padding_length));
2233 */
Hanno Becker3307b532017-12-27 21:37:21 +00002234
2235 if( post_avail < transform->maclen)
2236 {
2237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2238 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2239 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002240
Hanno Beckere83efe62019-04-29 13:52:53 +01002241 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002244 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002245 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002246
Hanno Becker3307b532017-12-27 21:37:21 +00002247 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002248 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002249 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2250 data, rec->data_len );
2251 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2252 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002253
Hanno Becker3307b532017-12-27 21:37:21 +00002254 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002255
Hanno Becker3307b532017-12-27 21:37:21 +00002256 rec->data_len += transform->maclen;
2257 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002258 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002259 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002262 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002264 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002270 /* Make extra sure authentication was performed, exactly once */
2271 if( auth_done != 1 )
2272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2274 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002275 }
2276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
2279 return( 0 );
2280}
2281
Hanno Becker611a83b2018-01-03 14:27:32 +00002282int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2283 mbedtls_ssl_transform *transform,
2284 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002285{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002286 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002288 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002289#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002290 size_t padlen = 0, correct = 1;
2291#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002292 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002293 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002294 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002295
Hanno Becker611a83b2018-01-03 14:27:32 +00002296#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002297 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002298 ((void) ssl);
2299#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002302 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002303 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2305 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2306 }
2307 if( rec == NULL ||
2308 rec->buf == NULL ||
2309 rec->buf_len < rec->data_offset ||
2310 rec->buf_len - rec->data_offset < rec->data_len )
2311 {
2312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002314 }
2315
Hanno Becker4c6876b2017-12-27 21:28:58 +00002316 data = rec->buf + rec->data_offset;
2317 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
Hanno Beckera5a2b082019-05-15 14:03:01 +01002319#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002320 /*
2321 * Match record's CID with incoming CID.
2322 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002323 if( rec->cid_len != transform->in_cid_len ||
2324 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2325 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002326 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002327 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002328#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2331 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002332 {
2333 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002334 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2335 transform->iv_dec,
2336 transform->ivlen,
2337 data, rec->data_len,
2338 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002341 return( ret );
2342 }
2343
Hanno Becker4c6876b2017-12-27 21:28:58 +00002344 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2347 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 }
Paul Bakker68884e32013-01-07 18:20:04 +01002350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002352#if defined(MBEDTLS_GCM_C) || \
2353 defined(MBEDTLS_CCM_C) || \
2354 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 mode == MBEDTLS_MODE_CCM ||
2357 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002358 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002359 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002360 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002361
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002362 /*
2363 * Compute and update sizes
2364 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002365 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002368 "+ taglen (%d)", rec->data_len,
2369 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002371 }
Paul Bakker68884e32013-01-07 18:20:04 +01002372
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002373 /*
2374 * Prepare IV
2375 */
2376 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2377 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002378 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002379 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002380 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002381
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002382 }
2383 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2384 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002385 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002386 unsigned char i;
2387
2388 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2389
2390 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002391 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002392 }
2393 else
2394 {
2395 /* Reminder if we ever add an AEAD mode with a different size */
2396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2398 }
2399
Hanno Becker4c6876b2017-12-27 21:28:58 +00002400 data += explicit_iv_len;
2401 rec->data_offset += explicit_iv_len;
2402 rec->data_len -= explicit_iv_len + transform->taglen;
2403
Hanno Beckere83efe62019-04-29 13:52:53 +01002404 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002405 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002406 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002407
2408 memcpy( transform->iv_dec + transform->fixed_ivlen,
2409 data - explicit_iv_len, explicit_iv_len );
2410
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002411 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002412 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002413 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002414
Paul Bakker68884e32013-01-07 18:20:04 +01002415
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002416 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002417 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002418 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002419 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2420 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002421 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002422 data, rec->data_len,
2423 data, &olen,
2424 data + rec->data_len,
2425 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2430 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002431
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002432 return( ret );
2433 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002434 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002435
Hanno Becker4c6876b2017-12-27 21:28:58 +00002436 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2439 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002440 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2444#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002445 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002448 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002449
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 /*
Paul Bakker45829992013-01-03 14:52:21 +01002451 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002454 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2455 {
2456 /* The ciphertext is prefixed with the CBC IV. */
2457 minlen += transform->ivlen;
2458 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002459#endif
Paul Bakker45829992013-01-03 14:52:21 +01002460
Hanno Becker4c6876b2017-12-27 21:28:58 +00002461 /* Size considerations:
2462 *
2463 * - The CBC cipher text must not be empty and hence
2464 * at least of size transform->ivlen.
2465 *
2466 * Together with the potential IV-prefix, this explains
2467 * the first of the two checks below.
2468 *
2469 * - The record must contain a MAC, either in plain or
2470 * encrypted, depending on whether Encrypt-then-MAC
2471 * is used or not.
2472 * - If it is, the message contains the IV-prefix,
2473 * the CBC ciphertext, and the MAC.
2474 * - If it is not, the padded plaintext, and hence
2475 * the CBC ciphertext, has at least length maclen + 1
2476 * because there is at least the padding length byte.
2477 *
2478 * As the CBC ciphertext is not empty, both cases give the
2479 * lower bound minlen + maclen + 1 on the record size, which
2480 * we test for in the second check below.
2481 */
2482 if( rec->data_len < minlen + transform->ivlen ||
2483 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002486 "+ 1 ) ( + expl IV )", rec->data_len,
2487 transform->ivlen,
2488 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002490 }
2491
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002492 /*
2493 * Authenticate before decrypt if enabled
2494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002496 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497 {
Hanno Becker992b6872017-11-09 18:57:39 +00002498 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002501
Hanno Becker4c6876b2017-12-27 21:28:58 +00002502 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2503 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504
Hanno Beckere83efe62019-04-29 13:52:53 +01002505 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002506
Hanno Beckere83efe62019-04-29 13:52:53 +01002507 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2508 add_data_len );
2509 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2510 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002511 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2512 data, rec->data_len );
2513 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2514 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002515
Hanno Becker4c6876b2017-12-27 21:28:58 +00002516 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2517 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002518 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002519 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520
Hanno Becker4c6876b2017-12-27 21:28:58 +00002521 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2522 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002526 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002527 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002530
2531 /*
2532 * Check length sanity
2533 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002534 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002537 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002539 }
2540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002542 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002543 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002544 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002545 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002546 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002547 /* This is safe because data_len >= minlen + maclen + 1 initially,
2548 * and at this point we have at most subtracted maclen (note that
2549 * minlen == transform->ivlen here). */
2550 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002551
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 data += transform->ivlen;
2553 rec->data_offset += transform->ivlen;
2554 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002555 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002557
Hanno Becker4c6876b2017-12-27 21:28:58 +00002558 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2559 transform->iv_dec, transform->ivlen,
2560 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002563 return( ret );
2564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002565
Hanno Becker4c6876b2017-12-27 21:28:58 +00002566 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2569 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002570 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002573 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002574 {
2575 /*
2576 * Save IV in SSL3 and TLS1
2577 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002578 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2579 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002581#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583 /* Safe since data_len >= minlen + maclen + 1, so after having
2584 * subtracted at most minlen and maclen up to this point,
2585 * data_len > 0. */
2586 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002587
Hanno Becker4c6876b2017-12-27 21:28:58 +00002588 if( auth_done == 1 )
2589 {
2590 correct *= ( rec->data_len >= padlen + 1 );
2591 padlen *= ( rec->data_len >= padlen + 1 );
2592 }
2593 else
Paul Bakker45829992013-01-03 14:52:21 +01002594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002596 if( rec->data_len < transform->maclen + padlen + 1 )
2597 {
2598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2599 rec->data_len,
2600 transform->maclen,
2601 padlen + 1 ) );
2602 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002603#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002604
2605 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2606 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Hanno Becker4c6876b2017-12-27 21:28:58 +00002609 padlen++;
2610
2611 /* Regardless of the validity of the padding,
2612 * we have data_len >= padlen here. */
2613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002615 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002617 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#if defined(MBEDTLS_SSL_DEBUG_ALL)
2620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002621 "should be no more than %d",
2622 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002623#endif
Paul Bakker45829992013-01-03 14:52:21 +01002624 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626 }
2627 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2629#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2630 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002631 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002633 /* The padding check involves a series of up to 256
2634 * consecutive memory reads at the end of the record
2635 * plaintext buffer. In order to hide the length and
2636 * validity of the padding, always perform exactly
2637 * `min(256,plaintext_len)` reads (but take into account
2638 * only the last `padlen` bytes for the padding check). */
2639 size_t pad_count = 0;
2640 size_t real_count = 0;
2641 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002642
Hanno Becker4c6876b2017-12-27 21:28:58 +00002643 /* Index of first padding byte; it has been ensured above
2644 * that the subtraction is safe. */
2645 size_t const padding_idx = rec->data_len - padlen;
2646 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2647 size_t const start_idx = rec->data_len - num_checks;
2648 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002649
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002651 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002652 real_count |= ( idx >= padding_idx );
2653 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002654 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002655 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002658 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002660#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002661 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002663 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2665 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2668 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002669 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670
Hanno Becker4c6876b2017-12-27 21:28:58 +00002671 /* If the padding was found to be invalid, padlen == 0
2672 * and the subtraction is safe. If the padding was found valid,
2673 * padlen hasn't been changed and the previous assertion
2674 * data_len >= padlen still holds. */
2675 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002677 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002679 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2682 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002685#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002687 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002688#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
2690 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002691 * Authenticate if not done yet.
2692 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002694#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002695 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002696 {
Hanno Becker992b6872017-11-09 18:57:39 +00002697 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002698
Hanno Becker4c6876b2017-12-27 21:28:58 +00002699 /* If the initial value of padlen was such that
2700 * data_len < maclen + padlen + 1, then padlen
2701 * got reset to 1, and the initial check
2702 * data_len >= minlen + maclen + 1
2703 * guarantees that at this point we still
2704 * have at least data_len >= maclen.
2705 *
2706 * If the initial value of padlen was such that
2707 * data_len >= maclen + padlen + 1, then we have
2708 * subtracted either padlen + 1 (if the padding was correct)
2709 * or 0 (if the padding was incorrect) since then,
2710 * hence data_len >= maclen in any case.
2711 */
2712 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Hanno Beckere83efe62019-04-29 13:52:53 +01002714 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002717 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002718 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002719 ssl_mac( &transform->md_ctx_dec,
2720 transform->mac_dec,
2721 data, rec->data_len,
2722 rec->ctr, rec->type,
2723 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002724 }
2725 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2727#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2728 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002729 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002730 {
2731 /*
2732 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002733 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002734 *
2735 * Known timing attacks:
2736 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2737 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002738 * To compensate for different timings for the MAC calculation
2739 * depending on how much padding was removed (which is determined
2740 * by padlen), process extra_run more blocks through the hash
2741 * function.
2742 *
2743 * The formula in the paper is
2744 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2745 * where L1 is the size of the header plus the decrypted message
2746 * plus CBC padding and L2 is the size of the header plus the
2747 * decrypted message. This is for an underlying hash function
2748 * with 64-byte blocks.
2749 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2750 * correctly. We round down instead of up, so -56 is the correct
2751 * value for our calculations instead of -55.
2752 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002753 * Repeat the formula rather than defining a block_size variable.
2754 * This avoids requiring division by a variable at runtime
2755 * (which would be marginally less efficient and would require
2756 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002757 */
2758 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002759 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002760
2761 /*
2762 * The next two sizes are the minimum and maximum values of
2763 * in_msglen over all padlen values.
2764 *
2765 * They're independent of padlen, since we previously did
2766 * in_msglen -= padlen.
2767 *
2768 * Note that max_len + maclen is never more than the buffer
2769 * length, as we previously did in_msglen -= maclen too.
2770 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002771 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002772 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2773
Hanno Becker4c6876b2017-12-27 21:28:58 +00002774 memset( tmp, 0, sizeof( tmp ) );
2775
2776 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002778#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2779 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002780 case MBEDTLS_MD_MD5:
2781 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002782 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002783 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002784 extra_run =
2785 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2786 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002787 break;
2788#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002789#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002790 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002791 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002792 extra_run =
2793 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2794 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002795 break;
2796#endif
2797 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2800 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002801
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002802 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002803
Hanno Beckere83efe62019-04-29 13:52:53 +01002804 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2805 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002806 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2807 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002808 /* Make sure we access everything even when padlen > 0. This
2809 * makes the synchronisation requirements for just-in-time
2810 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002811 ssl_read_memory( data + rec->data_len, padlen );
2812 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002813
2814 /* Call mbedtls_md_process at least once due to cache attacks
2815 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002816 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002817 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002818
Hanno Becker4c6876b2017-12-27 21:28:58 +00002819 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002820
2821 /* Make sure we access all the memory that could contain the MAC,
2822 * before we check it in the next code block. This makes the
2823 * synchronisation requirements for just-in-time Prime+Probe
2824 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002825 ssl_read_memory( data + min_len,
2826 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002827 }
2828 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2830 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2833 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002834 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002836#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002837 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2838 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002839#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Hanno Becker4c6876b2017-12-27 21:28:58 +00002841 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2842 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#if defined(MBEDTLS_SSL_DEBUG_ALL)
2845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002846#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002847 correct = 0;
2848 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002849 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002850 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002851
2852 /*
2853 * Finally check the correct flag
2854 */
2855 if( correct == 0 )
2856 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002857#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002858
2859 /* Make extra sure authentication was performed, exactly once */
2860 if( auth_done != 1 )
2861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Hanno Beckera5a2b082019-05-15 14:03:01 +01002866#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002867 if( rec->cid_len != 0 )
2868 {
2869 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2870 &rec->type );
2871 if( ret != 0 )
2872 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2873 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002874#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
2878 return( 0 );
2879}
2880
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002881#undef MAC_NONE
2882#undef MAC_PLAINTEXT
2883#undef MAC_CIPHERTEXT
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002886/*
2887 * Compression/decompression functions
2888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890{
2891 int ret;
2892 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002893 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002894 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002895 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002898
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002899 if( len_pre == 0 )
2900 return( 0 );
2901
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902 memcpy( msg_pre, ssl->out_msg, len_pre );
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905 ssl->out_msglen ) );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908 ssl->out_msg, ssl->out_msglen );
2909
Paul Bakker48916f92012-09-16 19:57:18 +00002910 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2911 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2912 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002913 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914
Paul Bakker48916f92012-09-16 19:57:18 +00002915 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002916 if( ret != Z_OK )
2917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2919 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 }
2921
Angus Grattond8213d02016-05-25 20:56:48 +10002922 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002923 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926 ssl->out_msglen ) );
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002929 ssl->out_msg, ssl->out_msglen );
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002932
2933 return( 0 );
2934}
2935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002937{
2938 int ret;
2939 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002940 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002941 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002942 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002945
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002946 if( len_pre == 0 )
2947 return( 0 );
2948
Paul Bakker2770fbd2012-07-03 13:30:23 +00002949 memcpy( msg_pre, ssl->in_msg, len_pre );
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002952 ssl->in_msglen ) );
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002955 ssl->in_msg, ssl->in_msglen );
2956
Paul Bakker48916f92012-09-16 19:57:18 +00002957 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2958 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2959 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002960 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002961 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002962
Paul Bakker48916f92012-09-16 19:57:18 +00002963 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002964 if( ret != Z_OK )
2965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2967 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002968 }
2969
Angus Grattond8213d02016-05-25 20:56:48 +10002970 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002971 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002974 ssl->in_msglen ) );
2975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002977 ssl->in_msg, ssl->in_msglen );
2978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002980
2981 return( 0 );
2982}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2986static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#if defined(MBEDTLS_SSL_PROTO_DTLS)
2989static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002990{
2991 /* If renegotiation is not enforced, retransmit until we would reach max
2992 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002993 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002994 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002995 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002996 unsigned char doublings = 1;
2997
2998 while( ratio != 0 )
2999 {
3000 ++doublings;
3001 ratio >>= 1;
3002 }
3003
3004 if( ++ssl->renego_records_seen > doublings )
3005 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003007 return( 0 );
3008 }
3009 }
3010
3011 return( ssl_write_hello_request( ssl ) );
3012}
3013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003015
Paul Bakker5121ce52009-01-03 21:22:43 +00003016/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003017 * Fill the input message buffer by appending data to it.
3018 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003019 *
3020 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3021 * available (from this read and/or a previous one). Otherwise, an error code
3022 * is returned (possibly EOF or WANT_READ).
3023 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003024 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3025 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3026 * since we always read a whole datagram at once.
3027 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003028 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003029 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003032{
Paul Bakker23986e52011-04-24 08:57:21 +00003033 int ret;
3034 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003038 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003041 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003043 }
3044
Angus Grattond8213d02016-05-25 20:56:48 +10003045 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003049 }
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003052 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003054 uint32_t timeout;
3055
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003056 /* Just to be sure */
3057 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3058 {
3059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3060 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3062 }
3063
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003064 /*
3065 * The point is, we need to always read a full datagram at once, so we
3066 * sometimes read more then requested, and handle the additional data.
3067 * It could be the rest of the current record (while fetching the
3068 * header) and/or some other records in the same datagram.
3069 */
3070
3071 /*
3072 * Move to the next record in the already read datagram if applicable
3073 */
3074 if( ssl->next_record_offset != 0 )
3075 {
3076 if( ssl->in_left < ssl->next_record_offset )
3077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003080 }
3081
3082 ssl->in_left -= ssl->next_record_offset;
3083
3084 if( ssl->in_left != 0 )
3085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003087 ssl->next_record_offset ) );
3088 memmove( ssl->in_hdr,
3089 ssl->in_hdr + ssl->next_record_offset,
3090 ssl->in_left );
3091 }
3092
3093 ssl->next_record_offset = 0;
3094 }
3095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003098
3099 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003100 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003101 */
3102 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003105 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003106 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003107
3108 /*
3109 * A record can't be split accross datagrams. If we need to read but
3110 * are not at the beginning of a new record, the caller did something
3111 * wrong.
3112 */
3113 if( ssl->in_left != 0 )
3114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3116 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003117 }
3118
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003119 /*
3120 * Don't even try to read if time's out already.
3121 * This avoids by-passing the timer when repeatedly receiving messages
3122 * that will end up being dropped.
3123 */
3124 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003125 {
3126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003127 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003128 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003129 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003130 {
Angus Grattond8213d02016-05-25 20:56:48 +10003131 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003134 timeout = ssl->handshake->retransmit_timeout;
3135 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003136 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003139
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003140 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003141 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3142 timeout );
3143 else
3144 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003147
3148 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003150 }
3151
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003152 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003155 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003158 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003159 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003162 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003163 }
3164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003168 return( ret );
3169 }
3170
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003171 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003174 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003176 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003177 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003180 return( ret );
3181 }
3182
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003183 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003184 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003186 }
3187
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 if( ret < 0 )
3189 return( ret );
3190
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003191 ssl->in_left = ret;
3192 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003193 MBEDTLS_SSL_TRANSPORT_ELSE
3194#endif /* MBEDTLS_SSL_PROTO_DTLS */
3195#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003198 ssl->in_left, nb_want ) );
3199
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003200 while( ssl->in_left < nb_want )
3201 {
3202 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003203
3204 if( ssl_check_timer( ssl ) != 0 )
3205 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3206 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003207 {
3208 if( ssl->f_recv_timeout != NULL )
3209 {
3210 ret = ssl->f_recv_timeout( ssl->p_bio,
3211 ssl->in_hdr + ssl->in_left, len,
3212 ssl->conf->read_timeout );
3213 }
3214 else
3215 {
3216 ret = ssl->f_recv( ssl->p_bio,
3217 ssl->in_hdr + ssl->in_left, len );
3218 }
3219 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003222 ssl->in_left, nb_want ) );
3223 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003224
3225 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003227
3228 if( ret < 0 )
3229 return( ret );
3230
mohammad160352aecb92018-03-28 23:41:40 -07003231 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003232 {
Darryl Green11999bb2018-03-13 15:22:58 +00003233 MBEDTLS_SSL_DEBUG_MSG( 1,
3234 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003235 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3237 }
3238
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003239 ssl->in_left += ret;
3240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003241 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003242#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003245
3246 return( 0 );
3247}
3248
3249/*
3250 * Flush any data not yet written
3251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003253{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003254 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003255 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003259 if( ssl->f_send == NULL )
3260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003262 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003264 }
3265
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003266 /* Avoid incrementing counter if data is flushed */
3267 if( ssl->out_left == 0 )
3268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003270 return( 0 );
3271 }
3272
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 while( ssl->out_left > 0 )
3274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003276 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277
Hanno Becker2b1e3542018-08-06 11:19:13 +01003278 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003279 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
3283 if( ret <= 0 )
3284 return( ret );
3285
mohammad160352aecb92018-03-28 23:41:40 -07003286 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003287 {
Darryl Green11999bb2018-03-13 15:22:58 +00003288 MBEDTLS_SSL_DEBUG_MSG( 1,
3289 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003290 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003291 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3292 }
3293
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 ssl->out_left -= ret;
3295 }
3296
Hanno Becker2b1e3542018-08-06 11:19:13 +01003297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003298 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003299 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003300 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003301 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003302 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003303#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003304#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003305 {
3306 ssl->out_hdr = ssl->out_buf + 8;
3307 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003308#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003309 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
3313 return( 0 );
3314}
3315
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003316/*
3317 * Functions to handle the DTLS retransmission state machine
3318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003320/*
3321 * Append current handshake message to current outgoing flight
3322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3327 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3328 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003329
3330 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003331 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003332 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003335 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003336 }
3337
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003338 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003342 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343 }
3344
3345 /* Copy current handshake message with headers */
3346 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3347 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003348 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349 msg->next = NULL;
3350
3351 /* Append to the current flight */
3352 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003353 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003354 else
3355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357 while( cur->next != NULL )
3358 cur = cur->next;
3359 cur->next = msg;
3360 }
3361
Hanno Becker3b235902018-08-06 09:54:53 +01003362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003363 return( 0 );
3364}
3365
3366/*
3367 * Free the current flight of handshake messages
3368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 mbedtls_ssl_flight_item *cur = flight;
3372 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003373
3374 while( cur != NULL )
3375 {
3376 next = cur->next;
3377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 mbedtls_free( cur->p );
3379 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003380
3381 cur = next;
3382 }
3383}
3384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3386static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003387#endif
3388
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003389/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003390 * Swap transform_out and out_ctr with the alternative ones
3391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003395 unsigned char tmp_out_ctr[8];
3396
3397 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003400 return;
3401 }
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003404
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003405 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003406 tmp_transform = ssl->transform_out;
3407 ssl->transform_out = ssl->handshake->alt_transform_out;
3408 ssl->handshake->alt_transform_out = tmp_transform;
3409
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003410 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003411 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3412 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003413 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003414
3415 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003416 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3419 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3424 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003425 }
3426 }
3427#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003428}
3429
3430/*
3431 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003432 */
3433int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3434{
3435 int ret = 0;
3436
3437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3438
3439 ret = mbedtls_ssl_flight_transmit( ssl );
3440
3441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3442
3443 return( ret );
3444}
3445
3446/*
3447 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003448 *
3449 * Need to remember the current message in case flush_output returns
3450 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003451 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003452 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003453int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003454{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003455 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461
3462 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003463 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003464 ssl_swap_epochs( ssl );
3465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003467 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003468
3469 while( ssl->handshake->cur_msg != NULL )
3470 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003471 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003472 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003473
Hanno Beckere1dcb032018-08-17 16:47:58 +01003474 int const is_finished =
3475 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3476 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3477
Hanno Becker04da1892018-08-14 13:22:10 +01003478 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3479 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3480
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003481 /* Swap epochs before sending Finished: we can't do it after
3482 * sending ChangeCipherSpec, in case write returns WANT_READ.
3483 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003484 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003485 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003487 ssl_swap_epochs( ssl );
3488 }
3489
Hanno Becker67bc7c32018-08-06 11:33:50 +01003490 ret = ssl_get_remaining_payload_in_datagram( ssl );
3491 if( ret < 0 )
3492 return( ret );
3493 max_frag_len = (size_t) ret;
3494
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003495 /* CCS is copied as is, while HS messages may need fragmentation */
3496 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3497 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003498 if( max_frag_len == 0 )
3499 {
3500 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3501 return( ret );
3502
3503 continue;
3504 }
3505
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003506 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003507 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003508 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003509
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003510 /* Update position inside current message */
3511 ssl->handshake->cur_msg_p += cur->len;
3512 }
3513 else
3514 {
3515 const unsigned char * const p = ssl->handshake->cur_msg_p;
3516 const size_t hs_len = cur->len - 12;
3517 const size_t frag_off = p - ( cur->p + 12 );
3518 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003519 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003520
Hanno Beckere1dcb032018-08-17 16:47:58 +01003521 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003522 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003523 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003524 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003525
Hanno Becker67bc7c32018-08-06 11:33:50 +01003526 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3527 return( ret );
3528
3529 continue;
3530 }
3531 max_hs_frag_len = max_frag_len - 12;
3532
3533 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3534 max_hs_frag_len : rem_len;
3535
3536 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003537 {
3538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003539 (unsigned) cur_hs_frag_len,
3540 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003541 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003542
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003543 /* Messages are stored with handshake headers as if not fragmented,
3544 * copy beginning of headers then fill fragmentation fields.
3545 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3546 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003547
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003548 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3549 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3550 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3551
Hanno Becker67bc7c32018-08-06 11:33:50 +01003552 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3553 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3554 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003555
3556 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3557
Hanno Becker3f7b9732018-08-28 09:53:25 +01003558 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003559 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3560 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003561 ssl->out_msgtype = cur->type;
3562
3563 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003564 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003565 }
3566
3567 /* If done with the current message move to the next one if any */
3568 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3569 {
3570 if( cur->next != NULL )
3571 {
3572 ssl->handshake->cur_msg = cur->next;
3573 ssl->handshake->cur_msg_p = cur->next->p + 12;
3574 }
3575 else
3576 {
3577 ssl->handshake->cur_msg = NULL;
3578 ssl->handshake->cur_msg_p = NULL;
3579 }
3580 }
3581
3582 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003583 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003586 return( ret );
3587 }
3588 }
3589
Hanno Becker67bc7c32018-08-06 11:33:50 +01003590 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3591 return( ret );
3592
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003593 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3595 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003596 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003599 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3600 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003601
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003603
3604 return( 0 );
3605}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003606
3607/*
3608 * To be called when the last message of an incoming flight is received.
3609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003611{
3612 /* We won't need to resend that one any more */
3613 ssl_flight_free( ssl->handshake->flight );
3614 ssl->handshake->flight = NULL;
3615 ssl->handshake->cur_msg = NULL;
3616
3617 /* The next incoming flight will start with this msg_seq */
3618 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3619
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003620 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003621 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003622
Hanno Becker0271f962018-08-16 13:23:47 +01003623 /* Clear future message buffering structure. */
3624 ssl_buffering_free( ssl );
3625
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003626 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003627 ssl_set_timer( ssl, 0 );
3628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3630 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003633 }
3634 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003636}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003637
3638/*
3639 * To be called when the last message of an outgoing flight is send.
3640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003642{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003643 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003644 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3647 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003650 }
3651 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003655
Paul Bakker5121ce52009-01-03 21:22:43 +00003656/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003659
3660/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003661 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003662 *
3663 * - fill in handshake headers
3664 * - update handshake checksum
3665 * - DTLS: save message for resending
3666 * - then pass to the record layer
3667 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003668 * DTLS: except for HelloRequest, messages are only queued, and will only be
3669 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003670 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003671 * Inputs:
3672 * - ssl->out_msglen: 4 + actual handshake message len
3673 * (4 is the size of handshake headers for TLS)
3674 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3675 * - ssl->out_msg + 4: the handshake message body
3676 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003677 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003678 * - ssl->out_msglen: the length of the record contents
3679 * (including handshake headers but excluding record headers)
3680 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003681 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003683{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003684 int ret;
3685 const size_t hs_len = ssl->out_msglen - 4;
3686 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3689
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003690 /*
3691 * Sanity checks
3692 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003693 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003694 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3695 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003696 /* In SSLv3, the client might send a NoCertificate alert. */
3697#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3698 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3699 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3700 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3701#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3702 {
3703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3705 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003706 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003707
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003708 /* Whenever we send anything different from a
3709 * HelloRequest we should be in a handshake - double check. */
3710 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3711 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003712 ssl->handshake == NULL )
3713 {
3714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3716 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003719 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003722 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003725 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003726#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003727
Hanno Beckerb50a2532018-08-06 11:52:54 +01003728 /* Double-check that we did not exceed the bounds
3729 * of the outgoing record buffer.
3730 * This should never fail as the various message
3731 * writing functions must obey the bounds of the
3732 * outgoing record buffer, but better be safe.
3733 *
3734 * Note: We deliberately do not check for the MTU or MFL here.
3735 */
3736 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3737 {
3738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3739 "size %u, maximum %u",
3740 (unsigned) ssl->out_msglen,
3741 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3742 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3743 }
3744
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003745 /*
3746 * Fill handshake headers
3747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003749 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003750 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3751 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3752 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003753
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003754 /*
3755 * DTLS has additional fields in the Handshake layer,
3756 * between the length field and the actual payload:
3757 * uint16 message_seq;
3758 * uint24 fragment_offset;
3759 * uint24 fragment_length;
3760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003762 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003763 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003764 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003765 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003766 {
3767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3768 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003770 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3772 }
3773
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003774 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003775 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003776
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003777 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003778 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003779 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003780 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3781 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3782 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003783 }
3784 else
3785 {
3786 ssl->out_msg[4] = 0;
3787 ssl->out_msg[5] = 0;
3788 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003789
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003790 /* Handshake hashes are computed without fragmentation,
3791 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003792 memset( ssl->out_msg + 6, 0x00, 3 );
3793 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003796
Hanno Becker0207e532018-08-28 10:28:28 +01003797 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003798 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3799 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 }
3801
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003802 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003804 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003805 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3806 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003807 {
3808 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003811 return( ret );
3812 }
3813 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003814 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003815#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003816 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003817 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003818 {
3819 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3820 return( ret );
3821 }
3822 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003823
3824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3825
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003826 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003827}
3828
3829/*
3830 * Record layer functions
3831 */
3832
3833/*
3834 * Write current record.
3835 *
3836 * Uses:
3837 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3838 * - ssl->out_msglen: length of the record content (excl headers)
3839 * - ssl->out_msg: record content
3840 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003841int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003842{
3843 int ret, done = 0;
3844 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003845 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003846
3847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003850 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003852 {
3853 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003856 return( ret );
3857 }
3858
3859 len = ssl->out_msglen;
3860 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3864 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 ret = mbedtls_ssl_hw_record_write( ssl );
3869 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3872 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003873 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003874
3875 if( ret == 0 )
3876 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003877 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003879 if( !done )
3880 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003881 unsigned i;
3882 size_t protected_record_size;
3883
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003884 /* Skip writing the record content type to after the encryption,
3885 * as it may change when using the CID extension. */
3886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003888 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003889
Hanno Becker19859472018-08-06 09:40:20 +01003890 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003891 ssl->out_len[0] = (unsigned char)( len >> 8 );
3892 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003893
Paul Bakker48916f92012-09-16 19:57:18 +00003894 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003895 {
Hanno Becker3307b532017-12-27 21:37:21 +00003896 mbedtls_record rec;
3897
3898 rec.buf = ssl->out_iv;
3899 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3900 ( ssl->out_iv - ssl->out_buf );
3901 rec.data_len = ssl->out_msglen;
3902 rec.data_offset = ssl->out_msg - rec.buf;
3903
3904 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3905 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3906 ssl->conf->transport, rec.ver );
3907 rec.type = ssl->out_msgtype;
3908
Hanno Beckera5a2b082019-05-15 14:03:01 +01003909#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003910 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003911 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003912#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003913
Hanno Becker611a83b2018-01-03 14:27:32 +00003914 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003915 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003918 return( ret );
3919 }
3920
Hanno Becker3307b532017-12-27 21:37:21 +00003921 if( rec.data_offset != 0 )
3922 {
3923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3924 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3925 }
3926
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003927 /* Update the record content type and CID. */
3928 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003929#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003930 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003931#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003932 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003933 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3934 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003935 }
3936
Hanno Becker43395762019-05-03 14:46:38 +01003937 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003938
3939#if defined(MBEDTLS_SSL_PROTO_DTLS)
3940 /* In case of DTLS, double-check that we don't exceed
3941 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003942 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003943 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003944 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003945 if( ret < 0 )
3946 return( ret );
3947
3948 if( protected_record_size > (size_t) ret )
3949 {
3950 /* Should never happen */
3951 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3952 }
3953 }
3954#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003955
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003956 /* Now write the potentially updated record content type. */
3957 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003960 "version = [%d:%d], msglen = %d",
3961 ssl->out_hdr[0], ssl->out_hdr[1],
3962 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003965 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003966
3967 ssl->out_left += protected_record_size;
3968 ssl->out_hdr += protected_record_size;
3969 ssl_update_out_pointers( ssl, ssl->transform_out );
3970
Hanno Becker04484622018-08-06 09:49:38 +01003971 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3972 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3973 break;
3974
3975 /* The loop goes to its end iff the counter is wrapping */
3976 if( i == ssl_ep_len( ssl ) )
3977 {
3978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3979 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003981 }
3982
Hanno Becker67bc7c32018-08-06 11:33:50 +01003983#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003984 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003985 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003986 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003987 size_t remaining;
3988 ret = ssl_get_remaining_payload_in_datagram( ssl );
3989 if( ret < 0 )
3990 {
3991 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3992 ret );
3993 return( ret );
3994 }
3995
3996 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003997 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003998 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003999 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004000 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004001 else
4002 {
Hanno Becker513815a2018-08-20 11:56:09 +01004003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004004 }
4005 }
4006#endif /* MBEDTLS_SSL_PROTO_DTLS */
4007
4008 if( ( flush == SSL_FORCE_FLUSH ) &&
4009 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004012 return( ret );
4013 }
4014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004016
4017 return( 0 );
4018}
4019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004021
4022static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4023{
4024 if( ssl->in_msglen < ssl->in_hslen ||
4025 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4026 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4027 {
4028 return( 1 );
4029 }
4030 return( 0 );
4031}
Hanno Becker44650b72018-08-16 12:51:11 +01004032
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004033static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004034{
4035 return( ( ssl->in_msg[9] << 16 ) |
4036 ( ssl->in_msg[10] << 8 ) |
4037 ssl->in_msg[11] );
4038}
4039
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004040static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004041{
4042 return( ( ssl->in_msg[6] << 16 ) |
4043 ( ssl->in_msg[7] << 8 ) |
4044 ssl->in_msg[8] );
4045}
4046
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004047static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004048{
4049 uint32_t msg_len, frag_off, frag_len;
4050
4051 msg_len = ssl_get_hs_total_len( ssl );
4052 frag_off = ssl_get_hs_frag_off( ssl );
4053 frag_len = ssl_get_hs_frag_len( ssl );
4054
4055 if( frag_off > msg_len )
4056 return( -1 );
4057
4058 if( frag_len > msg_len - frag_off )
4059 return( -1 );
4060
4061 if( frag_len + 12 > ssl->in_msglen )
4062 return( -1 );
4063
4064 return( 0 );
4065}
4066
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004067/*
4068 * Mark bits in bitmask (used for DTLS HS reassembly)
4069 */
4070static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4071{
4072 unsigned int start_bits, end_bits;
4073
4074 start_bits = 8 - ( offset % 8 );
4075 if( start_bits != 8 )
4076 {
4077 size_t first_byte_idx = offset / 8;
4078
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004079 /* Special case */
4080 if( len <= start_bits )
4081 {
4082 for( ; len != 0; len-- )
4083 mask[first_byte_idx] |= 1 << ( start_bits - len );
4084
4085 /* Avoid potential issues with offset or len becoming invalid */
4086 return;
4087 }
4088
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004089 offset += start_bits; /* Now offset % 8 == 0 */
4090 len -= start_bits;
4091
4092 for( ; start_bits != 0; start_bits-- )
4093 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4094 }
4095
4096 end_bits = len % 8;
4097 if( end_bits != 0 )
4098 {
4099 size_t last_byte_idx = ( offset + len ) / 8;
4100
4101 len -= end_bits; /* Now len % 8 == 0 */
4102
4103 for( ; end_bits != 0; end_bits-- )
4104 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4105 }
4106
4107 memset( mask + offset / 8, 0xFF, len / 8 );
4108}
4109
4110/*
4111 * Check that bitmask is full
4112 */
4113static int ssl_bitmask_check( unsigned char *mask, size_t len )
4114{
4115 size_t i;
4116
4117 for( i = 0; i < len / 8; i++ )
4118 if( mask[i] != 0xFF )
4119 return( -1 );
4120
4121 for( i = 0; i < len % 8; i++ )
4122 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4123 return( -1 );
4124
4125 return( 0 );
4126}
4127
Hanno Becker56e205e2018-08-16 09:06:12 +01004128/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004129static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004130 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004131{
Hanno Becker56e205e2018-08-16 09:06:12 +01004132 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004133
Hanno Becker56e205e2018-08-16 09:06:12 +01004134 alloc_len = 12; /* Handshake header */
4135 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004136
Hanno Beckerd07df862018-08-16 09:14:58 +01004137 if( add_bitmap )
4138 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004139
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004140 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004141}
Hanno Becker56e205e2018-08-16 09:06:12 +01004142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004144
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004145static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004146{
4147 return( ( ssl->in_msg[1] << 16 ) |
4148 ( ssl->in_msg[2] << 8 ) |
4149 ssl->in_msg[3] );
4150}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004151
Simon Butcher99000142016-10-13 17:21:01 +01004152int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004154 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004157 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004159 }
4160
Hanno Becker12555c62018-08-16 12:47:53 +01004161 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004164 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004165 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004168 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004169 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004170 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004171 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004172
Hanno Becker44650b72018-08-16 12:51:11 +01004173 if( ssl_check_hs_header( ssl ) != 0 )
4174 {
4175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4176 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4177 }
4178
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004179 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004180 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4181 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4182 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4183 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004184 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004185 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4186 {
4187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4188 recv_msg_seq,
4189 ssl->handshake->in_msg_seq ) );
4190 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4191 }
4192
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004193 /* Retransmit only on last message from previous flight, to avoid
4194 * too many retransmissions.
4195 * Besides, No sane server ever retransmits HelloVerifyRequest */
4196 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004200 "message_seq = %d, start_of_flight = %d",
4201 recv_msg_seq,
4202 ssl->handshake->in_flight_start_seq ) );
4203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004207 return( ret );
4208 }
4209 }
4210 else
4211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004213 "message_seq = %d, expected = %d",
4214 recv_msg_seq,
4215 ssl->handshake->in_msg_seq ) );
4216 }
4217
Hanno Becker90333da2017-10-10 11:27:13 +01004218 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004219 }
4220 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004221
Hanno Becker6d97ef52018-08-16 13:09:04 +01004222 /* Message reassembly is handled alongside buffering of future
4223 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004224 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004225 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004226 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004229 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004230 }
4231 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004232 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004234#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004235 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004236 /* With TLS we don't handle fragmentation (for now) */
4237 if( ssl->in_msglen < ssl->in_hslen )
4238 {
4239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4240 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4241 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004242 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004243#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004244
Simon Butcher99000142016-10-13 17:21:01 +01004245 return( 0 );
4246}
4247
4248void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4249{
Hanno Becker0271f962018-08-16 13:23:47 +01004250 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004251
Hanno Becker0271f962018-08-16 13:23:47 +01004252 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004253 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004254 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004255 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004256
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004257 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004259 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004260 ssl->handshake != NULL )
4261 {
Hanno Becker0271f962018-08-16 13:23:47 +01004262 unsigned offset;
4263 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004264
Hanno Becker0271f962018-08-16 13:23:47 +01004265 /* Increment handshake sequence number */
4266 hs->in_msg_seq++;
4267
4268 /*
4269 * Clear up handshake buffering and reassembly structure.
4270 */
4271
4272 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004273 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004274
4275 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004276 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4277 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004278 offset++, hs_buf++ )
4279 {
4280 *hs_buf = *(hs_buf + 1);
4281 }
4282
4283 /* Create a fresh last entry */
4284 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004285 }
4286#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004287}
4288
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004289/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004290 * DTLS anti-replay: RFC 6347 4.1.2.6
4291 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004292 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4293 * Bit n is set iff record number in_window_top - n has been seen.
4294 *
4295 * Usually, in_window_top is the last record number seen and the lsb of
4296 * in_window is set. The only exception is the initial state (record number 0
4297 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4300static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004301{
4302 ssl->in_window_top = 0;
4303 ssl->in_window = 0;
4304}
4305
4306static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4307{
4308 return( ( (uint64_t) buf[0] << 40 ) |
4309 ( (uint64_t) buf[1] << 32 ) |
4310 ( (uint64_t) buf[2] << 24 ) |
4311 ( (uint64_t) buf[3] << 16 ) |
4312 ( (uint64_t) buf[4] << 8 ) |
4313 ( (uint64_t) buf[5] ) );
4314}
4315
4316/*
4317 * Return 0 if sequence number is acceptable, -1 otherwise
4318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004320{
4321 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4322 uint64_t bit;
4323
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004324 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004325 return( 0 );
4326
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004327 if( rec_seqnum > ssl->in_window_top )
4328 return( 0 );
4329
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004330 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004331
4332 if( bit >= 64 )
4333 return( -1 );
4334
4335 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4336 return( -1 );
4337
4338 return( 0 );
4339}
4340
4341/*
4342 * Update replay window on new validated record
4343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345{
4346 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4347
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004348 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004349 return;
4350
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004351 if( rec_seqnum > ssl->in_window_top )
4352 {
4353 /* Update window_top and the contents of the window */
4354 uint64_t shift = rec_seqnum - ssl->in_window_top;
4355
4356 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004357 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004358 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004359 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004360 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004361 ssl->in_window |= 1;
4362 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004363
4364 ssl->in_window_top = rec_seqnum;
4365 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004366 else
4367 {
4368 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004369 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004370
4371 if( bit < 64 ) /* Always true, but be extra sure */
4372 ssl->in_window |= (uint64_t) 1 << bit;
4373 }
4374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004376
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004377#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004378/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004379static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4380
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004381/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004382 * Without any SSL context, check if a datagram looks like a ClientHello with
4383 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004384 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004385 *
4386 * - if cookie is valid, return 0
4387 * - if ClientHello looks superficially valid but cookie is not,
4388 * fill obuf and set olen, then
4389 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4390 * - otherwise return a specific error code
4391 */
4392static int ssl_check_dtls_clihlo_cookie(
4393 mbedtls_ssl_cookie_write_t *f_cookie_write,
4394 mbedtls_ssl_cookie_check_t *f_cookie_check,
4395 void *p_cookie,
4396 const unsigned char *cli_id, size_t cli_id_len,
4397 const unsigned char *in, size_t in_len,
4398 unsigned char *obuf, size_t buf_len, size_t *olen )
4399{
4400 size_t sid_len, cookie_len;
4401 unsigned char *p;
4402
4403 if( f_cookie_write == NULL || f_cookie_check == NULL )
4404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4405
4406 /*
4407 * Structure of ClientHello with record and handshake headers,
4408 * and expected values. We don't need to check a lot, more checks will be
4409 * done when actually parsing the ClientHello - skipping those checks
4410 * avoids code duplication and does not make cookie forging any easier.
4411 *
4412 * 0-0 ContentType type; copied, must be handshake
4413 * 1-2 ProtocolVersion version; copied
4414 * 3-4 uint16 epoch; copied, must be 0
4415 * 5-10 uint48 sequence_number; copied
4416 * 11-12 uint16 length; (ignored)
4417 *
4418 * 13-13 HandshakeType msg_type; (ignored)
4419 * 14-16 uint24 length; (ignored)
4420 * 17-18 uint16 message_seq; copied
4421 * 19-21 uint24 fragment_offset; copied, must be 0
4422 * 22-24 uint24 fragment_length; (ignored)
4423 *
4424 * 25-26 ProtocolVersion client_version; (ignored)
4425 * 27-58 Random random; (ignored)
4426 * 59-xx SessionID session_id; 1 byte len + sid_len content
4427 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4428 * ...
4429 *
4430 * Minimum length is 61 bytes.
4431 */
4432 if( in_len < 61 ||
4433 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4434 in[3] != 0 || in[4] != 0 ||
4435 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4436 {
4437 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4438 }
4439
4440 sid_len = in[59];
4441 if( sid_len > in_len - 61 )
4442 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4443
4444 cookie_len = in[60 + sid_len];
4445 if( cookie_len > in_len - 60 )
4446 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4447
4448 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4449 cli_id, cli_id_len ) == 0 )
4450 {
4451 /* Valid cookie */
4452 return( 0 );
4453 }
4454
4455 /*
4456 * If we get here, we've got an invalid cookie, let's prepare HVR.
4457 *
4458 * 0-0 ContentType type; copied
4459 * 1-2 ProtocolVersion version; copied
4460 * 3-4 uint16 epoch; copied
4461 * 5-10 uint48 sequence_number; copied
4462 * 11-12 uint16 length; olen - 13
4463 *
4464 * 13-13 HandshakeType msg_type; hello_verify_request
4465 * 14-16 uint24 length; olen - 25
4466 * 17-18 uint16 message_seq; copied
4467 * 19-21 uint24 fragment_offset; copied
4468 * 22-24 uint24 fragment_length; olen - 25
4469 *
4470 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4471 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4472 *
4473 * Minimum length is 28.
4474 */
4475 if( buf_len < 28 )
4476 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4477
4478 /* Copy most fields and adapt others */
4479 memcpy( obuf, in, 25 );
4480 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4481 obuf[25] = 0xfe;
4482 obuf[26] = 0xff;
4483
4484 /* Generate and write actual cookie */
4485 p = obuf + 28;
4486 if( f_cookie_write( p_cookie,
4487 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4488 {
4489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4490 }
4491
4492 *olen = p - obuf;
4493
4494 /* Go back and fill length fields */
4495 obuf[27] = (unsigned char)( *olen - 28 );
4496
4497 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4498 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4499 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4500
4501 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4502 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4503
4504 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4505}
4506
4507/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004508 * Handle possible client reconnect with the same UDP quadruplet
4509 * (RFC 6347 Section 4.2.8).
4510 *
4511 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4512 * that looks like a ClientHello.
4513 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004514 * - if the input looks like a ClientHello without cookies,
4515 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004516 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004517 * - if the input looks like a ClientHello with a valid cookie,
4518 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004519 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004520 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004521 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004522 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004523 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4524 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004525 */
4526static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4527{
4528 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004529 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004530
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004531 ret = ssl_check_dtls_clihlo_cookie(
4532 ssl->conf->f_cookie_write,
4533 ssl->conf->f_cookie_check,
4534 ssl->conf->p_cookie,
4535 ssl->cli_id, ssl->cli_id_len,
4536 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004537 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004538
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004539 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4540
4541 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004542 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004543 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004544 * If the error is permanent we'll catch it later,
4545 * if it's not, then hopefully it'll work next time. */
4546 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4547
4548 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004549 }
4550
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004551 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004552 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004553 /* Got a valid cookie, partially reset context */
4554 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4555 {
4556 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4557 return( ret );
4558 }
4559
4560 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004561 }
4562
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004563 return( ret );
4564}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004565#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004566
Hanno Becker46483f12019-05-03 13:25:54 +01004567static int ssl_check_record_type( uint8_t record_type )
4568{
4569 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4570 record_type != MBEDTLS_SSL_MSG_ALERT &&
4571 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4572 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4573 {
4574 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4575 }
4576
4577 return( 0 );
4578}
4579
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004580/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004581 * ContentType type;
4582 * ProtocolVersion version;
4583 * uint16 epoch; // DTLS only
4584 * uint48 sequence_number; // DTLS only
4585 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004586 *
4587 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004588 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004589 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4590 *
4591 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004592 * 1. proceed with the record if this function returns 0
4593 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4594 * 3. return CLIENT_RECONNECT if this function return that value
4595 * 4. drop the whole datagram if this function returns anything else.
4596 * Point 2 is needed when the peer is resending, and we have already received
4597 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004600{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004601 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004602 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004603
Hanno Becker8b09b732019-05-08 12:03:28 +01004604 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004605
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004607 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004608
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004609 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004610#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004611 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004612 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4613 ssl->conf->cid_len != 0 )
4614 {
4615 /* Shift pointers to account for record header including CID
4616 * struct {
4617 * ContentType special_type = tls12_cid;
4618 * ProtocolVersion version;
4619 * uint16 epoch;
4620 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004621 * opaque cid[cid_length]; // Additional field compared to
4622 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004623 * uint16 length;
4624 * opaque enc_content[DTLSCiphertext.length];
4625 * } DTLSCiphertext;
4626 */
4627
4628 /* So far, we only support static CID lengths
4629 * fixed in the configuration. */
4630 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4631 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4632 }
4633 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004634#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004635 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004638
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004639#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004640 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004641 * Section 4.1.2.7, that is, send alert only with TLS */
4642 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4643 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004644 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4645 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004646 }
4647#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004650 }
4651
4652 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004653 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 }
4658
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004659 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4662 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 }
4664
Hanno Becker8b09b732019-05-08 12:03:28 +01004665 /* Now that the total length of the record header is known, ensure
4666 * that the current datagram is large enough to hold it.
4667 * This would fail, for example, if we received a datagram of
4668 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4669 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4670 if( ret != 0 )
4671 {
4672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4673 return( ret );
4674 }
4675 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4676
4677 /* Parse and validate record length
4678 * This must happen after the CID parsing because
4679 * its position in the record header depends on
4680 * the presence of a CID. */
4681
4682 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004683 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004684 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4687 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004688 }
4689
Hanno Becker8b09b732019-05-08 12:03:28 +01004690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004691 "version = [%d:%d], msglen = %d",
4692 ssl->in_msgtype,
4693 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004694
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004695 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004696 * DTLS-related tests.
4697 * Check epoch before checking length constraint because
4698 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4699 * message gets duplicated before the corresponding Finished message,
4700 * the second ChangeCipherSpec should be discarded because it belongs
4701 * to an old epoch, but not because its length is shorter than
4702 * the minimum record length for packets using the new record transform.
4703 * Note that these two kinds of failures are handled differently,
4704 * as an unexpected record is silently skipped but an invalid
4705 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004706 */
4707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004708 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004709 {
4710 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4711
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004712 /* Check epoch (and sequence number) with DTLS */
4713 if( rec_epoch != ssl->in_epoch )
4714 {
4715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4716 "expected %d, received %d",
4717 ssl->in_epoch, rec_epoch ) );
4718
4719#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4720 /*
4721 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4722 * access the first byte of record content (handshake type), as we
4723 * have an active transform (possibly iv_len != 0), so use the
4724 * fact that the record header len is 13 instead.
4725 */
4726 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4727 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4728 rec_epoch == 0 &&
4729 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4730 ssl->in_left > 13 &&
4731 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4732 {
4733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4734 "from the same port" ) );
4735 return( ssl_handle_possible_reconnect( ssl ) );
4736 }
4737 else
4738#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004739 {
4740 /* Consider buffering the record. */
4741 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4742 {
4743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4744 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4745 }
4746
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004747 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004748 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004749 }
4750
4751#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4752 /* Replay detection only works for the current epoch */
4753 if( rec_epoch == ssl->in_epoch &&
4754 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4755 {
4756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4757 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4758 }
4759#endif
4760 }
4761#endif /* MBEDTLS_SSL_PROTO_DTLS */
4762
Hanno Becker52c6dc62017-05-26 16:07:36 +01004763
4764 /* Check length against bounds of the current transform and version */
4765 if( ssl->transform_in == NULL )
4766 {
4767 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004768 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004769 {
4770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4771 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4772 }
4773 }
4774 else
4775 {
4776 if( ssl->in_msglen < ssl->transform_in->minlen )
4777 {
4778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4779 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4780 }
4781
4782#if defined(MBEDTLS_SSL_PROTO_SSL3)
4783 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004784 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004785 {
4786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4787 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4788 }
4789#endif
4790#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4791 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4792 /*
4793 * TLS encrypted messages can have up to 256 bytes of padding
4794 */
4795 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4796 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004797 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004798 {
4799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4800 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4801 }
4802#endif
4803 }
4804
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004805 return( 0 );
4806}
Paul Bakker5121ce52009-01-03 21:22:43 +00004807
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004808/*
4809 * If applicable, decrypt (and decompress) record content
4810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004812{
4813 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004816 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4819 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 ret = mbedtls_ssl_hw_record_read( ssl );
4824 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4827 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004828 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004829
4830 if( ret == 0 )
4831 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004832 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004834 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004835 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004836 mbedtls_record rec;
4837
4838 rec.buf = ssl->in_iv;
4839 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4840 - ( ssl->in_iv - ssl->in_buf );
4841 rec.data_len = ssl->in_msglen;
4842 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004843#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004844 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004845 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004846#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004847
4848 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4849 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4850 ssl->conf->transport, rec.ver );
4851 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004852 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4853 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004856
Hanno Beckera5a2b082019-05-15 14:03:01 +01004857#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004858 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4859 ssl->conf->ignore_unexpected_cid
4860 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4861 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004863 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004864 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004865#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004866
Paul Bakker5121ce52009-01-03 21:22:43 +00004867 return( ret );
4868 }
4869
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004870 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004871 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004872 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4873 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004875
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004876 /* The record content type may change during decryption,
4877 * so re-read it. */
4878 ssl->in_msgtype = rec.type;
4879 /* Also update the input buffer, because unfortunately
4880 * the server-side ssl_parse_client_hello() reparses the
4881 * record header when receiving a ClientHello initiating
4882 * a renegotiation. */
4883 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004884 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004885 ssl->in_msglen = rec.data_len;
4886 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4887 ssl->in_len[1] = (unsigned char)( rec.data_len );
4888
Paul Bakker5121ce52009-01-03 21:22:43 +00004889 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4890 ssl->in_msg, ssl->in_msglen );
4891
Hanno Beckera5a2b082019-05-15 14:03:01 +01004892#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004893 /* We have already checked the record content type
4894 * in ssl_parse_record_header(), failing or silently
4895 * dropping the record in the case of an unknown type.
4896 *
4897 * Since with the use of CIDs, the record content type
4898 * might change during decryption, re-check the record
4899 * content type, but treat a failure as fatal this time. */
4900 if( ssl_check_record_type( ssl->in_msgtype ) )
4901 {
4902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4903 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4904 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004905#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004906
Angus Grattond8213d02016-05-25 20:56:48 +10004907 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4910 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004911 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004912 else if( ssl->in_msglen == 0 )
4913 {
4914#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4915 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4916 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4917 {
4918 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4920 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4921 }
4922#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4923
4924 ssl->nb_zero++;
4925
4926 /*
4927 * Three or more empty messages may be a DoS attack
4928 * (excessive CPU consumption).
4929 */
4930 if( ssl->nb_zero > 3 )
4931 {
4932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004933 "messages, possible DoS attack" ) );
4934 /* Treat the records as if they were not properly authenticated,
4935 * thereby failing the connection if we see more than allowed
4936 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004937 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4938 }
4939 }
4940 else
4941 ssl->nb_zero = 0;
4942
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004943 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4944#if defined(MBEDTLS_SSL_PROTO_TLS)
4945 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004946 {
4947 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004948 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004949 if( ++ssl->in_ctr[i - 1] != 0 )
4950 break;
4951
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004952 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004953 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004954 {
4955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4956 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4957 }
4958 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004959#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004960
Paul Bakker5121ce52009-01-03 21:22:43 +00004961 }
4962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004964 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004966 {
4967 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004970 return( ret );
4971 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004976 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004979 }
4980#endif
4981
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004982 return( 0 );
4983}
4984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004986
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004987/*
4988 * Read a record.
4989 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004990 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4991 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4992 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004993 */
Hanno Becker1097b342018-08-15 14:09:41 +01004994
4995/* Helper functions for mbedtls_ssl_read_record(). */
4996static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004997static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4998static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004999
Hanno Becker327c93b2018-08-15 13:56:18 +01005000int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005001 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005002{
5003 int ret;
5004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005006
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005007 if( ssl->keep_current_message == 0 )
5008 {
5009 do {
Simon Butcher99000142016-10-13 17:21:01 +01005010
Hanno Becker26994592018-08-15 14:14:59 +01005011 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005012 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005013 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005014
Hanno Beckere74d5562018-08-15 14:26:08 +01005015 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005016 {
Hanno Becker40f50842018-08-15 14:48:01 +01005017#if defined(MBEDTLS_SSL_PROTO_DTLS)
5018 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005019
Hanno Becker40f50842018-08-15 14:48:01 +01005020 /* We only check for buffered messages if the
5021 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005022 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005023 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005024 {
Hanno Becker40f50842018-08-15 14:48:01 +01005025 if( ssl_load_buffered_message( ssl ) == 0 )
5026 have_buffered = 1;
5027 }
5028
5029 if( have_buffered == 0 )
5030#endif /* MBEDTLS_SSL_PROTO_DTLS */
5031 {
5032 ret = ssl_get_next_record( ssl );
5033 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5034 continue;
5035
5036 if( ret != 0 )
5037 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005038 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005039 return( ret );
5040 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005041 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005042 }
5043
5044 ret = mbedtls_ssl_handle_message_type( ssl );
5045
Hanno Becker40f50842018-08-15 14:48:01 +01005046#if defined(MBEDTLS_SSL_PROTO_DTLS)
5047 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5048 {
5049 /* Buffer future message */
5050 ret = ssl_buffer_message( ssl );
5051 if( ret != 0 )
5052 return( ret );
5053
5054 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5055 }
5056#endif /* MBEDTLS_SSL_PROTO_DTLS */
5057
Hanno Becker90333da2017-10-10 11:27:13 +01005058 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5059 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005060
5061 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005062 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005063 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005064 return( ret );
5065 }
5066
Hanno Becker327c93b2018-08-15 13:56:18 +01005067 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005068 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005069 {
5070 mbedtls_ssl_update_handshake_status( ssl );
5071 }
Simon Butcher99000142016-10-13 17:21:01 +01005072 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005073 else
Simon Butcher99000142016-10-13 17:21:01 +01005074 {
Hanno Becker02f59072018-08-15 14:00:24 +01005075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005076 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005077 }
5078
5079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5080
5081 return( 0 );
5082}
5083
Hanno Becker40f50842018-08-15 14:48:01 +01005084#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005085static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005086{
Hanno Becker40f50842018-08-15 14:48:01 +01005087 if( ssl->in_left > ssl->next_record_offset )
5088 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005089
Hanno Becker40f50842018-08-15 14:48:01 +01005090 return( 0 );
5091}
5092
5093static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5094{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005095 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005096 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005097 int ret = 0;
5098
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005099 if( hs == NULL )
5100 return( -1 );
5101
Hanno Beckere00ae372018-08-20 09:39:42 +01005102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5103
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005104 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5105 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5106 {
5107 /* Check if we have seen a ChangeCipherSpec before.
5108 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005109 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005110 {
5111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5112 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005113 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005114 }
5115
Hanno Becker39b8bc92018-08-28 17:17:13 +01005116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005117 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5118 ssl->in_msglen = 1;
5119 ssl->in_msg[0] = 1;
5120
5121 /* As long as they are equal, the exact value doesn't matter. */
5122 ssl->in_left = 0;
5123 ssl->next_record_offset = 0;
5124
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005125 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005126 goto exit;
5127 }
Hanno Becker37f95322018-08-16 13:55:32 +01005128
Hanno Beckerb8f50142018-08-28 10:01:34 +01005129#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005130 /* Debug only */
5131 {
5132 unsigned offset;
5133 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5134 {
5135 hs_buf = &hs->buffering.hs[offset];
5136 if( hs_buf->is_valid == 1 )
5137 {
5138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5139 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005140 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005141 }
5142 }
5143 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005144#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005145
5146 /* Check if we have buffered and/or fully reassembled the
5147 * next handshake message. */
5148 hs_buf = &hs->buffering.hs[0];
5149 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5150 {
5151 /* Synthesize a record containing the buffered HS message. */
5152 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5153 ( hs_buf->data[2] << 8 ) |
5154 hs_buf->data[3];
5155
5156 /* Double-check that we haven't accidentally buffered
5157 * a message that doesn't fit into the input buffer. */
5158 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5159 {
5160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5162 }
5163
5164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5165 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5166 hs_buf->data, msg_len + 12 );
5167
5168 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5169 ssl->in_hslen = msg_len + 12;
5170 ssl->in_msglen = msg_len + 12;
5171 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5172
5173 ret = 0;
5174 goto exit;
5175 }
5176 else
5177 {
5178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5179 hs->in_msg_seq ) );
5180 }
5181
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005182 ret = -1;
5183
5184exit:
5185
5186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5187 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005188}
5189
Hanno Beckera02b0b42018-08-21 17:20:27 +01005190static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5191 size_t desired )
5192{
5193 int offset;
5194 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5196 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005197
Hanno Becker01315ea2018-08-21 17:22:17 +01005198 /* Get rid of future records epoch first, if such exist. */
5199 ssl_free_buffered_record( ssl );
5200
5201 /* Check if we have enough space available now. */
5202 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5203 hs->buffering.total_bytes_buffered ) )
5204 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005206 return( 0 );
5207 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005208
Hanno Becker4f432ad2018-08-28 10:02:32 +01005209 /* We don't have enough space to buffer the next expected handshake
5210 * message. Remove buffers used for future messages to gain space,
5211 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005212 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5213 offset >= 0; offset-- )
5214 {
5215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5216 offset ) );
5217
Hanno Beckerb309b922018-08-23 13:18:05 +01005218 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005219
5220 /* Check if we have enough space available now. */
5221 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5222 hs->buffering.total_bytes_buffered ) )
5223 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005225 return( 0 );
5226 }
5227 }
5228
5229 return( -1 );
5230}
5231
Hanno Becker40f50842018-08-15 14:48:01 +01005232static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5233{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005234 int ret = 0;
5235 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5236
5237 if( hs == NULL )
5238 return( 0 );
5239
5240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5241
5242 switch( ssl->in_msgtype )
5243 {
5244 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005246
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005247 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005248 break;
5249
5250 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005251 {
5252 unsigned recv_msg_seq_offset;
5253 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5254 mbedtls_ssl_hs_buffer *hs_buf;
5255 size_t msg_len = ssl->in_hslen - 12;
5256
5257 /* We should never receive an old handshake
5258 * message - double-check nonetheless. */
5259 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5260 {
5261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5263 }
5264
5265 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5266 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5267 {
5268 /* Silently ignore -- message too far in the future */
5269 MBEDTLS_SSL_DEBUG_MSG( 2,
5270 ( "Ignore future HS message with sequence number %u, "
5271 "buffering window %u - %u",
5272 recv_msg_seq, ssl->handshake->in_msg_seq,
5273 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5274
5275 goto exit;
5276 }
5277
5278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5279 recv_msg_seq, recv_msg_seq_offset ) );
5280
5281 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5282
5283 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005284 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005285 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005286 size_t reassembly_buf_sz;
5287
Hanno Becker37f95322018-08-16 13:55:32 +01005288 hs_buf->is_fragmented =
5289 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5290
5291 /* We copy the message back into the input buffer
5292 * after reassembly, so check that it's not too large.
5293 * This is an implementation-specific limitation
5294 * and not one from the standard, hence it is not
5295 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005296 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005297 {
5298 /* Ignore message */
5299 goto exit;
5300 }
5301
Hanno Beckere0b150f2018-08-21 15:51:03 +01005302 /* Check if we have enough space to buffer the message. */
5303 if( hs->buffering.total_bytes_buffered >
5304 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5305 {
5306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5308 }
5309
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005310 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5311 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005312
5313 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5314 hs->buffering.total_bytes_buffered ) )
5315 {
5316 if( recv_msg_seq_offset > 0 )
5317 {
5318 /* If we can't buffer a future message because
5319 * of space limitations -- ignore. */
5320 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",
5321 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5322 (unsigned) hs->buffering.total_bytes_buffered ) );
5323 goto exit;
5324 }
Hanno Beckere1801392018-08-21 16:51:05 +01005325 else
5326 {
5327 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",
5328 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5329 (unsigned) hs->buffering.total_bytes_buffered ) );
5330 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005331
Hanno Beckera02b0b42018-08-21 17:20:27 +01005332 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005333 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005334 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",
5335 (unsigned) msg_len,
5336 (unsigned) reassembly_buf_sz,
5337 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005338 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005339 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5340 goto exit;
5341 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005342 }
5343
5344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5345 msg_len ) );
5346
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005347 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5348 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005349 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005350 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005351 goto exit;
5352 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005353 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005354
5355 /* Prepare final header: copy msg_type, length and message_seq,
5356 * then add standardised fragment_offset and fragment_length */
5357 memcpy( hs_buf->data, ssl->in_msg, 6 );
5358 memset( hs_buf->data + 6, 0, 3 );
5359 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5360
5361 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005362
5363 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005364 }
5365 else
5366 {
5367 /* Make sure msg_type and length are consistent */
5368 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5369 {
5370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5371 /* Ignore */
5372 goto exit;
5373 }
5374 }
5375
Hanno Becker4422bbb2018-08-20 09:40:19 +01005376 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005377 {
5378 size_t frag_len, frag_off;
5379 unsigned char * const msg = hs_buf->data + 12;
5380
5381 /*
5382 * Check and copy current fragment
5383 */
5384
5385 /* Validation of header fields already done in
5386 * mbedtls_ssl_prepare_handshake_record(). */
5387 frag_off = ssl_get_hs_frag_off( ssl );
5388 frag_len = ssl_get_hs_frag_len( ssl );
5389
5390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5391 frag_off, frag_len ) );
5392 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5393
5394 if( hs_buf->is_fragmented )
5395 {
5396 unsigned char * const bitmask = msg + msg_len;
5397 ssl_bitmask_set( bitmask, frag_off, frag_len );
5398 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5399 msg_len ) == 0 );
5400 }
5401 else
5402 {
5403 hs_buf->is_complete = 1;
5404 }
5405
5406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5407 hs_buf->is_complete ? "" : "not yet " ) );
5408 }
5409
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005410 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005411 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005412
5413 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005414 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005415 break;
5416 }
5417
5418exit:
5419
5420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5421 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005422}
5423#endif /* MBEDTLS_SSL_PROTO_DTLS */
5424
Hanno Becker1097b342018-08-15 14:09:41 +01005425static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005426{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005427 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005428 * Consume last content-layer message and potentially
5429 * update in_msglen which keeps track of the contents'
5430 * consumption state.
5431 *
5432 * (1) Handshake messages:
5433 * Remove last handshake message, move content
5434 * and adapt in_msglen.
5435 *
5436 * (2) Alert messages:
5437 * Consume whole record content, in_msglen = 0.
5438 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005439 * (3) Change cipher spec:
5440 * Consume whole record content, in_msglen = 0.
5441 *
5442 * (4) Application data:
5443 * Don't do anything - the record layer provides
5444 * the application data as a stream transport
5445 * and consumes through mbedtls_ssl_read only.
5446 *
5447 */
5448
5449 /* Case (1): Handshake messages */
5450 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005451 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005452 /* Hard assertion to be sure that no application data
5453 * is in flight, as corrupting ssl->in_msglen during
5454 * ssl->in_offt != NULL is fatal. */
5455 if( ssl->in_offt != NULL )
5456 {
5457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5459 }
5460
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005461 /*
5462 * Get next Handshake message in the current record
5463 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005464
Hanno Becker4a810fb2017-05-24 16:27:30 +01005465 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005466 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005467 * current handshake content: If DTLS handshake
5468 * fragmentation is used, that's the fragment
5469 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005470 * size here is faulty and should be changed at
5471 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005472 * (2) While it doesn't seem to cause problems, one
5473 * has to be very careful not to assume that in_hslen
5474 * is always <= in_msglen in a sensible communication.
5475 * Again, it's wrong for DTLS handshake fragmentation.
5476 * The following check is therefore mandatory, and
5477 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005478 * Additionally, ssl->in_hslen might be arbitrarily out of
5479 * bounds after handling a DTLS message with an unexpected
5480 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005481 */
5482 if( ssl->in_hslen < ssl->in_msglen )
5483 {
5484 ssl->in_msglen -= ssl->in_hslen;
5485 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5486 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005487
Hanno Becker4a810fb2017-05-24 16:27:30 +01005488 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5489 ssl->in_msg, ssl->in_msglen );
5490 }
5491 else
5492 {
5493 ssl->in_msglen = 0;
5494 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005495
Hanno Becker4a810fb2017-05-24 16:27:30 +01005496 ssl->in_hslen = 0;
5497 }
5498 /* Case (4): Application data */
5499 else if( ssl->in_offt != NULL )
5500 {
5501 return( 0 );
5502 }
5503 /* Everything else (CCS & Alerts) */
5504 else
5505 {
5506 ssl->in_msglen = 0;
5507 }
5508
Hanno Becker1097b342018-08-15 14:09:41 +01005509 return( 0 );
5510}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005511
Hanno Beckere74d5562018-08-15 14:26:08 +01005512static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5513{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005514 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005515 return( 1 );
5516
5517 return( 0 );
5518}
5519
Hanno Becker5f066e72018-08-16 14:56:31 +01005520#if defined(MBEDTLS_SSL_PROTO_DTLS)
5521
5522static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5523{
5524 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5525 if( hs == NULL )
5526 return;
5527
Hanno Becker01315ea2018-08-21 17:22:17 +01005528 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005529 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005530 hs->buffering.total_bytes_buffered -=
5531 hs->buffering.future_record.len;
5532
5533 mbedtls_free( hs->buffering.future_record.data );
5534 hs->buffering.future_record.data = NULL;
5535 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005536}
5537
5538static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5539{
5540 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5541 unsigned char * rec;
5542 size_t rec_len;
5543 unsigned rec_epoch;
5544
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005545 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005546 return( 0 );
5547
5548 if( hs == NULL )
5549 return( 0 );
5550
Hanno Becker5f066e72018-08-16 14:56:31 +01005551 rec = hs->buffering.future_record.data;
5552 rec_len = hs->buffering.future_record.len;
5553 rec_epoch = hs->buffering.future_record.epoch;
5554
5555 if( rec == NULL )
5556 return( 0 );
5557
Hanno Becker4cb782d2018-08-20 11:19:05 +01005558 /* Only consider loading future records if the
5559 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005560 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005561 return( 0 );
5562
Hanno Becker5f066e72018-08-16 14:56:31 +01005563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5564
5565 if( rec_epoch != ssl->in_epoch )
5566 {
5567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5568 goto exit;
5569 }
5570
5571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5572
5573 /* Double-check that the record is not too large */
5574 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5575 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5576 {
5577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5579 }
5580
5581 memcpy( ssl->in_hdr, rec, rec_len );
5582 ssl->in_left = rec_len;
5583 ssl->next_record_offset = 0;
5584
5585 ssl_free_buffered_record( ssl );
5586
5587exit:
5588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5589 return( 0 );
5590}
5591
5592static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5593{
5594 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5595 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005596 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005597
5598 /* Don't buffer future records outside handshakes. */
5599 if( hs == NULL )
5600 return( 0 );
5601
5602 /* Only buffer handshake records (we are only interested
5603 * in Finished messages). */
5604 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5605 return( 0 );
5606
5607 /* Don't buffer more than one future epoch record. */
5608 if( hs->buffering.future_record.data != NULL )
5609 return( 0 );
5610
Hanno Becker01315ea2018-08-21 17:22:17 +01005611 /* Don't buffer record if there's not enough buffering space remaining. */
5612 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5613 hs->buffering.total_bytes_buffered ) )
5614 {
5615 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",
5616 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5617 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005618 return( 0 );
5619 }
5620
Hanno Becker5f066e72018-08-16 14:56:31 +01005621 /* Buffer record */
5622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5623 ssl->in_epoch + 1 ) );
5624 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5625 rec_hdr_len + ssl->in_msglen );
5626
5627 /* ssl_parse_record_header() only considers records
5628 * of the next epoch as candidates for buffering. */
5629 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005630 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005631
5632 hs->buffering.future_record.data =
5633 mbedtls_calloc( 1, hs->buffering.future_record.len );
5634 if( hs->buffering.future_record.data == NULL )
5635 {
5636 /* If we run out of RAM trying to buffer a
5637 * record from the next epoch, just ignore. */
5638 return( 0 );
5639 }
5640
Hanno Becker01315ea2018-08-21 17:22:17 +01005641 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005642
Hanno Becker01315ea2018-08-21 17:22:17 +01005643 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005644 return( 0 );
5645}
5646
5647#endif /* MBEDTLS_SSL_PROTO_DTLS */
5648
Hanno Beckere74d5562018-08-15 14:26:08 +01005649static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005650{
5651 int ret;
5652
Hanno Becker5f066e72018-08-16 14:56:31 +01005653#if defined(MBEDTLS_SSL_PROTO_DTLS)
5654 /* We might have buffered a future record; if so,
5655 * and if the epoch matches now, load it.
5656 * On success, this call will set ssl->in_left to
5657 * the length of the buffered record, so that
5658 * the calls to ssl_fetch_input() below will
5659 * essentially be no-ops. */
5660 ret = ssl_load_buffered_record( ssl );
5661 if( ret != 0 )
5662 return( ret );
5663#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005664
Hanno Becker8b09b732019-05-08 12:03:28 +01005665 /* Reset in pointers to default state for TLS/DTLS records,
5666 * assuming no CID and no offset between record content and
5667 * record plaintext. */
5668 ssl_update_in_pointers( ssl );
5669
5670 /* Ensure that we have enough space available for the default form
5671 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5672 * with no space for CIDs counted in). */
5673 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5674 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005677 return( ret );
5678 }
5679
5680 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005683 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005684 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005685 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005686 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5687 {
5688 ret = ssl_buffer_future_record( ssl );
5689 if( ret != 0 )
5690 return( ret );
5691
5692 /* Fall through to handling of unexpected records */
5693 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5694 }
5695
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005696 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5697 {
5698 /* Skip unexpected record (but not whole datagram) */
5699 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005700 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005701
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5703 "(header)" ) );
5704 }
5705 else
5706 {
5707 /* Skip invalid record and the rest of the datagram */
5708 ssl->next_record_offset = 0;
5709 ssl->in_left = 0;
5710
5711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5712 "(header)" ) );
5713 }
5714
5715 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005716 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005717 }
5718#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005719 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005720 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005721
5722 /*
5723 * Read and optionally decrypt the message contents
5724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005726 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005729 return( ret );
5730 }
5731
5732 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005734 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005735 {
Hanno Becker43395762019-05-03 14:46:38 +01005736 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005737 if( ssl->next_record_offset < ssl->in_left )
5738 {
5739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5740 }
5741 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005742 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005743#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005744#if defined(MBEDTLS_SSL_PROTO_TLS)
5745 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005746 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005747 }
5748#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005749
5750 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005753 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005754 {
5755 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005756 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005757 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005758 /* Except when waiting for Finished as a bad mac here
5759 * probably means something went wrong in the handshake
5760 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5761 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5762 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5763 {
5764#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5765 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5766 {
5767 mbedtls_ssl_send_alert_message( ssl,
5768 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5769 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5770 }
5771#endif
5772 return( ret );
5773 }
5774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005776 if( ssl->conf->badmac_limit != 0 &&
5777 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5780 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005781 }
5782#endif
5783
Hanno Becker4a810fb2017-05-24 16:27:30 +01005784 /* As above, invalid records cause
5785 * dismissal of the whole datagram. */
5786
5787 ssl->next_record_offset = 0;
5788 ssl->in_left = 0;
5789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005791 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005792 }
5793
5794 return( ret );
5795 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005796 MBEDTLS_SSL_TRANSPORT_ELSE
5797#endif /* MBEDTLS_SSL_PROTO_DTLS */
5798#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005799 {
5800 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5802 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 mbedtls_ssl_send_alert_message( ssl,
5805 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5806 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005807 }
5808#endif
5809 return( ret );
5810 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005811#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005812 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005813
Simon Butcher99000142016-10-13 17:21:01 +01005814 return( 0 );
5815}
5816
5817int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5818{
5819 int ret;
5820
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005821 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005822 * Handle particular types of records
5823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 {
Simon Butcher99000142016-10-13 17:21:01 +01005826 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5827 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005828 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005829 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005830 }
5831
Hanno Beckere678eaa2018-08-21 14:57:46 +01005832 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005833 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005834 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005835 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5837 ssl->in_msglen ) );
5838 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005839 }
5840
Hanno Beckere678eaa2018-08-21 14:57:46 +01005841 if( ssl->in_msg[0] != 1 )
5842 {
5843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5844 ssl->in_msg[0] ) );
5845 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5846 }
5847
5848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005849 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005850 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5851 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5852 {
5853 if( ssl->handshake == NULL )
5854 {
5855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5856 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5857 }
5858
5859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5860 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5861 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005862#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005863 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005866 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005867 if( ssl->in_msglen != 2 )
5868 {
5869 /* Note: Standard allows for more than one 2 byte alert
5870 to be packed in a single message, but Mbed TLS doesn't
5871 currently support this. */
5872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5873 ssl->in_msglen ) );
5874 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5875 }
5876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005878 ssl->in_msg[0], ssl->in_msg[1] ) );
5879
5880 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005881 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005886 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005888 }
5889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5891 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5894 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005896
5897#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5898 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5899 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5900 {
Hanno Becker90333da2017-10-10 11:27:13 +01005901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005902 /* Will be handled when trying to parse ServerHello */
5903 return( 0 );
5904 }
5905#endif
5906
5907#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5908 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5909 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5910 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5911 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5912 {
5913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5914 /* Will be handled in mbedtls_ssl_parse_certificate() */
5915 return( 0 );
5916 }
5917#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5918
5919 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005920 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005921 }
5922
Hanno Beckerc76c6192017-06-06 10:03:17 +01005923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005924 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005925 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005926 /* Drop unexpected ApplicationData records,
5927 * except at the beginning of renegotiations */
5928 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5929 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5930#if defined(MBEDTLS_SSL_RENEGOTIATION)
5931 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5932 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005933#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005934 )
5935 {
5936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5937 return( MBEDTLS_ERR_SSL_NON_FATAL );
5938 }
5939
5940 if( ssl->handshake != NULL &&
5941 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5942 {
5943 ssl_handshake_wrapup_free_hs_transform( ssl );
5944 }
5945 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005946#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005947
Paul Bakker5121ce52009-01-03 21:22:43 +00005948 return( 0 );
5949}
5950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005952{
5953 int ret;
5954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5956 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5957 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005958 {
5959 return( ret );
5960 }
5961
5962 return( 0 );
5963}
5964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005966 unsigned char level,
5967 unsigned char message )
5968{
5969 int ret;
5970
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005971 if( ssl == NULL || ssl->conf == NULL )
5972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005978 ssl->out_msglen = 2;
5979 ssl->out_msg[0] = level;
5980 ssl->out_msg[1] = message;
5981
Hanno Becker67bc7c32018-08-06 11:33:50 +01005982 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005985 return( ret );
5986 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005988
5989 return( 0 );
5990}
5991
Hanno Becker17572472019-02-08 07:19:04 +00005992#if defined(MBEDTLS_X509_CRT_PARSE_C)
5993static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5994{
5995#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5996 if( session->peer_cert != NULL )
5997 {
5998 mbedtls_x509_crt_free( session->peer_cert );
5999 mbedtls_free( session->peer_cert );
6000 session->peer_cert = NULL;
6001 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006002#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006003 if( session->peer_cert_digest != NULL )
6004 {
6005 /* Zeroization is not necessary. */
6006 mbedtls_free( session->peer_cert_digest );
6007 session->peer_cert_digest = NULL;
6008 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6009 session->peer_cert_digest_len = 0;
6010 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006011#else
6012 ((void) session);
6013#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006014}
6015#endif /* MBEDTLS_X509_CRT_PARSE_C */
6016
Paul Bakker5121ce52009-01-03 21:22:43 +00006017/*
6018 * Handshake functions
6019 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006020#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006021/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006023{
Hanno Becker8759e162017-12-27 21:34:08 +00006024 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006027
Hanno Becker5097cba2019-02-05 13:36:46 +00006028 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006031 ssl->state++;
6032 return( 0 );
6033 }
6034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006037}
6038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040{
Hanno Becker8759e162017-12-27 21:34:08 +00006041 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006044
Hanno Becker5097cba2019-02-05 13:36:46 +00006045 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006048 ssl->state++;
6049 return( 0 );
6050 }
6051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006054}
Gilles Peskinef9828522017-05-03 12:28:43 +02006055
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006056#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006057/* Some certificate support -> implement write and parse */
6058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006062 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006064 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006067
Hanno Becker5097cba2019-02-05 13:36:46 +00006068 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006071 ssl->state++;
6072 return( 0 );
6073 }
6074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006076 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 {
6078 if( ssl->client_auth == 0 )
6079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081 ssl->state++;
6082 return( 0 );
6083 }
6084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006086 /*
6087 * If using SSLv3 and got no cert, send an Alert message
6088 * (otherwise an empty Certificate message will be sent).
6089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6091 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 {
6093 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6095 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6096 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 goto write_msg;
6100 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103#endif /* MBEDTLS_SSL_CLI_C */
6104#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006105 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6110 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 }
6112 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006113#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006116
6117 /*
6118 * 0 . 0 handshake type
6119 * 1 . 3 handshake length
6120 * 4 . 6 length of all certs
6121 * 7 . 9 length of cert. 1
6122 * 10 . n-1 peer certificate
6123 * n . n+2 length of cert. 2
6124 * n+3 . ... upper level cert, etc.
6125 */
6126 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006128
Paul Bakker29087132010-03-21 21:03:34 +00006129 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 {
6131 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006132 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006135 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137 }
6138
6139 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6140 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6141 ssl->out_msg[i + 2] = (unsigned char)( n );
6142
6143 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6144 i += n; crt = crt->next;
6145 }
6146
6147 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6148 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6149 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6150
6151 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6153 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006154
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006155#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006156write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006157#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006158
6159 ssl->state++;
6160
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006161 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006162 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006164 return( ret );
6165 }
6166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006168
Paul Bakkered27a042013-04-18 22:46:23 +02006169 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006170}
6171
Hanno Becker285ff0c2019-01-31 07:44:03 +00006172#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006173
6174#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006175static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6176 unsigned char *crt_buf,
6177 size_t crt_buf_len )
6178{
6179 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6180
6181 if( peer_crt == NULL )
6182 return( -1 );
6183
6184 if( peer_crt->raw.len != crt_buf_len )
6185 return( -1 );
6186
Hanno Becker68b856d2019-02-08 14:00:04 +00006187 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006188}
Hanno Becker5882dd02019-06-06 16:25:57 +01006189#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006190static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6191 unsigned char *crt_buf,
6192 size_t crt_buf_len )
6193{
6194 int ret;
6195 unsigned char const * const peer_cert_digest =
6196 ssl->session->peer_cert_digest;
6197 mbedtls_md_type_t const peer_cert_digest_type =
6198 ssl->session->peer_cert_digest_type;
6199 mbedtls_md_info_t const * const digest_info =
6200 mbedtls_md_info_from_type( peer_cert_digest_type );
6201 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6202 size_t digest_len;
6203
6204 if( peer_cert_digest == NULL || digest_info == NULL )
6205 return( -1 );
6206
6207 digest_len = mbedtls_md_get_size( digest_info );
6208 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6209 return( -1 );
6210
6211 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6212 if( ret != 0 )
6213 return( -1 );
6214
6215 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6216}
Hanno Becker5882dd02019-06-06 16:25:57 +01006217#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006218#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006219
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006220/*
6221 * Once the certificate message is read, parse it into a cert chain and
6222 * perform basic checks, but leave actual verification to the caller
6223 */
Hanno Becker35e41772019-02-05 15:37:23 +00006224static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6225 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006226{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006227 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006228#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6229 int crt_cnt=0;
6230#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006231 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006232 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006237 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6238 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006240 }
6241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6243 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006246 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6247 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006249 }
6250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006252
Paul Bakker5121ce52009-01-03 21:22:43 +00006253 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006256 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006257
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006258 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006262 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6263 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006265 }
6266
Hanno Becker33c3dc82019-01-30 14:46:46 +00006267 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6268 i += 3;
6269
Hanno Becker33c3dc82019-01-30 14:46:46 +00006270 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006271 while( i < ssl->in_hslen )
6272 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006273 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006274 if ( i + 3 > ssl->in_hslen ) {
6275 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 );
Philippe Antoine747fd532018-05-30 09:13:21 +02006279 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6280 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006281 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6282 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006283 if( ssl->in_msg[i] != 0 )
6284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006286 mbedtls_ssl_send_alert_message( ssl,
6287 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6288 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006290 }
6291
Hanno Becker33c3dc82019-01-30 14:46:46 +00006292 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6294 | (unsigned int) ssl->in_msg[i + 2];
6295 i += 3;
6296
6297 if( n < 128 || i + n > ssl->in_hslen )
6298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006300 mbedtls_ssl_send_alert_message( ssl,
6301 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6302 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006304 }
6305
Hanno Becker33c3dc82019-01-30 14:46:46 +00006306 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006307#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6308 if( crt_cnt++ == 0 &&
6309 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6310 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006311 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006312 /* During client-side renegotiation, check that the server's
6313 * end-CRTs hasn't changed compared to the initial handshake,
6314 * mitigating the triple handshake attack. On success, reuse
6315 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6317 if( ssl_check_peer_crt_unchanged( ssl,
6318 &ssl->in_msg[i],
6319 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006320 {
Hanno Becker35e41772019-02-05 15:37:23 +00006321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6322 mbedtls_ssl_send_alert_message( ssl,
6323 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6324 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6325 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006326 }
Hanno Becker35e41772019-02-05 15:37:23 +00006327
6328 /* Now we can safely free the original chain. */
6329 ssl_clear_peer_cert( ssl->session );
6330 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006331#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6332
Hanno Becker33c3dc82019-01-30 14:46:46 +00006333 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006334#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006335 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006336#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006337 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006338 * it in-place from the input buffer instead of making a copy. */
6339 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6340#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006341 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006342 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006343 case 0: /*ok*/
6344 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6345 /* Ignore certificate with an unknown algorithm: maybe a
6346 prior certificate was already trusted. */
6347 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006348
Hanno Becker33c3dc82019-01-30 14:46:46 +00006349 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6350 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6351 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006352
Hanno Becker33c3dc82019-01-30 14:46:46 +00006353 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6354 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6355 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006356
Hanno Becker33c3dc82019-01-30 14:46:46 +00006357 default:
6358 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6359 crt_parse_der_failed:
6360 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6361 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6362 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006363 }
6364
6365 i += n;
6366 }
6367
Hanno Becker35e41772019-02-05 15:37:23 +00006368 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006369 return( 0 );
6370}
6371
Hanno Beckerb8a08572019-02-05 12:49:06 +00006372#if defined(MBEDTLS_SSL_SRV_C)
6373static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6374{
6375 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6376 return( -1 );
6377
6378#if defined(MBEDTLS_SSL_PROTO_SSL3)
6379 /*
6380 * Check if the client sent an empty certificate
6381 */
6382 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6383 {
6384 if( ssl->in_msglen == 2 &&
6385 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6386 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6387 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6388 {
6389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6390 return( 0 );
6391 }
6392
6393 return( -1 );
6394 }
6395#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6396
6397#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6398 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6399 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6400 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6401 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6402 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6403 {
6404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6405 return( 0 );
6406 }
6407
Hanno Beckerb8a08572019-02-05 12:49:06 +00006408#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6409 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01006410
6411 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00006412}
6413#endif /* MBEDTLS_SSL_SRV_C */
6414
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006415/* Check if a certificate message is expected.
6416 * Return either
6417 * - SSL_CERTIFICATE_EXPECTED, or
6418 * - SSL_CERTIFICATE_SKIP
6419 * indicating whether a Certificate message is expected or not.
6420 */
6421#define SSL_CERTIFICATE_EXPECTED 0
6422#define SSL_CERTIFICATE_SKIP 1
6423static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6424 int authmode )
6425{
6426 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6427 ssl->handshake->ciphersuite_info;
6428
6429 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6430 return( SSL_CERTIFICATE_SKIP );
6431
6432#if defined(MBEDTLS_SSL_SRV_C)
6433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6434 {
6435 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6436 return( SSL_CERTIFICATE_SKIP );
6437
6438 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6439 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006440 ssl->session_negotiate->verify_result =
6441 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6442 return( SSL_CERTIFICATE_SKIP );
6443 }
6444 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006445#else
6446 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006447#endif /* MBEDTLS_SSL_SRV_C */
6448
6449 return( SSL_CERTIFICATE_EXPECTED );
6450}
6451
Hanno Becker3cf50612019-02-05 14:36:34 +00006452static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6453 int authmode,
6454 mbedtls_x509_crt *chain,
6455 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006456{
Hanno Becker613d4902019-02-05 13:11:17 +00006457 int ret = 0;
Hanno Becker8c13ee62019-02-26 16:48:17 +00006458 int verify_ret;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006459 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6460 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006461 mbedtls_x509_crt *ca_chain;
6462 mbedtls_x509_crl *ca_crl;
6463
6464 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6465 return( 0 );
6466
6467#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6468 if( ssl->handshake->sni_ca_chain != NULL )
6469 {
6470 ca_chain = ssl->handshake->sni_ca_chain;
6471 ca_crl = ssl->handshake->sni_ca_crl;
6472 }
6473 else
6474#endif
6475 {
6476 ca_chain = ssl->conf->ca_chain;
6477 ca_crl = ssl->conf->ca_crl;
6478 }
6479
6480 /*
6481 * Main check: verify certificate
6482 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00006483 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00006484 chain,
6485 ca_chain, ca_crl,
6486 ssl->conf->cert_profile,
6487 ssl->hostname,
6488 &ssl->session_negotiate->verify_result,
6489 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6490
Hanno Becker8c13ee62019-02-26 16:48:17 +00006491 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00006492 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00006493 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00006494 }
6495
6496#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00006497 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00006498 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6499#endif
6500
6501 /*
6502 * Secondary checks: always done, but change 'ret' only if it was 0
6503 */
6504
6505#if defined(MBEDTLS_ECP_C)
6506 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00006507 mbedtls_pk_context *pk;
6508 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
6509 if( ret != 0 )
6510 return( ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00006511
6512 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00006513 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
6514 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
6515
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00006516 mbedtls_x509_crt_pk_release( chain );
Hanno Becker8c13ee62019-02-26 16:48:17 +00006517
6518 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00006519 {
6520 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6521
6522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00006523 if( verify_ret == 0 )
6524 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00006525 }
6526 }
6527#endif /* MBEDTLS_ECP_C */
6528
Hanno Becker8c13ee62019-02-26 16:48:17 +00006529 ret = mbedtls_ssl_check_cert_usage( chain,
6530 ciphersuite_info,
6531 ! ssl->conf->endpoint,
6532 &ssl->session_negotiate->verify_result );
6533 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00006534 {
6535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00006536 if( verify_ret == 0 )
6537 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00006538 }
6539
6540 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6541 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6542 * with details encoded in the verification flags. All other kinds
6543 * of error codes, including those from the user provided f_vrfy
6544 * functions, are treated as fatal and lead to a failure of
6545 * ssl_parse_certificate even if verification was optional. */
6546 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00006547 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6548 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00006549 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00006550 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00006551 }
6552
6553 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6554 {
6555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00006556 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00006557 }
6558
Hanno Becker8c13ee62019-02-26 16:48:17 +00006559 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00006560 {
6561 uint8_t alert;
6562
6563 /* The certificate may have been rejected for several reasons.
6564 Pick one and send the corresponding alert. Which alert to send
6565 may be a subject of debate in some cases. */
6566 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6567 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6568 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6569 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6570 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6571 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6572 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6573 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6574 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6575 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6576 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6577 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6578 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6579 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6580 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6581 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6582 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6583 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6584 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6585 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6586 else
6587 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6588 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6589 alert );
6590 }
6591
6592#if defined(MBEDTLS_DEBUG_C)
6593 if( ssl->session_negotiate->verify_result != 0 )
6594 {
6595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6596 ssl->session_negotiate->verify_result ) );
6597 }
6598 else
6599 {
6600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6601 }
6602#endif /* MBEDTLS_DEBUG_C */
6603
Hanno Becker8c13ee62019-02-26 16:48:17 +00006604 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00006605}
6606
Hanno Becker34106f62019-02-08 14:59:05 +00006607#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01006608
6609#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006610static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6611 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006612{
6613 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00006614 /* Remember digest of the peer's end-CRT. */
6615 ssl->session_negotiate->peer_cert_digest =
6616 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6617 if( ssl->session_negotiate->peer_cert_digest == NULL )
6618 {
6619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6620 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6621 mbedtls_ssl_send_alert_message( ssl,
6622 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6623 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6624
6625 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6626 }
6627
6628 ret = mbedtls_md( mbedtls_md_info_from_type(
6629 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6630 start, len,
6631 ssl->session_negotiate->peer_cert_digest );
6632
6633 ssl->session_negotiate->peer_cert_digest_type =
6634 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6635 ssl->session_negotiate->peer_cert_digest_len =
6636 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6637
6638 return( ret );
6639}
Hanno Becker5882dd02019-06-06 16:25:57 +01006640#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006641
6642static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6643 unsigned char *start, size_t len )
6644{
6645 unsigned char *end = start + len;
6646 int ret;
6647
6648 /* Make a copy of the peer's raw public key. */
6649 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6650 ret = mbedtls_pk_parse_subpubkey( &start, end,
6651 &ssl->handshake->peer_pubkey );
6652 if( ret != 0 )
6653 {
6654 /* We should have parsed the public key before. */
6655 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6656 }
6657
6658 return( 0 );
6659}
6660#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6661
Hanno Becker3cf50612019-02-05 14:36:34 +00006662int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6663{
6664 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006665 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006666#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6667 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6668 ? ssl->handshake->sni_authmode
6669 : ssl->conf->authmode;
6670#else
6671 const int authmode = ssl->conf->authmode;
6672#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006673 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006674 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006675
6676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6677
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006678 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6679 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006680 {
6681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006682 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006683 }
6684
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006685#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6686 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006687 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006688 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006689 chain = ssl->handshake->ecrs_peer_cert;
6690 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006691 goto crt_verify;
6692 }
6693#endif
6694
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006695 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006696 {
6697 /* mbedtls_ssl_read_record may have sent an alert already. We
6698 let it decide whether to alert. */
6699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006700 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006701 }
6702
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006703#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00006704 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6705 {
6706 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006707
Hanno Beckerb8a08572019-02-05 12:49:06 +00006708 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006709 ret = 0;
6710 else
6711 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006712
Hanno Becker613d4902019-02-05 13:11:17 +00006713 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006714 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00006715#endif /* MBEDTLS_SSL_SRV_C */
6716
Hanno Becker35e41772019-02-05 15:37:23 +00006717 /* Clear existing peer CRT structure in case we tried to
6718 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006719 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006720
6721 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6722 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006723 {
6724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6725 sizeof( mbedtls_x509_crt ) ) );
6726 mbedtls_ssl_send_alert_message( ssl,
6727 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6728 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006729
Hanno Beckere4aeb762019-02-05 17:19:52 +00006730 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6731 goto exit;
6732 }
6733 mbedtls_x509_crt_init( chain );
6734
6735 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006736 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006737 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006738
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006739#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6740 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006741 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006742
6743crt_verify:
6744 if( ssl->handshake->ecrs_enabled)
6745 rs_ctx = &ssl->handshake->ecrs_ctx;
6746#endif
6747
Hanno Becker3cf50612019-02-05 14:36:34 +00006748 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006749 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006750 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006751 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006752
Hanno Becker3008d282019-02-05 17:02:28 +00006753#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00006754 {
Hanno Becker5882dd02019-06-06 16:25:57 +01006755 size_t pk_len;
6756 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00006757
Hanno Becker34106f62019-02-08 14:59:05 +00006758 /* We parse the CRT chain without copying, so
6759 * these pointers point into the input buffer,
6760 * and are hence still valid after freeing the
6761 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006762
Hanno Becker5882dd02019-06-06 16:25:57 +01006763#if defined(MBEDTLS_SSL_RENEGOTIATION)
6764 unsigned char *crt_start;
6765 size_t crt_len;
6766
Hanno Becker34106f62019-02-08 14:59:05 +00006767 crt_start = chain->raw.p;
6768 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01006769#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006770
Hanno Becker8c13ee62019-02-26 16:48:17 +00006771 pk_start = chain->cache->pk_raw.p;
6772 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00006773
6774 /* Free the CRT structures before computing
6775 * digest and copying the peer's public key. */
6776 mbedtls_x509_crt_free( chain );
6777 mbedtls_free( chain );
6778 chain = NULL;
6779
Hanno Becker5882dd02019-06-06 16:25:57 +01006780#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006781 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006782 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006783 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01006784#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006785
Hanno Becker34106f62019-02-08 14:59:05 +00006786 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006787 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00006788 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006789 }
Hanno Becker34106f62019-02-08 14:59:05 +00006790#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6791 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006792 ssl->session_negotiate->peer_cert = chain;
6793 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006794#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006797
Hanno Becker613d4902019-02-05 13:11:17 +00006798exit:
6799
Hanno Beckere4aeb762019-02-05 17:19:52 +00006800 if( ret == 0 )
6801 ssl->state++;
6802
6803#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6804 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6805 {
6806 ssl->handshake->ecrs_peer_cert = chain;
6807 chain = NULL;
6808 }
6809#endif
6810
6811 if( chain != NULL )
6812 {
6813 mbedtls_x509_crt_free( chain );
6814 mbedtls_free( chain );
6815 }
6816
Paul Bakker5121ce52009-01-03 21:22:43 +00006817 return( ret );
6818}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006819#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822{
6823 int ret;
6824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006828 ssl->out_msglen = 1;
6829 ssl->out_msg[0] = 1;
6830
Paul Bakker5121ce52009-01-03 21:22:43 +00006831 ssl->state++;
6832
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006833 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006834 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006836 return( ret );
6837 }
6838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006840
6841 return( 0 );
6842}
6843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006845{
6846 int ret;
6847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006849
Hanno Becker327c93b2018-08-15 13:56:18 +01006850 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006853 return( ret );
6854 }
6855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006859 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6860 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006862 }
6863
Hanno Beckere678eaa2018-08-21 14:57:46 +01006864 /* CCS records are only accepted if they have length 1 and content '1',
6865 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006866
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006867 /*
6868 * Switch to our negotiated transform and session parameters for inbound
6869 * data.
6870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006872 ssl->transform_in = ssl->transform_negotiate;
6873 ssl->session_in = ssl->session_negotiate;
6874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006876 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006879 ssl_dtls_replay_reset( ssl );
6880#endif
6881
6882 /* Increment epoch */
6883 if( ++ssl->in_epoch == 0 )
6884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006886 /* This is highly unlikely to happen for legitimate reasons, so
6887 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006889 }
6890 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006891 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006893#if defined(MBEDTLS_SSL_PROTO_TLS)
6894 {
6895 memset( ssl->in_ctr, 0, 8 );
6896 }
6897#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006898
Hanno Beckerf5970a02019-05-08 09:38:41 +01006899 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6902 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006907 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6908 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006910 }
6911 }
6912#endif
6913
Paul Bakker5121ce52009-01-03 21:22:43 +00006914 ssl->state++;
6915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006917
6918 return( 0 );
6919}
6920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6922 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006923{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006924 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6927 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6928 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006929 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006930 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6933#if defined(MBEDTLS_SHA512_C)
6934 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006935 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6936 else
6937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938#if defined(MBEDTLS_SHA256_C)
6939 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006940 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006941 else
6942#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006946 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006947 }
Paul Bakker380da532012-04-18 16:10:25 +00006948}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006951{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6953 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006954 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6955 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6958#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006959 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006962 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006963#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006965}
6966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006968 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006969{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6971 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006972 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6973 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6976#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006977 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006980 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006981#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006983}
6984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6986 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6987static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006988 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006989{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006990 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6991 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006992}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006993#endif
Paul Bakker380da532012-04-18 16:10:25 +00006994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6996#if defined(MBEDTLS_SHA256_C)
6997static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006998 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006999{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007000 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007001}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007002#endif
Paul Bakker380da532012-04-18 16:10:25 +00007003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004#if defined(MBEDTLS_SHA512_C)
7005static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007006 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007007{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007008 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007009}
Paul Bakker769075d2012-11-24 11:26:46 +01007010#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007014static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007016{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007017 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007018 mbedtls_md5_context md5;
7019 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007020
Paul Bakker5121ce52009-01-03 21:22:43 +00007021 unsigned char padbuf[48];
7022 unsigned char md5sum[16];
7023 unsigned char sha1sum[20];
7024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007026 if( !session )
7027 session = ssl->session;
7028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007030
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007031 mbedtls_md5_init( &md5 );
7032 mbedtls_sha1_init( &sha1 );
7033
7034 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7035 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007036
7037 /*
7038 * SSLv3:
7039 * hash =
7040 * MD5( master + pad2 +
7041 * MD5( handshake + sender + master + pad1 ) )
7042 * + SHA1( master + pad2 +
7043 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007044 */
7045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007047 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7048 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007052 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7053 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007057 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007058
Paul Bakker1ef83d62012-04-11 12:09:53 +00007059 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007060
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007061 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7062 mbedtls_md5_update_ret( &md5, session->master, 48 );
7063 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7064 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007065
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007066 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7067 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7068 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7069 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
Paul Bakker1ef83d62012-04-11 12:09:53 +00007071 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007073 mbedtls_md5_starts_ret( &md5 );
7074 mbedtls_md5_update_ret( &md5, session->master, 48 );
7075 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7076 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7077 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007078
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007079 mbedtls_sha1_starts_ret( &sha1 );
7080 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7081 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7082 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7083 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007086
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007087 mbedtls_md5_free( &md5 );
7088 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007089
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007090 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7091 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7092 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007099static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007101{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007102 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007103 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007104 mbedtls_md5_context md5;
7105 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007106 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007109 if( !session )
7110 session = ssl->session;
7111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007114 mbedtls_md5_init( &md5 );
7115 mbedtls_sha1_init( &sha1 );
7116
7117 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7118 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007119
Paul Bakker1ef83d62012-04-11 12:09:53 +00007120 /*
7121 * TLSv1:
7122 * hash = PRF( master, finished_label,
7123 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7124 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007127 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7128 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007129#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007132 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7133 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007134#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007137 ? "client finished"
7138 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007139
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007140 mbedtls_md5_finish_ret( &md5, padbuf );
7141 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007142
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007143 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007144 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007147
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007148 mbedtls_md5_free( &md5 );
7149 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007150
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007151 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7158#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007159static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007161{
7162 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007163 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007164 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007165 unsigned char padbuf[32];
7166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007168 if( !session )
7169 session = ssl->session;
7170
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007171 mbedtls_sha256_init( &sha256 );
7172
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007174
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007175 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007176
7177 /*
7178 * TLSv1.2:
7179 * hash = PRF( master, finished_label,
7180 * Hash( handshake ) )[0.11]
7181 */
7182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183#if !defined(MBEDTLS_SHA256_ALT)
7184 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007185 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007186#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007189 ? "client finished"
7190 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007191
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007192 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007193
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007194 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007195 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007198
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007199 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007200
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007201 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007208static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007210{
7211 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007212 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007213 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007214 unsigned char padbuf[48];
7215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007217 if( !session )
7218 session = ssl->session;
7219
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007220 mbedtls_sha512_init( &sha512 );
7221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007223
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007224 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007225
7226 /*
7227 * TLSv1.2:
7228 * hash = PRF( master, finished_label,
7229 * Hash( handshake ) )[0.11]
7230 */
7231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007233 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7234 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007235#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007238 ? "client finished"
7239 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007240
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007241 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007242
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007243 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007244 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007247
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007248 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007249
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007250 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254#endif /* MBEDTLS_SHA512_C */
7255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007260
7261 /*
7262 * Free our handshake params
7263 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007264 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007266 ssl->handshake = NULL;
7267
7268 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007269 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007270 */
7271 if( ssl->transform )
7272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 mbedtls_ssl_transform_free( ssl->transform );
7274 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007275 }
7276 ssl->transform = ssl->transform_negotiate;
7277 ssl->transform_negotiate = NULL;
7278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007280}
7281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007283{
7284 int resume = ssl->handshake->resume;
7285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288#if defined(MBEDTLS_SSL_RENEGOTIATION)
7289 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007292 ssl->renego_records_seen = 0;
7293 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007294#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007295
7296 /*
7297 * Free the previous session and switch in the current one
7298 */
Paul Bakker0a597072012-09-25 21:55:46 +00007299 if( ssl->session )
7300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007302 /* RFC 7366 3.1: keep the EtM state */
7303 ssl->session_negotiate->encrypt_then_mac =
7304 ssl->session->encrypt_then_mac;
7305#endif
7306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 mbedtls_ssl_session_free( ssl->session );
7308 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007309 }
7310 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007311 ssl->session_negotiate = NULL;
7312
Paul Bakker0a597072012-09-25 21:55:46 +00007313 /*
7314 * Add cache entry
7315 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007316 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007317 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007318 resume == 0 )
7319 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007320 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007322 }
Paul Bakker0a597072012-09-25 21:55:46 +00007323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007325 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007326 ssl->handshake->flight != NULL )
7327 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007328 /* Cancel handshake timer */
7329 ssl_set_timer( ssl, 0 );
7330
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007331 /* Keep last flight around in case we need to resend it:
7332 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007334 }
7335 else
7336#endif
7337 ssl_handshake_wrapup_free_hs_transform( ssl );
7338
Paul Bakker48916f92012-09-16 19:57:18 +00007339 ssl->state++;
7340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007342}
7343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007345{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007346 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007349
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007350 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007351
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007352 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007353
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007354 /*
7355 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7356 * may define some other value. Currently (early 2016), no defined
7357 * ciphersuite does this (and this is unlikely to change as activity has
7358 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007363 ssl->verify_data_len = hash_len;
7364 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007365#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007366
Paul Bakker5121ce52009-01-03 21:22:43 +00007367 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7369 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007370
7371 /*
7372 * In case of session resuming, invert the client and server
7373 * ChangeCipherSpec messages order.
7374 */
Paul Bakker0a597072012-09-25 21:55:46 +00007375 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007378 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007382 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007384#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007385 }
7386 else
7387 ssl->state++;
7388
Paul Bakker48916f92012-09-16 19:57:18 +00007389 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007390 * Switch to our negotiated transform and session parameters for outbound
7391 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007396 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007397 {
7398 unsigned char i;
7399
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007400 /* Remember current epoch settings for resending */
7401 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007402 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007403
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007404 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007405 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007406
7407 /* Increment epoch */
7408 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007409 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007410 break;
7411
7412 /* The loop goes to its end iff the counter is wrapping */
7413 if( i == 0 )
7414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7416 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007417 }
7418 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007419 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007421#if defined(MBEDTLS_SSL_PROTO_TLS)
7422 {
7423 memset( ssl->cur_out_ctr, 0, 8 );
7424 }
7425#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007426
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007427 ssl->transform_out = ssl->transform_negotiate;
7428 ssl->session_out = ssl->session_negotiate;
7429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7431 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7436 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007437 }
7438 }
7439#endif
7440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007442 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007444#endif
7445
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007446 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007447 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007449 return( ret );
7450 }
7451
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007452#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007453 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007454 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7455 {
7456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7457 return( ret );
7458 }
7459#endif
7460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007462
7463 return( 0 );
7464}
7465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007467#define SSL_MAX_HASH_LEN 36
7468#else
7469#define SSL_MAX_HASH_LEN 12
7470#endif
7471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007473{
Paul Bakker23986e52011-04-24 08:57:21 +00007474 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007475 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007476 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007479
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007480 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007481
Hanno Becker327c93b2018-08-15 13:56:18 +01007482 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007485 return( ret );
7486 }
7487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007491 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7492 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007494 }
7495
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007496 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497#if defined(MBEDTLS_SSL_PROTO_SSL3)
7498 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007499 hash_len = 36;
7500 else
7501#endif
7502 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7505 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007508 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7509 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007511 }
7512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007514 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007517 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7518 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007520 }
7521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007523 ssl->verify_data_len = hash_len;
7524 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007525#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007526
Paul Bakker0a597072012-09-25 21:55:46 +00007527 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007530 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007534 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007536#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007537 }
7538 else
7539 ssl->state++;
7540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007542 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007544#endif
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007547
7548 return( 0 );
7549}
7550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7556 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7557 mbedtls_md5_init( &handshake->fin_md5 );
7558 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007559 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7560 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007561#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7563#if defined(MBEDTLS_SHA256_C)
7564 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007565 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567#if defined(MBEDTLS_SHA512_C)
7568 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007569 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007572
7573 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007574
7575#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7576 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7577 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7578#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580#if defined(MBEDTLS_DHM_C)
7581 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if defined(MBEDTLS_ECDH_C)
7584 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007585#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007586#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007587 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007588#if defined(MBEDTLS_SSL_CLI_C)
7589 handshake->ecjpake_cache = NULL;
7590 handshake->ecjpake_cache_len = 0;
7591#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007592#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007593
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007594#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007595 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007596#endif
7597
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007598#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7599 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7600#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007601
7602#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7603 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7604 mbedtls_pk_init( &handshake->peer_pubkey );
7605#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007606}
7607
Hanno Becker611a83b2018-01-03 14:27:32 +00007608void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7613 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007614
Hanno Becker92231322018-01-03 15:32:51 +00007615#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 mbedtls_md_init( &transform->md_ctx_enc );
7617 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007618#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007619}
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007624}
7625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007627{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007628 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007629 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007631 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007633 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007634 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007635
7636 /*
7637 * Either the pointers are now NULL or cleared properly and can be freed.
7638 * Now allocate missing structures.
7639 */
7640 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007641 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007642 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007643 }
Paul Bakker48916f92012-09-16 19:57:18 +00007644
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007645 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007646 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007647 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007648 }
Paul Bakker48916f92012-09-16 19:57:18 +00007649
Paul Bakker82788fb2014-10-20 13:59:19 +02007650 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007651 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007652 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007653 }
Paul Bakker48916f92012-09-16 19:57:18 +00007654
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007655 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007656 if( ssl->handshake == NULL ||
7657 ssl->transform_negotiate == NULL ||
7658 ssl->session_negotiate == NULL )
7659 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 mbedtls_free( ssl->handshake );
7663 mbedtls_free( ssl->transform_negotiate );
7664 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007665
7666 ssl->handshake = NULL;
7667 ssl->transform_negotiate = NULL;
7668 ssl->session_negotiate = NULL;
7669
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007670 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007671 }
7672
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007673 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007675 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007676 ssl_handshake_params_init( ssl->handshake );
7677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007679 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007680 {
7681 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007682
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007683 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7684 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7685 else
7686 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007687
7688 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007689 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007690#endif
7691
Paul Bakker48916f92012-09-16 19:57:18 +00007692 return( 0 );
7693}
7694
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007695#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007696/* Dummy cookie callbacks for defaults */
7697static int ssl_cookie_write_dummy( void *ctx,
7698 unsigned char **p, unsigned char *end,
7699 const unsigned char *cli_id, size_t cli_id_len )
7700{
7701 ((void) ctx);
7702 ((void) p);
7703 ((void) end);
7704 ((void) cli_id);
7705 ((void) cli_id_len);
7706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007708}
7709
7710static int ssl_cookie_check_dummy( void *ctx,
7711 const unsigned char *cookie, size_t cookie_len,
7712 const unsigned char *cli_id, size_t cli_id_len )
7713{
7714 ((void) ctx);
7715 ((void) cookie);
7716 ((void) cookie_len);
7717 ((void) cli_id);
7718 ((void) cli_id_len);
7719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007721}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007722#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007723
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007724/* Once ssl->out_hdr as the address of the beginning of the
7725 * next outgoing record is set, deduce the other pointers.
7726 *
7727 * Note: For TLS, we save the implicit record sequence number
7728 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7729 * and the caller has to make sure there's space for this.
7730 */
7731
7732static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7733 mbedtls_ssl_transform *transform )
7734{
7735#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007736 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007737 {
7738 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007739#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007740 ssl->out_cid = ssl->out_ctr + 8;
7741 ssl->out_len = ssl->out_cid;
7742 if( transform != NULL )
7743 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007744#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007745 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007746#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007747 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007748 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007749 MBEDTLS_SSL_TRANSPORT_ELSE
7750#endif /* MBEDTLS_SSL_PROTO_DTLS */
7751#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007752 {
7753 ssl->out_ctr = ssl->out_hdr - 8;
7754 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007755#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007756 ssl->out_cid = ssl->out_len;
7757#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007758 ssl->out_iv = ssl->out_hdr + 5;
7759 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007760#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007761
7762 /* Adjust out_msg to make space for explicit IV, if used. */
7763 if( transform != NULL &&
7764 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7765 {
7766 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7767 }
7768 else
7769 ssl->out_msg = ssl->out_iv;
7770}
7771
7772/* Once ssl->in_hdr as the address of the beginning of the
7773 * next incoming record is set, deduce the other pointers.
7774 *
7775 * Note: For TLS, we save the implicit record sequence number
7776 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7777 * and the caller has to make sure there's space for this.
7778 */
7779
Hanno Beckerf5970a02019-05-08 09:38:41 +01007780static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007781{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007782 /* This function sets the pointers to match the case
7783 * of unprotected TLS/DTLS records, with both ssl->in_iv
7784 * and ssl->in_msg pointing to the beginning of the record
7785 * content.
7786 *
7787 * When decrypting a protected record, ssl->in_msg
7788 * will be shifted to point to the beginning of the
7789 * record plaintext.
7790 */
7791
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007792#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007793 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007794 {
Hanno Becker70e79282019-05-03 14:34:53 +01007795 /* This sets the header pointers to match records
7796 * without CID. When we receive a record containing
7797 * a CID, the fields are shifted accordingly in
7798 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007799 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007800#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007801 ssl->in_cid = ssl->in_ctr + 8;
7802 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007803#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007804 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007805#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007806 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007807 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007808 MBEDTLS_SSL_TRANSPORT_ELSE
7809#endif /* MBEDTLS_SSL_PROTO_DTLS */
7810#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007811 {
7812 ssl->in_ctr = ssl->in_hdr - 8;
7813 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007814#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007815 ssl->in_cid = ssl->in_len;
7816#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007817 ssl->in_iv = ssl->in_hdr + 5;
7818 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007819#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007820
Hanno Beckerf5970a02019-05-08 09:38:41 +01007821 /* This will be adjusted at record decryption time. */
7822 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007823}
7824
Paul Bakker5121ce52009-01-03 21:22:43 +00007825/*
7826 * Initialize an SSL context
7827 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007828void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7829{
7830 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7831}
7832
7833/*
7834 * Setup an SSL context
7835 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007836
7837static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7838{
7839 /* Set the incoming and outgoing record pointers. */
7840#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007841 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007842 {
7843 ssl->out_hdr = ssl->out_buf;
7844 ssl->in_hdr = ssl->in_buf;
7845 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007846 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007847#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007848#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007849 {
7850 ssl->out_hdr = ssl->out_buf + 8;
7851 ssl->in_hdr = ssl->in_buf + 8;
7852 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007853#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007854
7855 /* Derive other internal pointers. */
7856 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007857 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007858}
7859
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007860int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007861 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007862{
Paul Bakker48916f92012-09-16 19:57:18 +00007863 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007864
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007865 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007866
7867 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007868 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007869 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007870
7871 /* Set to NULL in case of an error condition */
7872 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007873
Angus Grattond8213d02016-05-25 20:56:48 +10007874 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7875 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007876 {
Angus Grattond8213d02016-05-25 20:56:48 +10007877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007878 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007879 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007880 }
7881
7882 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7883 if( ssl->out_buf == NULL )
7884 {
7885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007886 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007887 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007888 }
7889
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007890 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007891
Paul Bakker48916f92012-09-16 19:57:18 +00007892 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007893 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007894
7895 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007896
7897error:
7898 mbedtls_free( ssl->in_buf );
7899 mbedtls_free( ssl->out_buf );
7900
7901 ssl->conf = NULL;
7902
7903 ssl->in_buf = NULL;
7904 ssl->out_buf = NULL;
7905
7906 ssl->in_hdr = NULL;
7907 ssl->in_ctr = NULL;
7908 ssl->in_len = NULL;
7909 ssl->in_iv = NULL;
7910 ssl->in_msg = NULL;
7911
7912 ssl->out_hdr = NULL;
7913 ssl->out_ctr = NULL;
7914 ssl->out_len = NULL;
7915 ssl->out_iv = NULL;
7916 ssl->out_msg = NULL;
7917
k-stachowiak9f7798e2018-07-31 16:52:32 +02007918 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007919}
7920
7921/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007922 * Reset an initialized and used SSL context for re-use while retaining
7923 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007924 *
7925 * If partial is non-zero, keep data in the input buffer and client ID.
7926 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007927 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007928static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007929{
Paul Bakker48916f92012-09-16 19:57:18 +00007930 int ret;
7931
Hanno Becker7e772132018-08-10 12:38:21 +01007932#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7933 !defined(MBEDTLS_SSL_SRV_C)
7934 ((void) partial);
7935#endif
7936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007938
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007939 /* Cancel any possibly running timer */
7940 ssl_set_timer( ssl, 0 );
7941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942#if defined(MBEDTLS_SSL_RENEGOTIATION)
7943 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007944 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007945
7946 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7948 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007951
Paul Bakker7eb013f2011-10-06 12:37:39 +00007952 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007953 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007954
7955 ssl->in_msgtype = 0;
7956 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007958 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007959 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007962 ssl_dtls_replay_reset( ssl );
7963#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007964
7965 ssl->in_hslen = 0;
7966 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007967
7968 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007969
7970 ssl->out_msgtype = 0;
7971 ssl->out_msglen = 0;
7972 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7974 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007975 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007976#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007977
Hanno Becker19859472018-08-06 09:40:20 +01007978 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7979
Paul Bakker48916f92012-09-16 19:57:18 +00007980 ssl->transform_in = NULL;
7981 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007982
Hanno Becker78640902018-08-13 16:35:15 +01007983 ssl->session_in = NULL;
7984 ssl->session_out = NULL;
7985
Angus Grattond8213d02016-05-25 20:56:48 +10007986 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007987
7988#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007989 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007990#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7991 {
7992 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007993 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007994 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7997 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8000 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8003 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008004 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008005 }
8006#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008007
Paul Bakker48916f92012-09-16 19:57:18 +00008008 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 mbedtls_ssl_transform_free( ssl->transform );
8011 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008012 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008013 }
Paul Bakker48916f92012-09-16 19:57:18 +00008014
Paul Bakkerc0463502013-02-14 11:19:38 +01008015 if( ssl->session )
8016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 mbedtls_ssl_session_free( ssl->session );
8018 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008019 ssl->session = NULL;
8020 }
8021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008023 ssl->alpn_chosen = NULL;
8024#endif
8025
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008026#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008027#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008028 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008029#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008030 {
8031 mbedtls_free( ssl->cli_id );
8032 ssl->cli_id = NULL;
8033 ssl->cli_id_len = 0;
8034 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008035#endif
8036
Paul Bakker48916f92012-09-16 19:57:18 +00008037 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8038 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008039
8040 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008041}
8042
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008043/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008044 * Reset an initialized and used SSL context for re-use while retaining
8045 * all application-set variables, function pointers and data.
8046 */
8047int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8048{
8049 return( ssl_session_reset_int( ssl, 0 ) );
8050}
8051
8052/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008053 * SSL set accessors
8054 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008055void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008056{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008057 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008058}
8059
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008060void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008061{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008062 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008063}
8064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008066void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008067{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008068 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008069}
8070#endif
8071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008073void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008075 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008076}
8077#endif
8078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008080
Hanno Becker1841b0a2018-08-24 11:13:57 +01008081void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8082 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008083{
8084 ssl->disable_datagram_packing = !allow_packing;
8085}
8086
8087void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8088 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008089{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008090 conf->hs_timeout_min = min;
8091 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008092}
8093#endif
8094
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008095void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008096{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008097 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008098}
8099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008101void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008102 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008103 void *p_vrfy )
8104{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008105 conf->f_vrfy = f_vrfy;
8106 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008109
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008110void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008111 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008112 void *p_rng )
8113{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008114 conf->f_rng = f_rng;
8115 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008116}
8117
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008118void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008119 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008120 void *p_dbg )
8121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008122 conf->f_dbg = f_dbg;
8123 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008124}
8125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008127 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008128 mbedtls_ssl_send_t *f_send,
8129 mbedtls_ssl_recv_t *f_recv,
8130 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008131{
8132 ssl->p_bio = p_bio;
8133 ssl->f_send = f_send;
8134 ssl->f_recv = f_recv;
8135 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008136}
8137
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008138#if defined(MBEDTLS_SSL_PROTO_DTLS)
8139void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8140{
8141 ssl->mtu = mtu;
8142}
8143#endif
8144
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008145void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008146{
8147 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008148}
8149
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008150void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8151 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008152 mbedtls_ssl_set_timer_t *f_set_timer,
8153 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008154{
8155 ssl->p_timer = p_timer;
8156 ssl->f_set_timer = f_set_timer;
8157 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008158
8159 /* Make sure we start with no timer running */
8160 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008161}
8162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008164void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008165 void *p_cache,
8166 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8167 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008168{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008169 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008170 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008171 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#if defined(MBEDTLS_SSL_CLI_C)
8176int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008177{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008178 int ret;
8179
8180 if( ssl == NULL ||
8181 session == NULL ||
8182 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008183 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008186 }
8187
Hanno Becker58fccf22019-02-06 14:30:46 +00008188 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8189 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008190 return( ret );
8191
Paul Bakker0a597072012-09-25 21:55:46 +00008192 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008193
8194 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008197
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008198void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008199 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008200{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008201 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8202 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8203 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008205}
8206
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008207void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008208 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008209 int major, int minor )
8210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008212 return;
8213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008215 return;
8216
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008217 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008218}
8219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008221void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008222 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008223{
8224 conf->cert_profile = profile;
8225}
8226
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008227/* Append a new keycert entry to a (possibly empty) list */
8228static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8229 mbedtls_x509_crt *cert,
8230 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008231{
niisato8ee24222018-06-25 19:05:48 +09008232 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008233
niisato8ee24222018-06-25 19:05:48 +09008234 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8235 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008236 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008237
niisato8ee24222018-06-25 19:05:48 +09008238 new_cert->cert = cert;
8239 new_cert->key = key;
8240 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008241
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008242 /* Update head is the list was null, else add to the end */
8243 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008244 {
niisato8ee24222018-06-25 19:05:48 +09008245 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008246 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008247 else
8248 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008249 mbedtls_ssl_key_cert *cur = *head;
8250 while( cur->next != NULL )
8251 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008252 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008253 }
8254
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008255 return( 0 );
8256}
8257
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008258int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008259 mbedtls_x509_crt *own_cert,
8260 mbedtls_pk_context *pk_key )
8261{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008262 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008263}
8264
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008265void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008266 mbedtls_x509_crt *ca_chain,
8267 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008268{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008269 conf->ca_chain = ca_chain;
8270 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008271}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008273
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008274#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8275int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8276 mbedtls_x509_crt *own_cert,
8277 mbedtls_pk_context *pk_key )
8278{
8279 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8280 own_cert, pk_key ) );
8281}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008282
8283void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8284 mbedtls_x509_crt *ca_chain,
8285 mbedtls_x509_crl *ca_crl )
8286{
8287 ssl->handshake->sni_ca_chain = ca_chain;
8288 ssl->handshake->sni_ca_crl = ca_crl;
8289}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008290
8291void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8292 int authmode )
8293{
8294 ssl->handshake->sni_authmode = authmode;
8295}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008296#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8297
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008298#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008299/*
8300 * Set EC J-PAKE password for current handshake
8301 */
8302int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8303 const unsigned char *pw,
8304 size_t pw_len )
8305{
8306 mbedtls_ecjpake_role role;
8307
Janos Follath8eb64132016-06-03 15:40:57 +01008308 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8310
8311 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8312 role = MBEDTLS_ECJPAKE_SERVER;
8313 else
8314 role = MBEDTLS_ECJPAKE_CLIENT;
8315
8316 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8317 role,
8318 MBEDTLS_MD_SHA256,
8319 MBEDTLS_ECP_DP_SECP256R1,
8320 pw, pw_len ) );
8321}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008322#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008325int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008326 const unsigned char *psk, size_t psk_len,
8327 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008328{
Paul Bakker6db455e2013-09-18 17:29:31 +02008329 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008334
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008335 /* Identity len will be encoded on two bytes */
8336 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008337 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008338 {
8339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8340 }
8341
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008342 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008343 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008344 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008345
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008346 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008347 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008348 conf->psk_len = 0;
8349 }
8350 if( conf->psk_identity != NULL )
8351 {
8352 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008353 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008354 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008355 }
8356
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008357 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8358 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008359 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008360 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008361 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008362 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008363 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008364 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008365 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008366
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008367 conf->psk_len = psk_len;
8368 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008369
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008370 memcpy( conf->psk, psk, conf->psk_len );
8371 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008372
8373 return( 0 );
8374}
8375
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008376int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8377 const unsigned char *psk, size_t psk_len )
8378{
8379 if( psk == NULL || ssl->handshake == NULL )
8380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8381
8382 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8384
8385 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008386 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008387 mbedtls_platform_zeroize( ssl->handshake->psk,
8388 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008389 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008390 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008391 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008392
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008393 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008394 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008395
8396 ssl->handshake->psk_len = psk_len;
8397 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8398
8399 return( 0 );
8400}
8401
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008402void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008404 size_t),
8405 void *p_psk )
8406{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008407 conf->f_psk = f_psk;
8408 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008411
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008412#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008413
8414#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008415int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008416{
8417 int ret;
8418
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008419 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8420 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8421 {
8422 mbedtls_mpi_free( &conf->dhm_P );
8423 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008424 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008426
8427 return( 0 );
8428}
Hanno Becker470a8c42017-10-04 15:28:46 +01008429#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008430
Hanno Beckera90658f2017-10-04 15:29:08 +01008431int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8432 const unsigned char *dhm_P, size_t P_len,
8433 const unsigned char *dhm_G, size_t G_len )
8434{
8435 int ret;
8436
8437 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8438 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8439 {
8440 mbedtls_mpi_free( &conf->dhm_P );
8441 mbedtls_mpi_free( &conf->dhm_G );
8442 return( ret );
8443 }
8444
8445 return( 0 );
8446}
Paul Bakker5121ce52009-01-03 21:22:43 +00008447
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008448int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008449{
8450 int ret;
8451
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008452 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8453 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8454 {
8455 mbedtls_mpi_free( &conf->dhm_P );
8456 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008457 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008458 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008459
8460 return( 0 );
8461}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008462#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008463
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008464#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8465/*
8466 * Set the minimum length for Diffie-Hellman parameters
8467 */
8468void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8469 unsigned int bitlen )
8470{
8471 conf->dhm_min_bitlen = bitlen;
8472}
8473#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8474
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008475#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008476/*
8477 * Set allowed/preferred hashes for handshake signatures
8478 */
8479void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8480 const int *hashes )
8481{
8482 conf->sig_hashes = hashes;
8483}
Hanno Becker947194e2017-04-07 13:25:49 +01008484#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008485
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008486#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008487/*
8488 * Set the allowed elliptic curves
8489 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008490void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008491 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008492{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008493 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008494}
Hanno Becker947194e2017-04-07 13:25:49 +01008495#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008496
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008497#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008499{
Hanno Becker947194e2017-04-07 13:25:49 +01008500 /* Initialize to suppress unnecessary compiler warning */
8501 size_t hostname_len = 0;
8502
8503 /* Check if new hostname is valid before
8504 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008505 if( hostname != NULL )
8506 {
8507 hostname_len = strlen( hostname );
8508
8509 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8511 }
8512
8513 /* Now it's clear that we will overwrite the old hostname,
8514 * so we can free it safely */
8515
8516 if( ssl->hostname != NULL )
8517 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008518 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008519 mbedtls_free( ssl->hostname );
8520 }
8521
8522 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008523
Paul Bakker5121ce52009-01-03 21:22:43 +00008524 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008525 {
8526 ssl->hostname = NULL;
8527 }
8528 else
8529 {
8530 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008531 if( ssl->hostname == NULL )
8532 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008533
Hanno Becker947194e2017-04-07 13:25:49 +01008534 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008535
Hanno Becker947194e2017-04-07 13:25:49 +01008536 ssl->hostname[hostname_len] = '\0';
8537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008538
8539 return( 0 );
8540}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008541#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008542
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008543#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008544void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008546 const unsigned char *, size_t),
8547 void *p_sni )
8548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008549 conf->f_sni = f_sni;
8550 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008555int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008556{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008557 size_t cur_len, tot_len;
8558 const char **p;
8559
8560 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008561 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8562 * MUST NOT be truncated."
8563 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008564 */
8565 tot_len = 0;
8566 for( p = protos; *p != NULL; p++ )
8567 {
8568 cur_len = strlen( *p );
8569 tot_len += cur_len;
8570
8571 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008573 }
8574
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008575 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008576
8577 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008578}
8579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008581{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008582 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008585
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008586void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008587{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008588 conf->max_major_ver = major;
8589 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008590}
8591
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008592void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008593{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008594 conf->min_major_ver = major;
8595 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008596}
8597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008599void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008600{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008601 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008602}
8603#endif
8604
Janos Follath088ce432017-04-10 12:42:31 +01008605#if defined(MBEDTLS_SSL_SRV_C)
8606void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8607 char cert_req_ca_list )
8608{
8609 conf->cert_req_ca_list = cert_req_ca_list;
8610}
8611#endif
8612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008614void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008615{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008616 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008617}
8618#endif
8619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008621void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008623 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008624}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008625
8626void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008627 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008628{
8629 conf->enforce_extended_master_secret = ems_enf;
8630}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008631#endif
8632
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008633#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008634void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008635{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008636 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008637}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008638#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008641int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008642{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008644 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008647 }
8648
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008649 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008650
8651 return( 0 );
8652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008653#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008655#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008656void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008657{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008658 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008662#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008663void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008664{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008665 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008666}
8667#endif
8668
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008669void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008671 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008672}
8673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008675void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008676{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008677 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008678}
8679
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008680void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008682 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008683}
8684
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008685void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008686 const unsigned char period[8] )
8687{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008688 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008693#if defined(MBEDTLS_SSL_CLI_C)
8694void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008695{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008696 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008697}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008698#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008699
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008700#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008701void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8702 mbedtls_ssl_ticket_write_t *f_ticket_write,
8703 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8704 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008705{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008706 conf->f_ticket_write = f_ticket_write;
8707 conf->f_ticket_parse = f_ticket_parse;
8708 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008709}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008712
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008713#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8714void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8715 mbedtls_ssl_export_keys_t *f_export_keys,
8716 void *p_export_keys )
8717{
8718 conf->f_export_keys = f_export_keys;
8719 conf->p_export_keys = p_export_keys;
8720}
8721#endif
8722
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008723#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008724void mbedtls_ssl_conf_async_private_cb(
8725 mbedtls_ssl_config *conf,
8726 mbedtls_ssl_async_sign_t *f_async_sign,
8727 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8728 mbedtls_ssl_async_resume_t *f_async_resume,
8729 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008730 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008731{
8732 conf->f_async_sign_start = f_async_sign;
8733 conf->f_async_decrypt_start = f_async_decrypt;
8734 conf->f_async_resume = f_async_resume;
8735 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008736 conf->p_async_config_data = async_config_data;
8737}
8738
Gilles Peskine8f97af72018-04-26 11:46:10 +02008739void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8740{
8741 return( conf->p_async_config_data );
8742}
8743
Gilles Peskine1febfef2018-04-30 11:54:39 +02008744void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008745{
8746 if( ssl->handshake == NULL )
8747 return( NULL );
8748 else
8749 return( ssl->handshake->user_async_ctx );
8750}
8751
Gilles Peskine1febfef2018-04-30 11:54:39 +02008752void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008753 void *ctx )
8754{
8755 if( ssl->handshake != NULL )
8756 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008757}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008758#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008759
Paul Bakker5121ce52009-01-03 21:22:43 +00008760/*
8761 * SSL get accessors
8762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008764{
8765 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8766}
8767
Hanno Becker8b170a02017-10-10 11:51:19 +01008768int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8769{
8770 /*
8771 * Case A: We're currently holding back
8772 * a message for further processing.
8773 */
8774
8775 if( ssl->keep_current_message == 1 )
8776 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008778 return( 1 );
8779 }
8780
8781 /*
8782 * Case B: Further records are pending in the current datagram.
8783 */
8784
8785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008786 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008787 ssl->in_left > ssl->next_record_offset )
8788 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008790 return( 1 );
8791 }
8792#endif /* MBEDTLS_SSL_PROTO_DTLS */
8793
8794 /*
8795 * Case C: A handshake message is being processed.
8796 */
8797
Hanno Becker8b170a02017-10-10 11:51:19 +01008798 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8799 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008801 return( 1 );
8802 }
8803
8804 /*
8805 * Case D: An application data message is being processed
8806 */
8807 if( ssl->in_offt != NULL )
8808 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008809 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008810 return( 1 );
8811 }
8812
8813 /*
8814 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008815 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008816 * we implement support for multiple alerts in single records.
8817 */
8818
8819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8820 return( 0 );
8821}
8822
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008823uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008824{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008825 if( ssl->session != NULL )
8826 return( ssl->session->verify_result );
8827
8828 if( ssl->session_negotiate != NULL )
8829 return( ssl->session_negotiate->verify_result );
8830
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008831 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008832}
8833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008835{
Paul Bakker926c8e42013-03-06 10:23:34 +01008836 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008837 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008840}
8841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008845 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008846 {
8847 switch( ssl->minor_ver )
8848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008850 return( "DTLSv1.0" );
8851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008853 return( "DTLSv1.2" );
8854
8855 default:
8856 return( "unknown (DTLS)" );
8857 }
8858 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008859 MBEDTLS_SSL_TRANSPORT_ELSE
8860#endif /* MBEDTLS_SSL_PROTO_DTLS */
8861#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008862 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008863 switch( ssl->minor_ver )
8864 {
8865 case MBEDTLS_SSL_MINOR_VERSION_0:
8866 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008867
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008868 case MBEDTLS_SSL_MINOR_VERSION_1:
8869 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008870
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008871 case MBEDTLS_SSL_MINOR_VERSION_2:
8872 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008873
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008874 case MBEDTLS_SSL_MINOR_VERSION_3:
8875 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008876
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008877 default:
8878 return( "unknown" );
8879 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008880 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008881#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008882}
8883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008884int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008885{
Hanno Becker3136ede2018-08-17 15:28:19 +01008886 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008888 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008889
Hanno Becker43395762019-05-03 14:46:38 +01008890 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8891
Hanno Becker78640902018-08-13 16:35:15 +01008892 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008893 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895#if defined(MBEDTLS_ZLIB_SUPPORT)
8896 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8897 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008898#endif
8899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008902 case MBEDTLS_MODE_GCM:
8903 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008904 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008906 transform_expansion = transform->minlen;
8907 break;
8908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008910
8911 block_size = mbedtls_cipher_get_block_size(
8912 &transform->cipher_ctx_enc );
8913
Hanno Becker3136ede2018-08-17 15:28:19 +01008914 /* Expansion due to the addition of the MAC. */
8915 transform_expansion += transform->maclen;
8916
8917 /* Expansion due to the addition of CBC padding;
8918 * Theoretically up to 256 bytes, but we never use
8919 * more than the block size of the underlying cipher. */
8920 transform_expansion += block_size;
8921
8922 /* For TLS 1.1 or higher, an explicit IV is added
8923 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008924#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8925 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008926 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008927#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008928
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008929 break;
8930
8931 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008934 }
8935
Hanno Beckera5a2b082019-05-15 14:03:01 +01008936#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008937 if( transform->out_cid_len != 0 )
8938 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008939#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008940
Hanno Becker43395762019-05-03 14:46:38 +01008941 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008942}
8943
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008944#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8945size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8946{
8947 size_t max_len;
8948
8949 /*
8950 * Assume mfl_code is correct since it was checked when set
8951 */
Angus Grattond8213d02016-05-25 20:56:48 +10008952 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008953
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008954 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008955 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008956 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008957 {
Angus Grattond8213d02016-05-25 20:56:48 +10008958 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008959 }
8960
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008961 /* During a handshake, use the value being negotiated */
8962 if( ssl->session_negotiate != NULL &&
8963 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8964 {
8965 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8966 }
8967
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008968 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008969}
8970#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8971
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008972#if defined(MBEDTLS_SSL_PROTO_DTLS)
8973static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8974{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008975 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8976 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8977 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8978 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8979 return ( 0 );
8980
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008981 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8982 return( ssl->mtu );
8983
8984 if( ssl->mtu == 0 )
8985 return( ssl->handshake->mtu );
8986
8987 return( ssl->mtu < ssl->handshake->mtu ?
8988 ssl->mtu : ssl->handshake->mtu );
8989}
8990#endif /* MBEDTLS_SSL_PROTO_DTLS */
8991
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008992int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8993{
8994 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8995
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008996#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8997 !defined(MBEDTLS_SSL_PROTO_DTLS)
8998 (void) ssl;
8999#endif
9000
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009001#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9002 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9003
9004 if( max_len > mfl )
9005 max_len = mfl;
9006#endif
9007
9008#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009009 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009010 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009011 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009012 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9013 const size_t overhead = (size_t) ret;
9014
9015 if( ret < 0 )
9016 return( ret );
9017
9018 if( mtu <= overhead )
9019 {
9020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9021 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9022 }
9023
9024 if( max_len > mtu - overhead )
9025 max_len = mtu - overhead;
9026 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009027#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009028
Hanno Becker0defedb2018-08-10 12:35:02 +01009029#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9030 !defined(MBEDTLS_SSL_PROTO_DTLS)
9031 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009032#endif
9033
9034 return( (int) max_len );
9035}
9036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037#if defined(MBEDTLS_X509_CRT_PARSE_C)
9038const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009039{
9040 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009041 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009042
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009043#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009044 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009045#else
9046 return( NULL );
9047#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009048}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009052int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9053 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009054{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009055 if( ssl == NULL ||
9056 dst == NULL ||
9057 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009058 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009061 }
9062
Hanno Becker58fccf22019-02-06 14:30:46 +00009063 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009066
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009067const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9068{
9069 if( ssl == NULL )
9070 return( NULL );
9071
9072 return( ssl->session );
9073}
9074
Paul Bakker5121ce52009-01-03 21:22:43 +00009075/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009076 * Define ticket header determining Mbed TLS version
9077 * and structure of the ticket.
9078 */
9079
Hanno Becker41527622019-05-16 12:50:45 +01009080/*
Hanno Becker26829e92019-05-28 14:30:45 +01009081 * Define bitflag determining compile-time settings influencing
9082 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009083 */
9084
Hanno Becker26829e92019-05-28 14:30:45 +01009085#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009086#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009087#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009088#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009089#endif /* MBEDTLS_HAVE_TIME */
9090
9091#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009092#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009093#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009094#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009095#endif /* MBEDTLS_X509_CRT_PARSE_C */
9096
9097#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009098#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009099#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009100#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009101#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9102
9103#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009104#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009105#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009106#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009107#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9108
9109#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009110#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009111#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009112#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009113#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9114
9115#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009116#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009117#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009118#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009119#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9120
Hanno Becker41527622019-05-16 12:50:45 +01009121#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9122#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9123#else
9124#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9125#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9126
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009127#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9128#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9129#else
9130#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9131#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9132
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009133#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9134#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9135#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9136#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9137#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9138#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9139#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009140#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009141
Hanno Becker26829e92019-05-28 14:30:45 +01009142#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009143 ( (uint16_t) ( \
9144 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9145 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9146 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9147 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9148 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9149 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009150 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9151 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009152
Hanno Becker557fe9f2019-05-16 12:41:07 +01009153static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009154 MBEDTLS_VERSION_MAJOR,
9155 MBEDTLS_VERSION_MINOR,
9156 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009157 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9158 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009159};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009160
9161/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009162 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009163 * (in the presentation language of TLS, RFC 8446 section 3)
9164 *
Hanno Becker26829e92019-05-28 14:30:45 +01009165 * opaque mbedtls_version[3]; // major, minor, patch
9166 * opaque session_format[2]; // version-specific 16-bit field determining
9167 * // the format of the remaining
9168 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009169 *
9170 * Note: When updating the format, remember to keep
9171 * these version+format bytes.
9172 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009173 * // In this version, `session_format` determines
9174 * // the setting of those compile-time
9175 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009176 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009177 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009178 * uint8 ciphersuite[2]; // defined by the standard
9179 * uint8 compression; // 0 or 1
9180 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009181 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009182 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009183 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009184 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9185 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9186 * case disabled: uint8_t peer_cert_digest_type;
9187 * opaque peer_cert_digest<0..2^8-1>;
9188 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009189 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009190 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009191 * uint8 mfl_code; // up to 255 according to standard
9192 * uint8 trunc_hmac; // 0 or 1
9193 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009194 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009195 * The order is the same as in the definition of the structure, except
9196 * verify_result is put before peer_cert so that all mandatory fields come
9197 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009198 */
9199int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9200 unsigned char *buf,
9201 size_t buf_len,
9202 size_t *olen )
9203{
9204 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009205 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009206#if defined(MBEDTLS_HAVE_TIME)
9207 uint64_t start;
9208#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009209#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009210#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009211 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009212#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009213#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009214
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009215 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009216 * Add version identifier
9217 */
9218
9219 used += sizeof( ssl_serialized_session_header );
9220
9221 if( used <= buf_len )
9222 {
9223 memcpy( p, ssl_serialized_session_header,
9224 sizeof( ssl_serialized_session_header ) );
9225 p += sizeof( ssl_serialized_session_header );
9226 }
9227
9228 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009229 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009230 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009231#if defined(MBEDTLS_HAVE_TIME)
9232 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009233
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009234 if( used <= buf_len )
9235 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009236 start = (uint64_t) session->start;
9237
9238 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9239 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9240 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9241 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9242 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9243 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9244 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9245 *p++ = (unsigned char)( ( start ) & 0xFF );
9246 }
9247#endif /* MBEDTLS_HAVE_TIME */
9248
9249 /*
9250 * Basic mandatory fields
9251 */
9252 used += 2 /* ciphersuite */
9253 + 1 /* compression */
9254 + 1 /* id_len */
9255 + sizeof( session->id )
9256 + sizeof( session->master )
9257 + 4; /* verify_result */
9258
9259 if( used <= buf_len )
9260 {
9261 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9262 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9263
9264 *p++ = (unsigned char)( session->compression & 0xFF );
9265
9266 *p++ = (unsigned char)( session->id_len & 0xFF );
9267 memcpy( p, session->id, 32 );
9268 p += 32;
9269
9270 memcpy( p, session->master, 48 );
9271 p += 48;
9272
9273 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9274 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9275 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9276 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009277 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009278
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009279 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009280 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009281 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009282#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009283#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009284 if( session->peer_cert == NULL )
9285 cert_len = 0;
9286 else
9287 cert_len = session->peer_cert->raw.len;
9288
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009289 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009290
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009291 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009292 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009293 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9294 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9295 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9296
9297 if( session->peer_cert != NULL )
9298 {
9299 memcpy( p, session->peer_cert->raw.p, cert_len );
9300 p += cert_len;
9301 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009302 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009303
Hanno Becker5882dd02019-06-06 16:25:57 +01009304#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009305 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009306 if( session->peer_cert_digest != NULL )
9307 {
9308 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9309 if( used <= buf_len )
9310 {
9311 *p++ = (unsigned char) session->peer_cert_digest_type;
9312 *p++ = (unsigned char) session->peer_cert_digest_len;
9313 memcpy( p, session->peer_cert_digest,
9314 session->peer_cert_digest_len );
9315 p += session->peer_cert_digest_len;
9316 }
9317 }
9318 else
9319 {
9320 used += 2;
9321 if( used <= buf_len )
9322 {
9323 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9324 *p++ = 0;
9325 }
9326 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009327#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009328#endif /* MBEDTLS_X509_CRT_PARSE_C */
9329
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009330 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009331 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009332 */
9333#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009334 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009335
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009336 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009337 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009338 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9339 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9340 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9341
9342 if( session->ticket != NULL )
9343 {
9344 memcpy( p, session->ticket, session->ticket_len );
9345 p += session->ticket_len;
9346 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009347
9348 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9349 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9350 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9351 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009352 }
9353#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9354
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009355 /*
9356 * Misc extension-related info
9357 */
9358#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9359 used += 1;
9360
9361 if( used <= buf_len )
9362 *p++ = session->mfl_code;
9363#endif
9364
9365#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9366 used += 1;
9367
9368 if( used <= buf_len )
9369 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9370#endif
9371
9372#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9373 used += 1;
9374
9375 if( used <= buf_len )
9376 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9377#endif
9378
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009379 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009380 *olen = used;
9381
9382 if( used > buf_len )
9383 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009384
9385 return( 0 );
9386}
9387
9388/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009389 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009390 *
9391 * This internal version is wrapped by a public function that cleans up in
9392 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009393 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009394static int ssl_session_load( mbedtls_ssl_session *session,
9395 const unsigned char *buf,
9396 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009397{
9398 const unsigned char *p = buf;
9399 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009400#if defined(MBEDTLS_HAVE_TIME)
9401 uint64_t start;
9402#endif
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 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009406#endif
9407#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009408
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009409 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009410 * Check version identifier
9411 */
9412
9413 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009415
9416 if( memcmp( p, ssl_serialized_session_header,
9417 sizeof( ssl_serialized_session_header ) ) != 0 )
9418 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009419 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009420 }
9421 p += sizeof( ssl_serialized_session_header );
9422
9423 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009424 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009425 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009426#if defined(MBEDTLS_HAVE_TIME)
9427 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009428 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9429
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009430 start = ( (uint64_t) p[0] << 56 ) |
9431 ( (uint64_t) p[1] << 48 ) |
9432 ( (uint64_t) p[2] << 40 ) |
9433 ( (uint64_t) p[3] << 32 ) |
9434 ( (uint64_t) p[4] << 24 ) |
9435 ( (uint64_t) p[5] << 16 ) |
9436 ( (uint64_t) p[6] << 8 ) |
9437 ( (uint64_t) p[7] );
9438 p += 8;
9439
9440 session->start = (time_t) start;
9441#endif /* MBEDTLS_HAVE_TIME */
9442
9443 /*
9444 * Basic mandatory fields
9445 */
9446 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9448
9449 session->ciphersuite = ( p[0] << 8 ) | p[1];
9450 p += 2;
9451
9452 session->compression = *p++;
9453
9454 session->id_len = *p++;
9455 memcpy( session->id, p, 32 );
9456 p += 32;
9457
9458 memcpy( session->master, p, 48 );
9459 p += 48;
9460
9461 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9462 ( (uint32_t) p[1] << 16 ) |
9463 ( (uint32_t) p[2] << 8 ) |
9464 ( (uint32_t) p[3] );
9465 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009466
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009467 /* Immediately clear invalid pointer values that have been read, in case
9468 * we exit early before we replaced them with valid ones. */
9469#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009470#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009471 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009472#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009473 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009474#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009475#endif
9476#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9477 session->ticket = NULL;
9478#endif
9479
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009480 /*
9481 * Peer certificate
9482 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009483#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009484#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009485 if( 3 > (size_t)( end - p ) )
9486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9487
9488 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9489 p += 3;
9490
9491 if( cert_len == 0 )
9492 {
9493 session->peer_cert = NULL;
9494 }
9495 else
9496 {
9497 int ret;
9498
9499 if( cert_len > (size_t)( end - p ) )
9500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9501
9502 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9503
9504 if( session->peer_cert == NULL )
9505 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9506
9507 mbedtls_x509_crt_init( session->peer_cert );
9508
9509 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9510 p, cert_len ) ) != 0 )
9511 {
9512 mbedtls_x509_crt_free( session->peer_cert );
9513 mbedtls_free( session->peer_cert );
9514 session->peer_cert = NULL;
9515 return( ret );
9516 }
9517
9518 p += cert_len;
9519 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009520#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009521 /* Deserialize CRT digest from the end of the ticket. */
9522 if( 2 > (size_t)( end - p ) )
9523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9524
9525 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9526 session->peer_cert_digest_len = (size_t) *p++;
9527
9528 if( session->peer_cert_digest_len != 0 )
9529 {
Hanno Becker2326d202019-06-06 14:54:55 +01009530 const mbedtls_md_info_t *md_info =
9531 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9532 if( md_info == NULL )
9533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9534 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9536
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009537 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9539
9540 session->peer_cert_digest =
9541 mbedtls_calloc( 1, session->peer_cert_digest_len );
9542 if( session->peer_cert_digest == NULL )
9543 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9544
9545 memcpy( session->peer_cert_digest, p,
9546 session->peer_cert_digest_len );
9547 p += session->peer_cert_digest_len;
9548 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009549#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009550#endif /* MBEDTLS_X509_CRT_PARSE_C */
9551
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009552 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009553 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009554 */
9555#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9556 if( 3 > (size_t)( end - p ) )
9557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9558
9559 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9560 p += 3;
9561
9562 if( session->ticket_len != 0 )
9563 {
9564 if( session->ticket_len > (size_t)( end - p ) )
9565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9566
9567 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9568 if( session->ticket == NULL )
9569 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9570
9571 memcpy( session->ticket, p, session->ticket_len );
9572 p += session->ticket_len;
9573 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009574
9575 if( 4 > (size_t)( end - p ) )
9576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9577
9578 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9579 ( (uint32_t) p[1] << 16 ) |
9580 ( (uint32_t) p[2] << 8 ) |
9581 ( (uint32_t) p[3] );
9582 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009583#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9584
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009585 /*
9586 * Misc extension-related info
9587 */
9588#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9589 if( 1 > (size_t)( end - p ) )
9590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9591
9592 session->mfl_code = *p++;
9593#endif
9594
9595#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9596 if( 1 > (size_t)( end - p ) )
9597 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9598
9599 session->trunc_hmac = *p++;
9600#endif
9601
9602#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9603 if( 1 > (size_t)( end - p ) )
9604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9605
9606 session->encrypt_then_mac = *p++;
9607#endif
9608
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009609 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009610 if( p != end )
9611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9612
9613 return( 0 );
9614}
9615
9616/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009617 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009618 */
9619int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9620 const unsigned char *buf,
9621 size_t len )
9622{
9623 int ret = ssl_session_load( session, buf, len );
9624
9625 if( ret != 0 )
9626 mbedtls_ssl_session_free( session );
9627
9628 return( ret );
9629}
9630
9631/*
Paul Bakker1961b702013-01-25 14:49:24 +01009632 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009637
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009638 if( ssl == NULL || ssl->conf == NULL )
9639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009642 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009644#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009646 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009648#endif
9649
Paul Bakker1961b702013-01-25 14:49:24 +01009650 return( ret );
9651}
9652
9653/*
9654 * Perform the SSL handshake
9655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009657{
9658 int ret = 0;
9659
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009660 if( ssl == NULL || ssl->conf == NULL )
9661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009668
9669 if( ret != 0 )
9670 break;
9671 }
9672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009674
9675 return( ret );
9676}
9677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678#if defined(MBEDTLS_SSL_RENEGOTIATION)
9679#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009680/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009681 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009684{
9685 int ret;
9686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009688
9689 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9691 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009692
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009693 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009694 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009695 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009696 return( ret );
9697 }
9698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009700
9701 return( 0 );
9702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009704
9705/*
9706 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707 * - any side: calling mbedtls_ssl_renegotiate(),
9708 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9709 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009710 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009711 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009715{
9716 int ret;
9717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009719
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009720 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9721 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009722
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009723 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9724 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009726 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009728 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009729 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009730 ssl->handshake->out_msg_seq = 1;
9731 else
9732 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009733 }
9734#endif
9735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9737 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009742 return( ret );
9743 }
9744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009746
9747 return( 0 );
9748}
9749
9750/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009751 * Renegotiate current connection on client,
9752 * or request renegotiation on server
9753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009755{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009757
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009758 if( ssl == NULL || ssl->conf == NULL )
9759 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009762 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009763 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009769
9770 /* Did we already try/start sending HelloRequest? */
9771 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009773
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009774 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009779 /*
9780 * On client, either start the renegotiation process or,
9781 * if already in progress, continue the handshake
9782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9786 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009787
9788 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009791 return( ret );
9792 }
9793 }
9794 else
9795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009799 return( ret );
9800 }
9801 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009803
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009804 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009805}
9806
9807/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009808 * Check record counters and renegotiate if they're above the limit.
9809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009811{
Andres AG2196c7f2016-12-15 17:01:16 +00009812 size_t ep_len = ssl_ep_len( ssl );
9813 int in_ctr_cmp;
9814 int out_ctr_cmp;
9815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9817 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009818 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009819 {
9820 return( 0 );
9821 }
9822
Andres AG2196c7f2016-12-15 17:01:16 +00009823 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9824 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009825 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009826 ssl->conf->renego_period + ep_len, 8 - ep_len );
9827
9828 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009829 {
9830 return( 0 );
9831 }
9832
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009837
9838/*
9839 * Receive application data decrypted from the SSL layer
9840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009842{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009843 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009844 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009845
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009846 if( ssl == NULL || ssl->conf == NULL )
9847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009852 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009855 return( ret );
9856
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009857 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009859 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009860 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009861 return( ret );
9862 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009863 }
9864#endif
9865
Hanno Becker4a810fb2017-05-24 16:27:30 +01009866 /*
9867 * Check if renegotiation is necessary and/or handshake is
9868 * in process. If yes, perform/continue, and fall through
9869 * if an unexpected packet is received while the client
9870 * is waiting for the ServerHello.
9871 *
9872 * (There is no equivalent to the last condition on
9873 * the server-side as it is not treated as within
9874 * a handshake while waiting for the ClientHello
9875 * after a renegotiation request.)
9876 */
9877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009879 ret = ssl_check_ctr_renegotiate( ssl );
9880 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9881 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009884 return( ret );
9885 }
9886#endif
9887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009891 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9892 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009895 return( ret );
9896 }
9897 }
9898
Hanno Beckere41158b2017-10-23 13:30:32 +01009899 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009900 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009901 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009902 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009903 if( ssl->f_get_timer != NULL &&
9904 ssl->f_get_timer( ssl->p_timer ) == -1 )
9905 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009906 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009907 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009908
Hanno Becker327c93b2018-08-15 13:56:18 +01009909 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009910 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009911 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9912 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009913
Hanno Becker4a810fb2017-05-24 16:27:30 +01009914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9915 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009916 }
9917
9918 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009920 {
9921 /*
9922 * OpenSSL sends empty messages to randomize the IV
9923 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009924 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009926 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009927 return( 0 );
9928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009930 return( ret );
9931 }
9932 }
9933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009937
Hanno Becker4a810fb2017-05-24 16:27:30 +01009938 /*
9939 * - For client-side, expect SERVER_HELLO_REQUEST.
9940 * - For server-side, expect CLIENT_HELLO.
9941 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9942 */
9943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009945 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009947 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009950
9951 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009953 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009954 {
9955 continue;
9956 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009957 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009958#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009959#if defined(MBEDTLS_SSL_PROTO_TLS)
9960 {
9961 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9962 }
9963#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009964 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009965#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009966
Hanno Becker4a810fb2017-05-24 16:27:30 +01009967#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009968 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009972
9973 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009975 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009976 {
9977 continue;
9978 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009979 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009980#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009981#if defined(MBEDTLS_SSL_PROTO_TLS)
9982 {
9983 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9984 }
9985#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009986 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009987#endif /* MBEDTLS_SSL_SRV_C */
9988
Hanno Becker21df7f92017-10-17 11:03:26 +01009989#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009990 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009991 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9992 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9993 ssl->conf->allow_legacy_renegotiation ==
9994 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9995 {
9996 /*
9997 * Accept renegotiation request
9998 */
Paul Bakker48916f92012-09-16 19:57:18 +00009999
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010000 /* DTLS clients need to know renego is server-initiated */
10001#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010002 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010003 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10004 {
10005 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10006 }
10007#endif
10008 ret = ssl_start_renegotiation( ssl );
10009 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10010 ret != 0 )
10011 {
10012 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10013 return( ret );
10014 }
10015 }
10016 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010017#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010018 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010019 /*
10020 * Refuse renegotiation
10021 */
10022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010025#if defined(MBEDTLS_SSL_PROTO_SSL3)
10026 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010027 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010028 /* SSLv3 does not have a "no_renegotiation" warning, so
10029 we send a fatal alert and abort the connection. */
10030 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10031 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10032 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010033 }
10034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10036#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10037 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10038 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10041 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10042 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010043 {
10044 return( ret );
10045 }
Paul Bakker48916f92012-09-16 19:57:18 +000010046 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010047 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10049 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010053 }
Paul Bakker48916f92012-09-16 19:57:18 +000010054 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010055
Hanno Becker90333da2017-10-10 11:27:13 +010010056 /* At this point, we don't know whether the renegotiation has been
10057 * completed or not. The cases to consider are the following:
10058 * 1) The renegotiation is complete. In this case, no new record
10059 * has been read yet.
10060 * 2) The renegotiation is incomplete because the client received
10061 * an application data record while awaiting the ServerHello.
10062 * 3) The renegotiation is incomplete because the client received
10063 * a non-handshake, non-application data message while awaiting
10064 * the ServerHello.
10065 * In each of these case, looping will be the proper action:
10066 * - For 1), the next iteration will read a new record and check
10067 * if it's application data.
10068 * - For 2), the loop condition isn't satisfied as application data
10069 * is present, hence continue is the same as break
10070 * - For 3), the loop condition is satisfied and read_record
10071 * will re-deliver the message that was held back by the client
10072 * when expecting the ServerHello.
10073 */
10074 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010075 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010076#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010078 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010079 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010080 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010081 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010084 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010085 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010086 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010087 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010088 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10092 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010095 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010096 }
10097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10101 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010102 }
10103
10104 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010105
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010106 /* We're going to return something now, cancel timer,
10107 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010108 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010109 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010110
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010111#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010112 /* If we requested renego but received AppData, resend HelloRequest.
10113 * Do it now, after setting in_offt, to avoid taking this branch
10114 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010116 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010117 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010118 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010119 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010122 return( ret );
10123 }
10124 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010126#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010127 }
10128
10129 n = ( len < ssl->in_msglen )
10130 ? len : ssl->in_msglen;
10131
10132 memcpy( buf, ssl->in_offt, n );
10133 ssl->in_msglen -= n;
10134
10135 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010136 {
10137 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010138 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010139 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010140 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010141 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010142 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010143 /* more data available */
10144 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010145 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010148
Paul Bakker23986e52011-04-24 08:57:21 +000010149 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010150}
10151
10152/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010153 * Send application data to be encrypted by the SSL layer, taking care of max
10154 * fragment length and buffer size.
10155 *
10156 * According to RFC 5246 Section 6.2.1:
10157 *
10158 * Zero-length fragments of Application data MAY be sent as they are
10159 * potentially useful as a traffic analysis countermeasure.
10160 *
10161 * Therefore, it is possible that the input message length is 0 and the
10162 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010163 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010164static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010165 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010166{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010167 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10168 const size_t max_len = (size_t) ret;
10169
10170 if( ret < 0 )
10171 {
10172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10173 return( ret );
10174 }
10175
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010176 if( len > max_len )
10177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010179 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010182 "maximum fragment length: %d > %d",
10183 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010185 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010186 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010187#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010188#if defined(MBEDTLS_SSL_PROTO_TLS)
10189 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010190 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010191 }
10192#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010193 }
Paul Bakker887bd502011-06-08 13:10:54 +000010194
Paul Bakker5121ce52009-01-03 21:22:43 +000010195 if( ssl->out_left != 0 )
10196 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010197 /*
10198 * The user has previously tried to send the data and
10199 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10200 * written. In this case, we expect the high-level write function
10201 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010206 return( ret );
10207 }
10208 }
Paul Bakker887bd502011-06-08 13:10:54 +000010209 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010210 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010211 /*
10212 * The user is trying to send a message the first time, so we need to
10213 * copy the data into the internal buffers and setup the data structure
10214 * to keep track of partial writes
10215 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010216 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010218 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010219
Hanno Becker67bc7c32018-08-06 11:33:50 +010010220 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010223 return( ret );
10224 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010225 }
10226
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010227 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010228}
10229
10230/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010231 * Write application data, doing 1/n-1 splitting if necessary.
10232 *
10233 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010234 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010235 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010238static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010239 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010240{
10241 int ret;
10242
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010243 if( ssl->conf->cbc_record_splitting ==
10244 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010245 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10247 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10248 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010249 {
10250 return( ssl_write_real( ssl, buf, len ) );
10251 }
10252
10253 if( ssl->split_done == 0 )
10254 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010255 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010256 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010257 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010258 }
10259
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010260 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10261 return( ret );
10262 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010263
10264 return( ret + 1 );
10265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010267
10268/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010269 * Write application data (public-facing wrapper)
10270 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010271int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010272{
10273 int ret;
10274
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010276
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010277 if( ssl == NULL || ssl->conf == NULL )
10278 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10279
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010280#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010281 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10282 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010283 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010284 return( ret );
10285 }
10286#endif
10287
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010288 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010289 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010290 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010291 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010293 return( ret );
10294 }
10295 }
10296
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010297#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010298 ret = ssl_write_split( ssl, buf, len );
10299#else
10300 ret = ssl_write_real( ssl, buf, len );
10301#endif
10302
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010304
10305 return( ret );
10306}
10307
10308/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010309 * Notify the peer that the connection is being closed
10310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010312{
10313 int ret;
10314
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010315 if( ssl == NULL || ssl->conf == NULL )
10316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010319
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010320 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10326 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10327 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010330 return( ret );
10331 }
10332 }
10333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010335
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010336 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010337}
10338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010340{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010341 if( transform == NULL )
10342 return;
10343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010345 deflateEnd( &transform->ctx_deflate );
10346 inflateEnd( &transform->ctx_inflate );
10347#endif
10348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10350 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010351
Hanno Becker92231322018-01-03 15:32:51 +000010352#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353 mbedtls_md_free( &transform->md_ctx_enc );
10354 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010355#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010356
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010357 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010358}
10359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360#if defined(MBEDTLS_X509_CRT_PARSE_C)
10361static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010362{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010364
10365 while( cur != NULL )
10366 {
10367 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010369 cur = next;
10370 }
10371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010373
Hanno Becker0271f962018-08-16 13:23:47 +010010374#if defined(MBEDTLS_SSL_PROTO_DTLS)
10375
10376static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10377{
10378 unsigned offset;
10379 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10380
10381 if( hs == NULL )
10382 return;
10383
Hanno Becker283f5ef2018-08-24 09:34:47 +010010384 ssl_free_buffered_record( ssl );
10385
Hanno Becker0271f962018-08-16 13:23:47 +010010386 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010387 ssl_buffering_free_slot( ssl, offset );
10388}
10389
10390static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10391 uint8_t slot )
10392{
10393 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10394 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010395
10396 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10397 return;
10398
Hanno Beckere605b192018-08-21 15:59:07 +010010399 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010400 {
Hanno Beckere605b192018-08-21 15:59:07 +010010401 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010402 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010403 mbedtls_free( hs_buf->data );
10404 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010405 }
10406}
10407
10408#endif /* MBEDTLS_SSL_PROTO_DTLS */
10409
Gilles Peskine9b562d52018-04-25 20:32:43 +020010410void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010411{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010412 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10413
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010414 if( handshake == NULL )
10415 return;
10416
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010417#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10418 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10419 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010420 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010421 handshake->async_in_progress = 0;
10422 }
10423#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10424
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010425#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10426 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10427 mbedtls_md5_free( &handshake->fin_md5 );
10428 mbedtls_sha1_free( &handshake->fin_sha1 );
10429#endif
10430#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10431#if defined(MBEDTLS_SHA256_C)
10432 mbedtls_sha256_free( &handshake->fin_sha256 );
10433#endif
10434#if defined(MBEDTLS_SHA512_C)
10435 mbedtls_sha512_free( &handshake->fin_sha512 );
10436#endif
10437#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439#if defined(MBEDTLS_DHM_C)
10440 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010441#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442#if defined(MBEDTLS_ECDH_C)
10443 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010444#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010445#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010446 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010447#if defined(MBEDTLS_SSL_CLI_C)
10448 mbedtls_free( handshake->ecjpake_cache );
10449 handshake->ecjpake_cache = NULL;
10450 handshake->ecjpake_cache_len = 0;
10451#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010452#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010453
Janos Follath4ae5c292016-02-10 11:27:43 +000010454#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10455 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010456 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010458#endif
10459
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010460#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10461 if( handshake->psk != NULL )
10462 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010463 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010464 mbedtls_free( handshake->psk );
10465 }
10466#endif
10467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10469 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010470 /*
10471 * Free only the linked list wrapper, not the keys themselves
10472 * since the belong to the SNI callback
10473 */
10474 if( handshake->sni_key_cert != NULL )
10475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010477
10478 while( cur != NULL )
10479 {
10480 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010482 cur = next;
10483 }
10484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010486
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010487#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010488 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010489 if( handshake->ecrs_peer_cert != NULL )
10490 {
10491 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10492 mbedtls_free( handshake->ecrs_peer_cert );
10493 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010494#endif
10495
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010496#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10497 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10498 mbedtls_pk_free( &handshake->peer_pubkey );
10499#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010501#if defined(MBEDTLS_SSL_PROTO_DTLS)
10502 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010503 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010504 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010505#endif
10506
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010507 mbedtls_platform_zeroize( handshake,
10508 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010509}
10510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010511void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010512{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010513 if( session == NULL )
10514 return;
10515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010517 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010518#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010519
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010520#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010521 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010522#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010523
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010524 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010525}
10526
Paul Bakker5121ce52009-01-03 21:22:43 +000010527/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010528 * Serialize a full SSL context
10529 */
10530int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10531 unsigned char *buf,
10532 size_t buf_len,
10533 size_t *olen )
10534{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010535 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010536 (void) ssl;
10537
10538 if( buf != NULL )
10539 memset( buf, 0, buf_len );
10540
10541 *olen = 0;
10542
10543 return( 0 );
10544}
10545
10546/*
10547 * Deserialize a full SSL context
10548 */
10549int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10550 const unsigned char *buf,
10551 size_t len )
10552{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010553 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010554 (void) ssl;
10555 (void) buf;
10556 (void) len;
10557
10558 return( 0 );
10559}
10560
10561/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010562 * Free an SSL context
10563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010564void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010565{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010566 if( ssl == NULL )
10567 return;
10568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010570
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010571 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010572 {
Angus Grattond8213d02016-05-25 20:56:48 +100010573 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010574 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010575 }
10576
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010577 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010578 {
Angus Grattond8213d02016-05-25 20:56:48 +100010579 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010580 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010581 }
10582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010583#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010584 if( ssl->compress_buf != NULL )
10585 {
Angus Grattond8213d02016-05-25 20:56:48 +100010586 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010587 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010588 }
10589#endif
10590
Paul Bakker48916f92012-09-16 19:57:18 +000010591 if( ssl->transform )
10592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010593 mbedtls_ssl_transform_free( ssl->transform );
10594 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010595 }
10596
10597 if( ssl->handshake )
10598 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010599 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010600 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10601 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603 mbedtls_free( ssl->handshake );
10604 mbedtls_free( ssl->transform_negotiate );
10605 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010606 }
10607
Paul Bakkerc0463502013-02-14 11:19:38 +010010608 if( ssl->session )
10609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610 mbedtls_ssl_session_free( ssl->session );
10611 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010612 }
10613
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010614#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010615 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010616 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010617 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010618 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010619 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010620#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10623 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10626 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010627 }
10628#endif
10629
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010630#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010632#endif
10633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010635
Paul Bakker86f04f42013-02-14 11:20:09 +010010636 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010637 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010638}
10639
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010640/*
10641 * Initialze mbedtls_ssl_config
10642 */
10643void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10644{
10645 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010646
10647#if !defined(MBEDTLS_SSL_PROTO_TLS)
10648 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10649#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010650}
10651
Simon Butcherc97b6972015-12-27 23:48:17 +000010652#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010653static int ssl_preset_default_hashes[] = {
10654#if defined(MBEDTLS_SHA512_C)
10655 MBEDTLS_MD_SHA512,
10656 MBEDTLS_MD_SHA384,
10657#endif
10658#if defined(MBEDTLS_SHA256_C)
10659 MBEDTLS_MD_SHA256,
10660 MBEDTLS_MD_SHA224,
10661#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010662#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010663 MBEDTLS_MD_SHA1,
10664#endif
10665 MBEDTLS_MD_NONE
10666};
Simon Butcherc97b6972015-12-27 23:48:17 +000010667#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010668
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010669static int ssl_preset_suiteb_ciphersuites[] = {
10670 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10671 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10672 0
10673};
10674
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010675#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010676static int ssl_preset_suiteb_hashes[] = {
10677 MBEDTLS_MD_SHA256,
10678 MBEDTLS_MD_SHA384,
10679 MBEDTLS_MD_NONE
10680};
10681#endif
10682
10683#if defined(MBEDTLS_ECP_C)
10684static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10685 MBEDTLS_ECP_DP_SECP256R1,
10686 MBEDTLS_ECP_DP_SECP384R1,
10687 MBEDTLS_ECP_DP_NONE
10688};
10689#endif
10690
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010691/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010692 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010693 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010694int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010695 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010696{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010697#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010698 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010699#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010700
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010701 /* Use the functions here so that they are covered in tests,
10702 * but otherwise access member directly for efficiency */
10703 mbedtls_ssl_conf_endpoint( conf, endpoint );
10704 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010705
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010706 /*
10707 * Things that are common to all presets
10708 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010709#if defined(MBEDTLS_SSL_CLI_C)
10710 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10711 {
10712 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10713#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10714 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10715#endif
10716 }
10717#endif
10718
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010719#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010720 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010721#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722
10723#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10724 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10725#endif
10726
10727#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10728 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010729 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010730 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010731#endif
10732
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010733#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10734 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10735#endif
10736
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010737#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010738 conf->f_cookie_write = ssl_cookie_write_dummy;
10739 conf->f_cookie_check = ssl_cookie_check_dummy;
10740#endif
10741
10742#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10743 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10744#endif
10745
Janos Follath088ce432017-04-10 12:42:31 +010010746#if defined(MBEDTLS_SSL_SRV_C)
10747 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10748#endif
10749
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010750#if defined(MBEDTLS_SSL_PROTO_DTLS)
10751 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10752 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10753#endif
10754
10755#if defined(MBEDTLS_SSL_RENEGOTIATION)
10756 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010757 memset( conf->renego_period, 0x00, 2 );
10758 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010759#endif
10760
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010761#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10762 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10763 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010764 const unsigned char dhm_p[] =
10765 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10766 const unsigned char dhm_g[] =
10767 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10768
Hanno Beckera90658f2017-10-04 15:29:08 +010010769 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10770 dhm_p, sizeof( dhm_p ),
10771 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010772 {
10773 return( ret );
10774 }
10775 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010776#endif
10777
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010778 /*
10779 * Preset-specific defaults
10780 */
10781 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010782 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010783 /*
10784 * NSA Suite B
10785 */
10786 case MBEDTLS_SSL_PRESET_SUITEB:
10787 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10788 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10789 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10790 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10791
10792 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10793 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10794 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10795 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10796 ssl_preset_suiteb_ciphersuites;
10797
10798#if defined(MBEDTLS_X509_CRT_PARSE_C)
10799 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010800#endif
10801
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010802#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010803 conf->sig_hashes = ssl_preset_suiteb_hashes;
10804#endif
10805
10806#if defined(MBEDTLS_ECP_C)
10807 conf->curve_list = ssl_preset_suiteb_curves;
10808#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010809 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010810
10811 /*
10812 * Default
10813 */
10814 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010815 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10816 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10817 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10818 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10819 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10820 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10821 MBEDTLS_SSL_MIN_MINOR_VERSION :
10822 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010823 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10824 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10825
10826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010827 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010828 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10829#endif
10830
10831 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10832 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10833 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10834 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10835 mbedtls_ssl_list_ciphersuites();
10836
10837#if defined(MBEDTLS_X509_CRT_PARSE_C)
10838 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10839#endif
10840
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010841#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010842 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010843#endif
10844
10845#if defined(MBEDTLS_ECP_C)
10846 conf->curve_list = mbedtls_ecp_grp_id_list();
10847#endif
10848
10849#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10850 conf->dhm_min_bitlen = 1024;
10851#endif
10852 }
10853
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010854 return( 0 );
10855}
10856
10857/*
10858 * Free mbedtls_ssl_config
10859 */
10860void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10861{
10862#if defined(MBEDTLS_DHM_C)
10863 mbedtls_mpi_free( &conf->dhm_P );
10864 mbedtls_mpi_free( &conf->dhm_G );
10865#endif
10866
10867#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10868 if( conf->psk != NULL )
10869 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010870 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010871 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010872 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010873 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010874 }
10875
10876 if( conf->psk_identity != NULL )
10877 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010878 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010879 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010880 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010881 conf->psk_identity_len = 0;
10882 }
10883#endif
10884
10885#if defined(MBEDTLS_X509_CRT_PARSE_C)
10886 ssl_key_cert_free( conf->key_cert );
10887#endif
10888
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010889 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010890}
10891
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010892#if defined(MBEDTLS_PK_C) && \
10893 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010894/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010895 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010897unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010899#if defined(MBEDTLS_RSA_C)
10900 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10901 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010903#if defined(MBEDTLS_ECDSA_C)
10904 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10905 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010906#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010908}
10909
Hanno Becker7e5437a2017-04-28 17:15:26 +010010910unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10911{
10912 switch( type ) {
10913 case MBEDTLS_PK_RSA:
10914 return( MBEDTLS_SSL_SIG_RSA );
10915 case MBEDTLS_PK_ECDSA:
10916 case MBEDTLS_PK_ECKEY:
10917 return( MBEDTLS_SSL_SIG_ECDSA );
10918 default:
10919 return( MBEDTLS_SSL_SIG_ANON );
10920 }
10921}
10922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010923mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010924{
10925 switch( sig )
10926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927#if defined(MBEDTLS_RSA_C)
10928 case MBEDTLS_SSL_SIG_RSA:
10929 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931#if defined(MBEDTLS_ECDSA_C)
10932 case MBEDTLS_SSL_SIG_ECDSA:
10933 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010934#endif
10935 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010937 }
10938}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010939#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010940
Hanno Becker7e5437a2017-04-28 17:15:26 +010010941#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10942 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10943
10944/* Find an entry in a signature-hash set matching a given hash algorithm. */
10945mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10946 mbedtls_pk_type_t sig_alg )
10947{
10948 switch( sig_alg )
10949 {
10950 case MBEDTLS_PK_RSA:
10951 return( set->rsa );
10952 case MBEDTLS_PK_ECDSA:
10953 return( set->ecdsa );
10954 default:
10955 return( MBEDTLS_MD_NONE );
10956 }
10957}
10958
10959/* Add a signature-hash-pair to a signature-hash set */
10960void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10961 mbedtls_pk_type_t sig_alg,
10962 mbedtls_md_type_t md_alg )
10963{
10964 switch( sig_alg )
10965 {
10966 case MBEDTLS_PK_RSA:
10967 if( set->rsa == MBEDTLS_MD_NONE )
10968 set->rsa = md_alg;
10969 break;
10970
10971 case MBEDTLS_PK_ECDSA:
10972 if( set->ecdsa == MBEDTLS_MD_NONE )
10973 set->ecdsa = md_alg;
10974 break;
10975
10976 default:
10977 break;
10978 }
10979}
10980
10981/* Allow exactly one hash algorithm for each signature. */
10982void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10983 mbedtls_md_type_t md_alg )
10984{
10985 set->rsa = md_alg;
10986 set->ecdsa = md_alg;
10987}
10988
10989#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10990 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10991
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010992/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010993 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010995mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010996{
10997 switch( hash )
10998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010999#if defined(MBEDTLS_MD5_C)
11000 case MBEDTLS_SSL_HASH_MD5:
11001 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003#if defined(MBEDTLS_SHA1_C)
11004 case MBEDTLS_SSL_HASH_SHA1:
11005 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011007#if defined(MBEDTLS_SHA256_C)
11008 case MBEDTLS_SSL_HASH_SHA224:
11009 return( MBEDTLS_MD_SHA224 );
11010 case MBEDTLS_SSL_HASH_SHA256:
11011 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011012#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011013#if defined(MBEDTLS_SHA512_C)
11014 case MBEDTLS_SSL_HASH_SHA384:
11015 return( MBEDTLS_MD_SHA384 );
11016 case MBEDTLS_SSL_HASH_SHA512:
11017 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011018#endif
11019 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011021 }
11022}
11023
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011024/*
11025 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11026 */
11027unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11028{
11029 switch( md )
11030 {
11031#if defined(MBEDTLS_MD5_C)
11032 case MBEDTLS_MD_MD5:
11033 return( MBEDTLS_SSL_HASH_MD5 );
11034#endif
11035#if defined(MBEDTLS_SHA1_C)
11036 case MBEDTLS_MD_SHA1:
11037 return( MBEDTLS_SSL_HASH_SHA1 );
11038#endif
11039#if defined(MBEDTLS_SHA256_C)
11040 case MBEDTLS_MD_SHA224:
11041 return( MBEDTLS_SSL_HASH_SHA224 );
11042 case MBEDTLS_MD_SHA256:
11043 return( MBEDTLS_SSL_HASH_SHA256 );
11044#endif
11045#if defined(MBEDTLS_SHA512_C)
11046 case MBEDTLS_MD_SHA384:
11047 return( MBEDTLS_SSL_HASH_SHA384 );
11048 case MBEDTLS_MD_SHA512:
11049 return( MBEDTLS_SSL_HASH_SHA512 );
11050#endif
11051 default:
11052 return( MBEDTLS_SSL_HASH_NONE );
11053 }
11054}
11055
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011056#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011057/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011058 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011059 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011060 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011061int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011063 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011064
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011065 if( ssl->conf->curve_list == NULL )
11066 return( -1 );
11067
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011068 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011069 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011070 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011071
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011072 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011073}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011074#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011075
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011076#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011077/*
11078 * Check if a hash proposed by the peer is in our list.
11079 * Return 0 if we're willing to use it, -1 otherwise.
11080 */
11081int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11082 mbedtls_md_type_t md )
11083{
11084 const int *cur;
11085
11086 if( ssl->conf->sig_hashes == NULL )
11087 return( -1 );
11088
11089 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11090 if( *cur == (int) md )
11091 return( 0 );
11092
11093 return( -1 );
11094}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011095#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011097#if defined(MBEDTLS_X509_CRT_PARSE_C)
11098int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11099 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011100 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011101 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011102{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011103 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011104#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011105 int usage = 0;
11106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011107#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011108 const char *ext_oid;
11109 size_t ext_len;
11110#endif
11111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011112#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11113 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011114 ((void) cert);
11115 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011116 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011117#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011119#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11120 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011121 {
11122 /* Server part of the key exchange */
11123 switch( ciphersuite->key_exchange )
11124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011125 case MBEDTLS_KEY_EXCHANGE_RSA:
11126 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011127 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011128 break;
11129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011130 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11131 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11132 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11133 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011134 break;
11135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011136 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11137 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011138 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011139 break;
11140
11141 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011142 case MBEDTLS_KEY_EXCHANGE_NONE:
11143 case MBEDTLS_KEY_EXCHANGE_PSK:
11144 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11145 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011146 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011147 usage = 0;
11148 }
11149 }
11150 else
11151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011152 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11153 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011154 }
11155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011156 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011157 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011158 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011159 ret = -1;
11160 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011161#else
11162 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011163#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011165#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11166 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011168 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11169 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011170 }
11171 else
11172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011173 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11174 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011175 }
11176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011177 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011178 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011179 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011180 ret = -1;
11181 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011182#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011183
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011184 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011185}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011186#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011187
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011188/*
11189 * Convert version numbers to/from wire format
11190 * and, for DTLS, to/from TLS equivalent.
11191 *
11192 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011193 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011194 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11195 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011197void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011198 unsigned char ver[2] )
11199{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011200#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11201 ((void) transport);
11202#endif
11203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011204#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011205 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011207 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011208 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11209
11210 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11211 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11212 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011213 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011214#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011215#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011216 {
11217 ver[0] = (unsigned char) major;
11218 ver[1] = (unsigned char) minor;
11219 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011220#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011221}
11222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011223void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011224 const unsigned char ver[2] )
11225{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011226#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11227 ((void) transport);
11228#endif
11229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011231 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011232 {
11233 *major = 255 - ver[0] + 2;
11234 *minor = 255 - ver[1] + 1;
11235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011236 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011237 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11238 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011239 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011240#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011241#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011242 {
11243 *major = ver[0];
11244 *minor = ver[1];
11245 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011246#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011247}
11248
Simon Butcher99000142016-10-13 17:21:01 +010011249int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11250{
11251#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11252 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11253 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11254
11255 switch( md )
11256 {
11257#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11258#if defined(MBEDTLS_MD5_C)
11259 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011260 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011261#endif
11262#if defined(MBEDTLS_SHA1_C)
11263 case MBEDTLS_SSL_HASH_SHA1:
11264 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11265 break;
11266#endif
11267#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11268#if defined(MBEDTLS_SHA512_C)
11269 case MBEDTLS_SSL_HASH_SHA384:
11270 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11271 break;
11272#endif
11273#if defined(MBEDTLS_SHA256_C)
11274 case MBEDTLS_SSL_HASH_SHA256:
11275 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11276 break;
11277#endif
11278 default:
11279 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11280 }
11281
11282 return 0;
11283#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11284 (void) ssl;
11285 (void) md;
11286
11287 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11288#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11289}
11290
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011291#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11292 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11293int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11294 unsigned char *output,
11295 unsigned char *data, size_t data_len )
11296{
11297 int ret = 0;
11298 mbedtls_md5_context mbedtls_md5;
11299 mbedtls_sha1_context mbedtls_sha1;
11300
11301 mbedtls_md5_init( &mbedtls_md5 );
11302 mbedtls_sha1_init( &mbedtls_sha1 );
11303
11304 /*
11305 * digitally-signed struct {
11306 * opaque md5_hash[16];
11307 * opaque sha_hash[20];
11308 * };
11309 *
11310 * md5_hash
11311 * MD5(ClientHello.random + ServerHello.random
11312 * + ServerParams);
11313 * sha_hash
11314 * SHA(ClientHello.random + ServerHello.random
11315 * + ServerParams);
11316 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011317 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011318 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011320 goto exit;
11321 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011322 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011323 ssl->handshake->randbytes, 64 ) ) != 0 )
11324 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011326 goto exit;
11327 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011328 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011329 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011331 goto exit;
11332 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011333 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011334 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011336 goto exit;
11337 }
11338
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011339 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011340 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011342 goto exit;
11343 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011344 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345 ssl->handshake->randbytes, 64 ) ) != 0 )
11346 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011348 goto exit;
11349 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011350 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011351 data_len ) ) != 0 )
11352 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354 goto exit;
11355 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011356 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011357 output + 16 ) ) != 0 )
11358 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011360 goto exit;
11361 }
11362
11363exit:
11364 mbedtls_md5_free( &mbedtls_md5 );
11365 mbedtls_sha1_free( &mbedtls_sha1 );
11366
11367 if( ret != 0 )
11368 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11369 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11370
11371 return( ret );
11372
11373}
11374#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11375 MBEDTLS_SSL_PROTO_TLS1_1 */
11376
11377#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11378 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11379int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011380 unsigned char *hash, size_t *hashlen,
11381 unsigned char *data, size_t data_len,
11382 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011383{
11384 int ret = 0;
11385 mbedtls_md_context_t ctx;
11386 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011387 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011388
11389 mbedtls_md_init( &ctx );
11390
11391 /*
11392 * digitally-signed struct {
11393 * opaque client_random[32];
11394 * opaque server_random[32];
11395 * ServerDHParams params;
11396 * };
11397 */
11398 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11399 {
11400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11401 goto exit;
11402 }
11403 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11404 {
11405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11406 goto exit;
11407 }
11408 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11409 {
11410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11411 goto exit;
11412 }
11413 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11414 {
11415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11416 goto exit;
11417 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011418 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011419 {
11420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11421 goto exit;
11422 }
11423
11424exit:
11425 mbedtls_md_free( &ctx );
11426
11427 if( ret != 0 )
11428 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11429 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11430
11431 return( ret );
11432}
11433#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11434 MBEDTLS_SSL_PROTO_TLS1_2 */
11435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011436#endif /* MBEDTLS_SSL_TLS_C */