blob: 65cdd49d80bde0a0b2d3fcd214ad103e6cbf88bc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Hanno Becker58fccf22019-02-06 14:30:46 +0000371int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
372 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000378
379#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380 if( src->peer_cert != NULL )
381 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200382 int ret;
383
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200384 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200391 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 dst->peer_cert = NULL;
395 return( ret );
396 }
397 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100398#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000399 if( src->peer_cert_digest != NULL )
400 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000401 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000402 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000409 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000410 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200414
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200415#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200416 if( src->ticket != NULL )
417 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200418 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200419 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200421
422 memcpy( dst->ticket, src->ticket, src->ticket_len );
423 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200424#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200425
426 return( 0 );
427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
430int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200431 const unsigned char *key_enc, const unsigned char *key_dec,
432 size_t keylen,
433 const unsigned char *iv_enc, const unsigned char *iv_dec,
434 size_t ivlen,
435 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200436 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
438int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
441int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
442#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444/*
445 * Key material generation
446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200448static int ssl3_prf( const unsigned char *secret, size_t slen,
449 const char *label,
450 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000451 unsigned char *dstbuf, size_t dlen )
452{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100453 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000454 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200455 mbedtls_md5_context md5;
456 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000457 unsigned char padding[16];
458 unsigned char sha1sum[20];
459 ((void)label);
460
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200461 mbedtls_md5_init( &md5 );
462 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200463
Paul Bakker5f70b252012-09-13 14:23:06 +0000464 /*
465 * SSLv3:
466 * block =
467 * MD5( secret + SHA1( 'A' + secret + random ) ) +
468 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
469 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
470 * ...
471 */
472 for( i = 0; i < dlen / 16; i++ )
473 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200474 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000475
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100478 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100479 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100480 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100482 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100484 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100485 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100489 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100490 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000495 }
496
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200498 mbedtls_md5_free( &md5 );
499 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000500
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500501 mbedtls_platform_zeroize( padding, sizeof( padding ) );
502 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000503
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100504 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200509static int tls1_prf( const unsigned char *secret, size_t slen,
510 const char *label,
511 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000512 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000513{
Paul Bakker23986e52011-04-24 08:57:21 +0000514 size_t nb, hs;
515 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200516 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 unsigned char tmp[128];
518 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_md_info_t *md_info;
520 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100521 int ret;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 hs = ( slen + 1 ) / 2;
529 S1 = secret;
530 S2 = secret + slen - hs;
531
532 nb = strlen( label );
533 memcpy( tmp + 20, label, nb );
534 memcpy( tmp + 20 + nb, random, rlen );
535 nb += rlen;
536
537 /*
538 * First compute P_md5(secret,label+random)[0..dlen]
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544 return( ret );
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
547 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
548 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 for( i = 0; i < dlen; i += 16 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md_hmac_reset ( &md_ctx );
553 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
554 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_reset ( &md_ctx );
557 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
558 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
561
562 for( j = 0; j < k; j++ )
563 dstbuf[i + j] = h_i[j];
564 }
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 /*
569 * XOR out with P_sha1(secret,label+random)[0..dlen]
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100575 return( ret );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
578 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
579 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 for( i = 0; i < dlen; i += 20 )
582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_md_hmac_reset ( &md_ctx );
584 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
585 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_md_hmac_reset ( &md_ctx );
588 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
589 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
592
593 for( j = 0; j < k; j++ )
594 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
595 }
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500599 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
600 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602 return( 0 );
603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
607static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608 const unsigned char *secret, size_t slen,
609 const char *label,
610 const unsigned char *random, size_t rlen,
611 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612{
613 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100614 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
617 const mbedtls_md_info_t *md_info;
618 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 int ret;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627
628 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000630
631 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100632 memcpy( tmp + md_len, label, nb );
633 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634 nb += rlen;
635
636 /*
637 * Compute P_<hash>(secret, label + random)[0..dlen]
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100640 return( ret );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
643 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
644 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100645
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100646 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_md_hmac_reset ( &md_ctx );
649 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
650 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_md_hmac_reset ( &md_ctx );
653 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
654 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000655
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000657
658 for( j = 0; j < k; j++ )
659 dstbuf[i + j] = h_i[j];
660 }
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100663
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500664 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
665 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000666
667 return( 0 );
668}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100671static int tls_prf_sha256( const unsigned char *secret, size_t slen,
672 const char *label,
673 const unsigned char *random, size_t rlen,
674 unsigned char *dstbuf, size_t dlen )
675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100677 label, random, rlen, dstbuf, dlen ) );
678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200682static int tls_prf_sha384( const unsigned char *secret, size_t slen,
683 const char *label,
684 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000685 unsigned char *dstbuf, size_t dlen )
686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100688 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_SHA512_C */
691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
696 defined(MBEDTLS_SSL_PROTO_TLS1_1)
697static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200698#endif
Paul Bakker380da532012-04-18 16:10:25 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200701static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200703#endif
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200706static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200708#endif
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
711#if defined(MBEDTLS_SHA256_C)
712static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200713static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200715#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SHA512_C)
718static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200719static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000723
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200724/* Type for the TLS PRF */
725typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
726 const unsigned char *, size_t,
727 unsigned char *, size_t);
728
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200729/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200730 * Populate a transform structure with session keys and all the other
731 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200732 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200733 * Parameters:
734 * - [in/out]: transform: structure to populate
735 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200736 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200737 * - [in] ciphersuite
738 * - [in] master
739 * - [in] encrypt_then_mac
740 * - [in] trunc_hmac
741 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742 * - [in] tls_prf: pointer to PRF to use for key derivation
743 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200744 * - [in] minor_ver: SSL/TLS minor version
745 * - [in] endpoint: client or server
746 * - [in] ssl: optionally used for:
747 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
748 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
749 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200750 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200752 int ciphersuite,
753 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100754#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
756 int encrypt_then_mac,
757#endif
758#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
759 int trunc_hmac,
760#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100761#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200762#if defined(MBEDTLS_ZLIB_SUPPORT)
763 int compression,
764#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200765 ssl_tls_prf_t tls_prf,
766 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200767 int minor_ver,
768 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200769 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000770{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 unsigned char keyblk[256];
773 unsigned char *key1;
774 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100775 unsigned char *mac_enc;
776 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000777 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200778 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000779 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 const mbedtls_cipher_info_t *cipher_info;
782 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100783
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200784#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
785 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
786 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200787 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200788 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000789#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000790
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200791 /* Copy info about negotiated version and extensions */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100792#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
793 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200794 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000795#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200796 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 /*
799 * Get various info structures
800 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 if( ciphersuite_info == NULL )
803 {
804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200805 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
807 }
808
Hanno Becker8759e162017-12-27 21:34:08 +0000809 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100810 if( cipher_info == NULL )
811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000813 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100815 }
816
Hanno Becker8759e162017-12-27 21:34:08 +0000817 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100818 if( md_info == NULL )
819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000821 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100823 }
824
Hanno Beckera5a2b082019-05-15 14:03:01 +0100825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100826 /* Copy own and peer's CID if the use of the CID
827 * extension has been negotiated. */
828 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
829 {
830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100831
Hanno Becker4932f9f2019-05-03 15:23:51 +0100832 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100833 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100834 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100835 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100836
837 transform->out_cid_len = ssl->handshake->peer_cid_len;
838 memcpy( transform->out_cid, ssl->handshake->peer_cid,
839 ssl->handshake->peer_cid_len );
840 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
841 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100842 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100843#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100844
Paul Bakker5121ce52009-01-03 21:22:43 +0000845 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200846 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000847 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200848 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100849 if( ret != 0 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100852 return( ret );
853 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200856 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200857 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200858 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 /*
862 * Determine the appropriate key, IV and MAC length.
863 */
Paul Bakker68884e32013-01-07 18:20:04 +0100864
Hanno Beckere7f2df02017-12-27 08:17:40 +0000865 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
Hanno Beckerf1229442018-01-03 15:32:31 +0000867#if defined(MBEDTLS_GCM_C) || \
868 defined(MBEDTLS_CCM_C) || \
869 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871 cipher_info->mode == MBEDTLS_MODE_CCM ||
872 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 {
Hanno Becker8759e162017-12-27 21:34:08 +0000874 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200875
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000877 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000878 transform->taglen =
879 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200880
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200881 /* All modes haves 96-bit IVs;
882 * GCM and CCM has 4 implicit and 8 explicit bytes
883 * ChachaPoly has all 12 bytes implicit
884 */
Paul Bakker68884e32013-01-07 18:20:04 +0100885 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200886 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
887 transform->fixed_ivlen = 12;
888 else
889 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200891 /* Minimum length of encrypted record */
892 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000893 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100894 }
895 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000896#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
897#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
898 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
899 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200901 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
903 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100907 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 mac_key_len = mbedtls_md_get_size( md_info );
911 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200914 /*
915 * If HMAC is to be truncated, we shall keep the leftmost bytes,
916 * (rfc 6066 page 13 or rfc 2104 section 4),
917 * so we only need to adjust the length here.
918 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200919 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000922
923#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
924 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000925 * HMAC implementation which also truncates the key
926 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000927 mac_key_len = transform->maclen;
928#endif
929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931
932 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100933 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200935 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937 transform->minlen = transform->maclen;
938 else
Paul Bakker68884e32013-01-07 18:20:04 +0100939 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940 /*
941 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100942 * 1. if EtM is in use: one block plus MAC
943 * otherwise: * first multiple of blocklen greater than maclen
944 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200947 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100948 {
949 transform->minlen = transform->maclen
950 + cipher_info->block_size;
951 }
952 else
953#endif
954 {
955 transform->minlen = transform->maclen
956 + cipher_info->block_size
957 - transform->maclen % cipher_info->block_size;
958 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200961 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
962 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200963 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100964 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200967 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
968 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200969 {
970 transform->minlen += transform->ivlen;
971 }
972 else
973#endif
974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200977 }
Paul Bakker68884e32013-01-07 18:20:04 +0100978 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000980 else
981#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
985 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Hanno Beckere7f2df02017-12-27 08:17:40 +0000987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
988 (unsigned) keylen,
989 (unsigned) transform->minlen,
990 (unsigned) transform->ivlen,
991 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
993 /*
994 * Finally setup the cipher contexts, IVs and MAC secrets.
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200997 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001000 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker68884e32013-01-07 18:20:04 +01001002 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001005 /*
1006 * This is not used in TLS v1.1.
1007 */
Paul Bakker48916f92012-09-16 19:57:18 +00001008 iv_copy_len = ( transform->fixed_ivlen ) ?
1009 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001010 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1011 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 }
1014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_SSL_CLI_C */
1016#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001017 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001019 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker81c7b182017-11-09 18:39:33 +00001022 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001023 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001025 /*
1026 * This is not used in TLS v1.1.
1027 */
Paul Bakker48916f92012-09-16 19:57:18 +00001028 iv_copy_len = ( transform->fixed_ivlen ) ?
1029 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001030 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1031 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001032 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Hanno Becker92231322018-01-03 15:32:51 +00001041#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001043 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001044 {
Hanno Becker92231322018-01-03 15:32:51 +00001045 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001049 }
1050
Hanno Becker81c7b182017-11-09 18:39:33 +00001051 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1052 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001053 }
1054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1056#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1057 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001058 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001059 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001060 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1061 For AEAD-based ciphersuites, there is nothing to do here. */
1062 if( mac_key_len != 0 )
1063 {
1064 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1065 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1066 }
Paul Bakker68884e32013-01-07 18:20:04 +01001067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001068 else
1069#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001073 }
Hanno Becker92231322018-01-03 15:32:51 +00001074#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1077 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001078 {
1079 int ret = 0;
1080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001082
Hanno Beckere7f2df02017-12-27 08:17:40 +00001083 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001084 transform->iv_enc, transform->iv_dec,
1085 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001086 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001087 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001091 }
1092 }
Hanno Becker92231322018-01-03 15:32:51 +00001093#else
1094 ((void) mac_dec);
1095 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001097
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001098#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1099 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001101 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001102 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001103 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001104 iv_copy_len );
1105 }
1106#endif
1107
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001108 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 return( ret );
1113 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001114
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001115 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 cipher_info ) ) != 0 )
1117 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001119 return( ret );
1120 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001123 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127 return( ret );
1128 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001131 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001135 return( ret );
1136 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_CIPHER_MODE_CBC)
1139 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1142 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001145 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001146 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1149 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001152 return( ret );
1153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001157 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001159 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001161 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001164
Paul Bakker48916f92012-09-16 19:57:18 +00001165 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1166 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001168 if( deflateInit( &transform->ctx_deflate,
1169 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001170 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1173 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001174 }
1175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001177
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 return( 0 );
1179}
1180
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001181/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001182 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001183 *
1184 * Inputs:
1185 * - SSL/TLS minor version
1186 * - hash associated with the ciphersuite (only used by TLS 1.2)
1187 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001188 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001189 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1190 */
1191static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1192 int minor_ver,
1193 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001195#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1196 (void) hash;
1197#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001198
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001201 {
1202 handshake->tls_prf = ssl3_prf;
1203 handshake->calc_verify = ssl_calc_verify_ssl;
1204 handshake->calc_finished = ssl_calc_finished_ssl;
1205 }
1206 else
1207#endif
1208#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001209 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001210 {
1211 handshake->tls_prf = tls1_prf;
1212 handshake->calc_verify = ssl_calc_verify_tls;
1213 handshake->calc_finished = ssl_calc_finished_tls;
1214 }
1215 else
1216#endif
1217#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1218#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001219 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1220 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001221 {
1222 handshake->tls_prf = tls_prf_sha384;
1223 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1224 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1225 }
1226 else
1227#endif
1228#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001229 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001230 {
1231 handshake->tls_prf = tls_prf_sha256;
1232 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1233 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1234 }
1235 else
1236#endif
1237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1238 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1240 }
1241
1242 return( 0 );
1243}
1244
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001245/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001246 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001247 *
1248 * Parameters:
1249 * [in/out] handshake
1250 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1251 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 * [out] master
1253 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001255static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001257 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001258{
1259 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001260
1261#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001262 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001263 (void) ssl;
1264#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001265
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001266 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001267 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1269 return( 0 );
1270 }
1271
1272 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1273 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001274
1275#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001276 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1277 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001278 {
1279 unsigned char session_hash[48];
1280 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001281
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001282 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001283
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001284 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1285 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001286
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001287 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001288 "extended master secret",
1289 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001290 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001291 }
1292 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001293#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001294 {
1295 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1296 "master secret",
1297 handshake->randbytes, 64,
1298 master, 48 );
1299 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001300 if( ret != 0 )
1301 {
1302 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1303 return( ret );
1304 }
1305
1306 mbedtls_platform_zeroize( handshake->premaster,
1307 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001308
1309 return( 0 );
1310}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001311
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001312int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1313{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001314 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001315 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1316 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001317
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1319
1320 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001321 ret = ssl_set_handshake_prfs( ssl->handshake,
1322 ssl->minor_ver,
1323 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001324 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001325 {
1326 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001327 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001328 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001329
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001330 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001331 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001332 ssl->session_negotiate->master,
1333 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001334 if( ret != 0 )
1335 {
1336 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1337 return( ret );
1338 }
1339
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001340 /* Swap the client and server random values:
1341 * - MS derivation wanted client+server (RFC 5246 8.1)
1342 * - key derivation wants server+client (RFC 5246 6.3) */
1343 {
1344 unsigned char tmp[64];
1345 memcpy( tmp, ssl->handshake->randbytes, 64 );
1346 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1347 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1348 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1349 }
1350
1351 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001352 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001353 ssl->session_negotiate->ciphersuite,
1354 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001355#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001356#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1357 ssl->session_negotiate->encrypt_then_mac,
1358#endif
1359#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1360 ssl->session_negotiate->trunc_hmac,
1361#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001362#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001363#if defined(MBEDTLS_ZLIB_SUPPORT)
1364 ssl->session_negotiate->compression,
1365#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001366 ssl->handshake->tls_prf,
1367 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001368 ssl->minor_ver,
1369 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001370 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001371 if( ret != 0 )
1372 {
1373 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1374 return( ret );
1375 }
1376
1377 /* We no longer need Server/ClientHello.random values */
1378 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1379 sizeof( ssl->handshake->randbytes ) );
1380
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001381 /* Allocate compression buffer */
1382#if defined(MBEDTLS_ZLIB_SUPPORT)
1383 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1384 ssl->compress_buf == NULL )
1385 {
1386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1387 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1388 if( ssl->compress_buf == NULL )
1389 {
1390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001391 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001392 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1393 }
1394 }
1395#endif
1396
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1398
1399 return( 0 );
1400}
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001403void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1404 unsigned char hash[36],
1405 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001407 mbedtls_md5_context md5;
1408 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 unsigned char pad_1[48];
1410 unsigned char pad_2[48];
1411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001414 mbedtls_md5_init( &md5 );
1415 mbedtls_sha1_init( &sha1 );
1416
1417 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1418 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Paul Bakker380da532012-04-18 16:10:25 +00001420 memset( pad_1, 0x36, 48 );
1421 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001422
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001423 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1424 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1425 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001427 mbedtls_md5_starts_ret( &md5 );
1428 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1429 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1430 mbedtls_md5_update_ret( &md5, hash, 16 );
1431 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001433 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1434 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1435 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001436
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001437 mbedtls_sha1_starts_ret( &sha1 );
1438 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1439 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1440 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1441 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001443 *hlen = 36;
1444
1445 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001447
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001448 mbedtls_md5_free( &md5 );
1449 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001450
Paul Bakker380da532012-04-18 16:10:25 +00001451 return;
1452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001456void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1457 unsigned char hash[36],
1458 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001459{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001460 mbedtls_md5_context md5;
1461 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001464
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001465 mbedtls_md5_init( &md5 );
1466 mbedtls_sha1_init( &sha1 );
1467
1468 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1469 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001470
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001471 mbedtls_md5_finish_ret( &md5, hash );
1472 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001473
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001474 *hlen = 36;
1475
1476 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001479 mbedtls_md5_free( &md5 );
1480 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001481
Paul Bakker380da532012-04-18 16:10:25 +00001482 return;
1483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1487#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001488void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1489 unsigned char hash[32],
1490 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001491{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001492 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001494 mbedtls_sha256_init( &sha256 );
1495
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001497
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001498 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001499 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001501 *hlen = 32;
1502
1503 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001505
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001506 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001507
Paul Bakker380da532012-04-18 16:10:25 +00001508 return;
1509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001513void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1514 unsigned char hash[48],
1515 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001516{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001517 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001518
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001519 mbedtls_sha512_init( &sha512 );
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001522
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001523 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001524 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001525
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001526 *hlen = 48;
1527
1528 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001531 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001532
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 return;
1534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_SHA512_C */
1536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1539int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001540{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001541 unsigned char *p = ssl->handshake->premaster;
1542 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001543 const unsigned char *psk = ssl->conf->psk;
1544 size_t psk_len = ssl->conf->psk_len;
1545
1546 /* If the psk callback was called, use its result */
1547 if( ssl->handshake->psk != NULL )
1548 {
1549 psk = ssl->handshake->psk;
1550 psk_len = ssl->handshake->psk_len;
1551 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001552
1553 /*
1554 * PMS = struct {
1555 * opaque other_secret<0..2^16-1>;
1556 * opaque psk<0..2^16-1>;
1557 * };
1558 * with "other_secret" depending on the particular key exchange
1559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1561 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001562 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001563 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001565
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001566 *(p++) = (unsigned char)( psk_len >> 8 );
1567 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001568
1569 if( end < p || (size_t)( end - p ) < psk_len )
1570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1571
1572 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001573 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001574 }
1575 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1577#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1578 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001579 {
1580 /*
1581 * other_secret already set by the ClientKeyExchange message,
1582 * and is 48 bytes long
1583 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001584 if( end - p < 2 )
1585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1586
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001587 *p++ = 0;
1588 *p++ = 48;
1589 p += 48;
1590 }
1591 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1593#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1594 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001595 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001596 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001597 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001598
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001599 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001601 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001602 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605 return( ret );
1606 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001607 *(p++) = (unsigned char)( len >> 8 );
1608 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001609 p += len;
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 }
1613 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1615#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1616 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001617 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001618 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 size_t zlen;
1620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001622 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001623 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626 return( ret );
1627 }
1628
1629 *(p++) = (unsigned char)( zlen >> 8 );
1630 *(p++) = (unsigned char)( zlen );
1631 p += zlen;
1632
Janos Follath3fbdada2018-08-15 10:26:53 +01001633 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1634 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001635 }
1636 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001641 }
1642
1643 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001644 if( end - p < 2 )
1645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001646
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001647 *(p++) = (unsigned char)( psk_len >> 8 );
1648 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001649
1650 if( end < p || (size_t)( end - p ) < psk_len )
1651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1652
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001653 memcpy( p, psk, psk_len );
1654 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001655
1656 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1657
1658 return( 0 );
1659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001663/*
1664 * SSLv3.0 MAC functions
1665 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001666#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001667static void ssl_mac( mbedtls_md_context_t *md_ctx,
1668 const unsigned char *secret,
1669 const unsigned char *buf, size_t len,
1670 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001671 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001672{
1673 unsigned char header[11];
1674 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001675 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1677 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001678
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001679 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001681 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001682 else
Paul Bakker68884e32013-01-07 18:20:04 +01001683 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 memcpy( header, ctr, 8 );
1686 header[ 8] = (unsigned char) type;
1687 header[ 9] = (unsigned char)( len >> 8 );
1688 header[10] = (unsigned char)( len );
1689
Paul Bakker68884e32013-01-07 18:20:04 +01001690 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 mbedtls_md_starts( md_ctx );
1692 mbedtls_md_update( md_ctx, secret, md_size );
1693 mbedtls_md_update( md_ctx, padding, padlen );
1694 mbedtls_md_update( md_ctx, header, 11 );
1695 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001696 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Paul Bakker68884e32013-01-07 18:20:04 +01001698 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_md_starts( md_ctx );
1700 mbedtls_md_update( md_ctx, secret, md_size );
1701 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001702 mbedtls_md_update( md_ctx, out, md_size );
1703 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001706
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001707/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001708 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001709#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001710 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1711 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1712 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1713/* This function makes sure every byte in the memory region is accessed
1714 * (in ascending addresses order) */
1715static void ssl_read_memory( unsigned char *p, size_t len )
1716{
1717 unsigned char acc = 0;
1718 volatile unsigned char force;
1719
1720 for( ; len != 0; p++, len-- )
1721 acc ^= *p;
1722
1723 force = acc;
1724 (void) force;
1725}
1726#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1727
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001728/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001730 */
Hanno Becker3307b532017-12-27 21:37:21 +00001731
Hanno Beckera5a2b082019-05-15 14:03:01 +01001732#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001733/* This functions transforms a DTLS plaintext fragment and a record content
1734 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001735 *
1736 * struct {
1737 * opaque content[DTLSPlaintext.length];
1738 * ContentType real_type;
1739 * uint8 zeros[length_of_padding];
1740 * } DTLSInnerPlaintext;
1741 *
1742 * Input:
1743 * - `content`: The beginning of the buffer holding the
1744 * plaintext to be wrapped.
1745 * - `*content_size`: The length of the plaintext in Bytes.
1746 * - `max_len`: The number of Bytes available starting from
1747 * `content`. This must be `>= *content_size`.
1748 * - `rec_type`: The desired record content type.
1749 *
1750 * Output:
1751 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1752 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1753 *
1754 * Returns:
1755 * - `0` on success.
1756 * - A negative error code if `max_len` didn't offer enough space
1757 * for the expansion.
1758 */
1759static int ssl_cid_build_inner_plaintext( unsigned char *content,
1760 size_t *content_size,
1761 size_t remaining,
1762 uint8_t rec_type )
1763{
1764 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001765 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1766 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1767 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001768
1769 /* Write real content type */
1770 if( remaining == 0 )
1771 return( -1 );
1772 content[ len ] = rec_type;
1773 len++;
1774 remaining--;
1775
1776 if( remaining < pad )
1777 return( -1 );
1778 memset( content + len, 0, pad );
1779 len += pad;
1780 remaining -= pad;
1781
1782 *content_size = len;
1783 return( 0 );
1784}
1785
Hanno Becker7dc25772019-05-20 15:08:01 +01001786/* This function parses a DTLSInnerPlaintext structure.
1787 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001788static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1789 size_t *content_size,
1790 uint8_t *rec_type )
1791{
1792 size_t remaining = *content_size;
1793
1794 /* Determine length of padding by skipping zeroes from the back. */
1795 do
1796 {
1797 if( remaining == 0 )
1798 return( -1 );
1799 remaining--;
1800 } while( content[ remaining ] == 0 );
1801
1802 *content_size = remaining;
1803 *rec_type = content[ remaining ];
1804
1805 return( 0 );
1806}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001807#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001808
Hanno Becker99abf512019-05-20 14:50:53 +01001809/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001810 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001811static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001812 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001813 mbedtls_record *rec )
1814{
Hanno Becker99abf512019-05-20 14:50:53 +01001815 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001816 *
1817 * additional_data = seq_num + TLSCompressed.type +
1818 * TLSCompressed.version + TLSCompressed.length;
1819 *
Hanno Becker99abf512019-05-20 14:50:53 +01001820 * For the CID extension, this is extended as follows
1821 * (quoting draft-ietf-tls-dtls-connection-id-05,
1822 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001823 *
1824 * additional_data = seq_num + DTLSPlaintext.type +
1825 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001826 * cid +
1827 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001828 * length_of_DTLSInnerPlaintext;
1829 */
1830
Hanno Becker3307b532017-12-27 21:37:21 +00001831 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1832 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001833 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001834
Hanno Beckera5a2b082019-05-15 14:03:01 +01001835#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001836 if( rec->cid_len != 0 )
1837 {
1838 memcpy( add_data + 11, rec->cid, rec->cid_len );
1839 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1840 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1841 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1842 *add_data_len = 13 + 1 + rec->cid_len;
1843 }
1844 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001845#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001846 {
1847 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1848 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1849 *add_data_len = 13;
1850 }
Hanno Becker3307b532017-12-27 21:37:21 +00001851}
1852
Hanno Becker611a83b2018-01-03 14:27:32 +00001853int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1854 mbedtls_ssl_transform *transform,
1855 mbedtls_record *rec,
1856 int (*f_rng)(void *, unsigned char *, size_t),
1857 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001860 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001861 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001862 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001863 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001864 size_t post_avail;
1865
1866 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001867#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001868 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001869 ((void) ssl);
1870#endif
1871
1872 /* The PRNG is used for dynamic IV generation that's used
1873 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1874#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1875 ( defined(MBEDTLS_AES_C) || \
1876 defined(MBEDTLS_ARIA_C) || \
1877 defined(MBEDTLS_CAMELLIA_C) ) && \
1878 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1879 ((void) f_rng);
1880 ((void) p_rng);
1881#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Hanno Becker3307b532017-12-27 21:37:21 +00001885 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001886 {
Hanno Becker3307b532017-12-27 21:37:21 +00001887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1888 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1889 }
Hanno Becker505089d2019-05-01 09:45:57 +01001890 if( rec == NULL
1891 || rec->buf == NULL
1892 || rec->buf_len < rec->data_offset
1893 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001894#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001895 || rec->cid_len != 0
1896#endif
1897 )
Hanno Becker3307b532017-12-27 21:37:21 +00001898 {
1899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001901 }
1902
Hanno Becker3307b532017-12-27 21:37:21 +00001903 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001904 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001906 data, rec->data_len );
1907
1908 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1909
1910 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1911 {
1912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1913 (unsigned) rec->data_len,
1914 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1916 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001917
Hanno Beckera5a2b082019-05-15 14:03:01 +01001918#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001919 /*
1920 * Add CID information
1921 */
1922 rec->cid_len = transform->out_cid_len;
1923 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1924 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001925
1926 if( rec->cid_len != 0 )
1927 {
1928 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001929 * Wrap plaintext into DTLSInnerPlaintext structure.
1930 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001931 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001932 * Note that this changes `rec->data_len`, and hence
1933 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001934 */
1935 if( ssl_cid_build_inner_plaintext( data,
1936 &rec->data_len,
1937 post_avail,
1938 rec->type ) != 0 )
1939 {
1940 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1941 }
1942
1943 rec->type = MBEDTLS_SSL_MSG_CID;
1944 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001945#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001946
Hanno Becker92c930f2019-04-29 17:31:37 +01001947 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1948
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001950 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001952#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 if( mode == MBEDTLS_MODE_STREAM ||
1954 ( mode == MBEDTLS_MODE_CBC
1955#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001956 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001957#endif
1958 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 {
Hanno Becker3307b532017-12-27 21:37:21 +00001960 if( post_avail < transform->maclen )
1961 {
1962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1963 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1964 }
1965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001967 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001968 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001969 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001970 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1971 data, rec->data_len, rec->ctr, rec->type, mac );
1972 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 }
1974 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1977 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001978 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001979 {
Hanno Becker992b6872017-11-09 18:57:39 +00001980 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1981
Hanno Beckere83efe62019-04-29 13:52:53 +01001982 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001983
Hanno Becker3307b532017-12-27 21:37:21 +00001984 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001985 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001986 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1987 data, rec->data_len );
1988 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1989 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1990
1991 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001992 }
1993 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001994#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1997 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001998 }
1999
Hanno Becker3307b532017-12-27 21:37:21 +00002000 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2001 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002002
Hanno Becker3307b532017-12-27 21:37:21 +00002003 rec->data_len += transform->maclen;
2004 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002005 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002006 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002007#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002009 /*
2010 * Encrypt
2011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2013 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002015 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002016 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002018 "including %d bytes of padding",
2019 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
Hanno Becker3307b532017-12-27 21:37:21 +00002021 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2022 transform->iv_enc, transform->ivlen,
2023 data, rec->data_len,
2024 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002027 return( ret );
2028 }
2029
Hanno Becker3307b532017-12-27 21:37:21 +00002030 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2033 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002034 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 }
Paul Bakker68884e32013-01-07 18:20:04 +01002036 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002038
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002039#if defined(MBEDTLS_GCM_C) || \
2040 defined(MBEDTLS_CCM_C) || \
2041 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002043 mode == MBEDTLS_MODE_CCM ||
2044 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002045 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002046 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002047 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002048 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002049
Hanno Becker3307b532017-12-27 21:37:21 +00002050 /* Check that there's space for both the authentication tag
2051 * and the explicit IV before and after the record content. */
2052 if( post_avail < transform->taglen ||
2053 rec->data_offset < explicit_iv_len )
2054 {
2055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2056 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2057 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002058
Paul Bakker68884e32013-01-07 18:20:04 +01002059 /*
2060 * Generate IV
2061 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002062 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2063 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002064 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002065 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002066 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2067 explicit_iv_len );
2068 /* Prefix record content with explicit IV. */
2069 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002070 }
2071 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2072 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002073 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002074 unsigned char i;
2075
2076 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2077
2078 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002079 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002080 }
2081 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002082 {
2083 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002086 }
2087
Hanno Beckere83efe62019-04-29 13:52:53 +01002088 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002089
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2091 iv, transform->ivlen );
2092 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002093 data - explicit_iv_len, explicit_iv_len );
2094 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002095 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002097 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002098 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002099
Paul Bakker68884e32013-01-07 18:20:04 +01002100 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002101 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002102 */
Hanno Becker3307b532017-12-27 21:37:21 +00002103
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002104 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002105 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002106 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002107 data, rec->data_len, /* source */
2108 data, &rec->data_len, /* destination */
2109 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002112 return( ret );
2113 }
2114
Hanno Becker3307b532017-12-27 21:37:21 +00002115 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2116 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002117
Hanno Becker3307b532017-12-27 21:37:21 +00002118 rec->data_len += transform->taglen + explicit_iv_len;
2119 rec->data_offset -= explicit_iv_len;
2120 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002121 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2125#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002126 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002129 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002130 size_t padlen, i;
2131 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002132
Hanno Becker3307b532017-12-27 21:37:21 +00002133 /* Currently we're always using minimal padding
2134 * (up to 255 bytes would be allowed). */
2135 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2136 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 padlen = 0;
2138
Hanno Becker3307b532017-12-27 21:37:21 +00002139 /* Check there's enough space in the buffer for the padding. */
2140 if( post_avail < padlen + 1 )
2141 {
2142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2143 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2144 }
2145
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002147 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Hanno Becker3307b532017-12-27 21:37:21 +00002149 rec->data_len += padlen + 1;
2150 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002153 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002154 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2155 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002156 */
Hanno Becker3307b532017-12-27 21:37:21 +00002157 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158 {
Hanno Becker3307b532017-12-27 21:37:21 +00002159 if( f_rng == NULL )
2160 {
2161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2163 }
2164
2165 if( rec->data_offset < transform->ivlen )
2166 {
2167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2168 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2169 }
2170
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002171 /*
2172 * Generate IV
2173 */
Hanno Becker3307b532017-12-27 21:37:21 +00002174 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002175 if( ret != 0 )
2176 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002177
Hanno Becker3307b532017-12-27 21:37:21 +00002178 memcpy( data - transform->ivlen, transform->iv_enc,
2179 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002180
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002181 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002185 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002186 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002187 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
Hanno Becker3307b532017-12-27 21:37:21 +00002189 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2190 transform->iv_enc,
2191 transform->ivlen,
2192 data, rec->data_len,
2193 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002196 return( ret );
2197 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002198
Hanno Becker3307b532017-12-27 21:37:21 +00002199 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002203 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002206 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002207 {
2208 /*
2209 * Save IV in SSL3 and TLS1
2210 */
Hanno Becker3307b532017-12-27 21:37:21 +00002211 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2212 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 }
Hanno Becker3307b532017-12-27 21:37:21 +00002214 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002215#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002216 {
2217 data -= transform->ivlen;
2218 rec->data_offset -= transform->ivlen;
2219 rec->data_len += transform->ivlen;
2220 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002223 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002224 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002225 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2226
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002227 /*
2228 * MAC(MAC_write_key, seq_num +
2229 * TLSCipherText.type +
2230 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002231 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002232 * IV + // except for TLS 1.0
2233 * ENC(content + padding + padding_length));
2234 */
Hanno Becker3307b532017-12-27 21:37:21 +00002235
2236 if( post_avail < transform->maclen)
2237 {
2238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2239 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2240 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002241
Hanno Beckere83efe62019-04-29 13:52:53 +01002242 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002245 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002246 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002247
Hanno Becker3307b532017-12-27 21:37:21 +00002248 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002249 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002250 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2251 data, rec->data_len );
2252 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2253 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002254
Hanno Becker3307b532017-12-27 21:37:21 +00002255 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002256
Hanno Becker3307b532017-12-27 21:37:21 +00002257 rec->data_len += transform->maclen;
2258 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002259 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002260 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002265 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002269 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002271 /* Make extra sure authentication was performed, exactly once */
2272 if( auth_done != 1 )
2273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2275 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002276 }
2277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
2280 return( 0 );
2281}
2282
Hanno Becker611a83b2018-01-03 14:27:32 +00002283int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2284 mbedtls_ssl_transform *transform,
2285 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002286{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002287 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002289 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002290#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002291 size_t padlen = 0, correct = 1;
2292#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002293 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002294 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002295 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002296
Hanno Becker611a83b2018-01-03 14:27:32 +00002297#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002298 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002299 ((void) ssl);
2300#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002303 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002304 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2307 }
2308 if( rec == NULL ||
2309 rec->buf == NULL ||
2310 rec->buf_len < rec->data_offset ||
2311 rec->buf_len - rec->data_offset < rec->data_len )
2312 {
2313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002315 }
2316
Hanno Becker4c6876b2017-12-27 21:28:58 +00002317 data = rec->buf + rec->data_offset;
2318 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
Hanno Beckera5a2b082019-05-15 14:03:01 +01002320#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002321 /*
2322 * Match record's CID with incoming CID.
2323 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002324 if( rec->cid_len != transform->in_cid_len ||
2325 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2326 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002327 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002328 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002329#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2332 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002333 {
2334 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002335 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2336 transform->iv_dec,
2337 transform->ivlen,
2338 data, rec->data_len,
2339 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002342 return( ret );
2343 }
2344
Hanno Becker4c6876b2017-12-27 21:28:58 +00002345 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 }
Paul Bakker68884e32013-01-07 18:20:04 +01002351 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353#if defined(MBEDTLS_GCM_C) || \
2354 defined(MBEDTLS_CCM_C) || \
2355 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 mode == MBEDTLS_MODE_CCM ||
2358 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002359 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002360 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002361 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002362
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 /*
2364 * Compute and update sizes
2365 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002366 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002369 "+ taglen (%d)", rec->data_len,
2370 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002372 }
Paul Bakker68884e32013-01-07 18:20:04 +01002373
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002374 /*
2375 * Prepare IV
2376 */
2377 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2378 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002379 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002380 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002381 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002382
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002383 }
2384 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2385 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002386 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002387 unsigned char i;
2388
2389 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2390
2391 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002392 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002393 }
2394 else
2395 {
2396 /* Reminder if we ever add an AEAD mode with a different size */
2397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2399 }
2400
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401 data += explicit_iv_len;
2402 rec->data_offset += explicit_iv_len;
2403 rec->data_len -= explicit_iv_len + transform->taglen;
2404
Hanno Beckere83efe62019-04-29 13:52:53 +01002405 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002406 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002407 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002408
2409 memcpy( transform->iv_dec + transform->fixed_ivlen,
2410 data - explicit_iv_len, explicit_iv_len );
2411
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002412 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002413 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002414 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002415
Paul Bakker68884e32013-01-07 18:20:04 +01002416
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002417 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002418 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002419 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002420 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2421 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002422 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002423 data, rec->data_len,
2424 data, &olen,
2425 data + rec->data_len,
2426 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2431 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002432
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002433 return( ret );
2434 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002435 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002436
Hanno Becker4c6876b2017-12-27 21:28:58 +00002437 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2440 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002441 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2445#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002446 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002449 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002450
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 /*
Paul Bakker45829992013-01-03 14:52:21 +01002452 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002455 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2456 {
2457 /* The ciphertext is prefixed with the CBC IV. */
2458 minlen += transform->ivlen;
2459 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002460#endif
Paul Bakker45829992013-01-03 14:52:21 +01002461
Hanno Becker4c6876b2017-12-27 21:28:58 +00002462 /* Size considerations:
2463 *
2464 * - The CBC cipher text must not be empty and hence
2465 * at least of size transform->ivlen.
2466 *
2467 * Together with the potential IV-prefix, this explains
2468 * the first of the two checks below.
2469 *
2470 * - The record must contain a MAC, either in plain or
2471 * encrypted, depending on whether Encrypt-then-MAC
2472 * is used or not.
2473 * - If it is, the message contains the IV-prefix,
2474 * the CBC ciphertext, and the MAC.
2475 * - If it is not, the padded plaintext, and hence
2476 * the CBC ciphertext, has at least length maclen + 1
2477 * because there is at least the padding length byte.
2478 *
2479 * As the CBC ciphertext is not empty, both cases give the
2480 * lower bound minlen + maclen + 1 on the record size, which
2481 * we test for in the second check below.
2482 */
2483 if( rec->data_len < minlen + transform->ivlen ||
2484 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002487 "+ 1 ) ( + expl IV )", rec->data_len,
2488 transform->ivlen,
2489 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002491 }
2492
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493 /*
2494 * Authenticate before decrypt if enabled
2495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002497 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498 {
Hanno Becker992b6872017-11-09 18:57:39 +00002499 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002502
Hanno Becker4c6876b2017-12-27 21:28:58 +00002503 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2504 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505
Hanno Beckere83efe62019-04-29 13:52:53 +01002506 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002507
Hanno Beckere83efe62019-04-29 13:52:53 +01002508 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2509 add_data_len );
2510 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2511 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002512 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2513 data, rec->data_len );
2514 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2515 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002516
Hanno Becker4c6876b2017-12-27 21:28:58 +00002517 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2518 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002519 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002520 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002521
Hanno Becker4c6876b2017-12-27 21:28:58 +00002522 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2523 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002527 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002528 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002531
2532 /*
2533 * Check length sanity
2534 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002535 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002538 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002540 }
2541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002543 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002544 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002546 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002547 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002548 /* This is safe because data_len >= minlen + maclen + 1 initially,
2549 * and at this point we have at most subtracted maclen (note that
2550 * minlen == transform->ivlen here). */
2551 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002552
Hanno Becker4c6876b2017-12-27 21:28:58 +00002553 data += transform->ivlen;
2554 rec->data_offset += transform->ivlen;
2555 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002556 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002558
Hanno Becker4c6876b2017-12-27 21:28:58 +00002559 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2560 transform->iv_dec, transform->ivlen,
2561 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002564 return( ret );
2565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002566
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002571 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002574 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002575 {
2576 /*
2577 * Save IV in SSL3 and TLS1
2578 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002579 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2580 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002582#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Hanno Becker4c6876b2017-12-27 21:28:58 +00002584 /* Safe since data_len >= minlen + maclen + 1, so after having
2585 * subtracted at most minlen and maclen up to this point,
2586 * data_len > 0. */
2587 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002588
Hanno Becker4c6876b2017-12-27 21:28:58 +00002589 if( auth_done == 1 )
2590 {
2591 correct *= ( rec->data_len >= padlen + 1 );
2592 padlen *= ( rec->data_len >= padlen + 1 );
2593 }
2594 else
Paul Bakker45829992013-01-03 14:52:21 +01002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002597 if( rec->data_len < transform->maclen + padlen + 1 )
2598 {
2599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2600 rec->data_len,
2601 transform->maclen,
2602 padlen + 1 ) );
2603 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002604#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002605
2606 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2607 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Hanno Becker4c6876b2017-12-27 21:28:58 +00002610 padlen++;
2611
2612 /* Regardless of the validity of the padding,
2613 * we have data_len >= padlen here. */
2614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002616 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002618 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#if defined(MBEDTLS_SSL_DEBUG_ALL)
2621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002622 "should be no more than %d",
2623 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002624#endif
Paul Bakker45829992013-01-03 14:52:21 +01002625 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 }
2627 }
2628 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2630#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2631 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002632 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002634 /* The padding check involves a series of up to 256
2635 * consecutive memory reads at the end of the record
2636 * plaintext buffer. In order to hide the length and
2637 * validity of the padding, always perform exactly
2638 * `min(256,plaintext_len)` reads (but take into account
2639 * only the last `padlen` bytes for the padding check). */
2640 size_t pad_count = 0;
2641 size_t real_count = 0;
2642 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002643
Hanno Becker4c6876b2017-12-27 21:28:58 +00002644 /* Index of first padding byte; it has been ensured above
2645 * that the subtraction is safe. */
2646 size_t const padding_idx = rec->data_len - padlen;
2647 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2648 size_t const start_idx = rec->data_len - num_checks;
2649 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002650
Hanno Becker4c6876b2017-12-27 21:28:58 +00002651 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002652 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002653 real_count |= ( idx >= padding_idx );
2654 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002655 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002656 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002659 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002661#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002662 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2666 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002670 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002671
Hanno Becker4c6876b2017-12-27 21:28:58 +00002672 /* If the padding was found to be invalid, padlen == 0
2673 * and the subtraction is safe. If the padding was found valid,
2674 * padlen hasn't been changed and the previous assertion
2675 * data_len >= padlen still holds. */
2676 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002678 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002680 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002686#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002688 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002689#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
2691 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002692 * Authenticate if not done yet.
2693 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002695#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002696 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002697 {
Hanno Becker992b6872017-11-09 18:57:39 +00002698 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002699
Hanno Becker4c6876b2017-12-27 21:28:58 +00002700 /* If the initial value of padlen was such that
2701 * data_len < maclen + padlen + 1, then padlen
2702 * got reset to 1, and the initial check
2703 * data_len >= minlen + maclen + 1
2704 * guarantees that at this point we still
2705 * have at least data_len >= maclen.
2706 *
2707 * If the initial value of padlen was such that
2708 * data_len >= maclen + padlen + 1, then we have
2709 * subtracted either padlen + 1 (if the padding was correct)
2710 * or 0 (if the padding was incorrect) since then,
2711 * hence data_len >= maclen in any case.
2712 */
2713 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Hanno Beckere83efe62019-04-29 13:52:53 +01002715 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002718 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002719 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002720 ssl_mac( &transform->md_ctx_dec,
2721 transform->mac_dec,
2722 data, rec->data_len,
2723 rec->ctr, rec->type,
2724 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002725 }
2726 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2728#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2729 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002730 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002731 {
2732 /*
2733 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002734 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002735 *
2736 * Known timing attacks:
2737 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2738 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002739 * To compensate for different timings for the MAC calculation
2740 * depending on how much padding was removed (which is determined
2741 * by padlen), process extra_run more blocks through the hash
2742 * function.
2743 *
2744 * The formula in the paper is
2745 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2746 * where L1 is the size of the header plus the decrypted message
2747 * plus CBC padding and L2 is the size of the header plus the
2748 * decrypted message. This is for an underlying hash function
2749 * with 64-byte blocks.
2750 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2751 * correctly. We round down instead of up, so -56 is the correct
2752 * value for our calculations instead of -55.
2753 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002754 * Repeat the formula rather than defining a block_size variable.
2755 * This avoids requiring division by a variable at runtime
2756 * (which would be marginally less efficient and would require
2757 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002758 */
2759 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002760 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002761
2762 /*
2763 * The next two sizes are the minimum and maximum values of
2764 * in_msglen over all padlen values.
2765 *
2766 * They're independent of padlen, since we previously did
2767 * in_msglen -= padlen.
2768 *
2769 * Note that max_len + maclen is never more than the buffer
2770 * length, as we previously did in_msglen -= maclen too.
2771 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002772 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002773 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2774
Hanno Becker4c6876b2017-12-27 21:28:58 +00002775 memset( tmp, 0, sizeof( tmp ) );
2776
2777 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002778 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002779#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2780 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002781 case MBEDTLS_MD_MD5:
2782 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002783 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002784 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002785 extra_run =
2786 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2787 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002788 break;
2789#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002790#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002791 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002792 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002793 extra_run =
2794 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2795 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002796 break;
2797#endif
2798 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2801 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002802
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002803 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002804
Hanno Beckere83efe62019-04-29 13:52:53 +01002805 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2806 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002807 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2808 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002809 /* Make sure we access everything even when padlen > 0. This
2810 * makes the synchronisation requirements for just-in-time
2811 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002812 ssl_read_memory( data + rec->data_len, padlen );
2813 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002814
2815 /* Call mbedtls_md_process at least once due to cache attacks
2816 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002817 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002818 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002819
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002821
2822 /* Make sure we access all the memory that could contain the MAC,
2823 * before we check it in the next code block. This makes the
2824 * synchronisation requirements for just-in-time Prime+Probe
2825 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002826 ssl_read_memory( data + min_len,
2827 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002828 }
2829 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2831 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002837#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002838 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2839 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002840#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Hanno Becker4c6876b2017-12-27 21:28:58 +00002842 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2843 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845#if defined(MBEDTLS_SSL_DEBUG_ALL)
2846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002847#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002848 correct = 0;
2849 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002850 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002851 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002852
2853 /*
2854 * Finally check the correct flag
2855 */
2856 if( correct == 0 )
2857 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002858#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002859
2860 /* Make extra sure authentication was performed, exactly once */
2861 if( auth_done != 1 )
2862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2864 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002865 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Hanno Beckera5a2b082019-05-15 14:03:01 +01002867#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002868 if( rec->cid_len != 0 )
2869 {
2870 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2871 &rec->type );
2872 if( ret != 0 )
2873 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2874 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002875#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
2879 return( 0 );
2880}
2881
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002882#undef MAC_NONE
2883#undef MAC_PLAINTEXT
2884#undef MAC_CIPHERTEXT
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002887/*
2888 * Compression/decompression functions
2889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002891{
2892 int ret;
2893 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002894 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002895 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002896 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002900 if( len_pre == 0 )
2901 return( 0 );
2902
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903 memcpy( msg_pre, ssl->out_msg, len_pre );
2904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002906 ssl->out_msglen ) );
2907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002909 ssl->out_msg, ssl->out_msglen );
2910
Paul Bakker48916f92012-09-16 19:57:18 +00002911 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2912 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2913 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002914 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915
Paul Bakker48916f92012-09-16 19:57:18 +00002916 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917 if( ret != Z_OK )
2918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2920 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002921 }
2922
Angus Grattond8213d02016-05-25 20:56:48 +10002923 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002924 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927 ssl->out_msglen ) );
2928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002930 ssl->out_msg, ssl->out_msglen );
2931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002933
2934 return( 0 );
2935}
2936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938{
2939 int ret;
2940 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002941 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002942 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002943 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002947 if( len_pre == 0 )
2948 return( 0 );
2949
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950 memcpy( msg_pre, ssl->in_msg, len_pre );
2951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953 ssl->in_msglen ) );
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956 ssl->in_msg, ssl->in_msglen );
2957
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2959 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2960 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002961 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002962 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963
Paul Bakker48916f92012-09-16 19:57:18 +00002964 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002965 if( ret != Z_OK )
2966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2968 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002969 }
2970
Angus Grattond8213d02016-05-25 20:56:48 +10002971 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002972 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975 ssl->in_msglen ) );
2976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002978 ssl->in_msg, ssl->in_msglen );
2979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002981
2982 return( 0 );
2983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2987static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#if defined(MBEDTLS_SSL_PROTO_DTLS)
2990static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002991{
2992 /* If renegotiation is not enforced, retransmit until we would reach max
2993 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002994 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002995 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002996 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002997 unsigned char doublings = 1;
2998
2999 while( ratio != 0 )
3000 {
3001 ++doublings;
3002 ratio >>= 1;
3003 }
3004
3005 if( ++ssl->renego_records_seen > doublings )
3006 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003008 return( 0 );
3009 }
3010 }
3011
3012 return( ssl_write_hello_request( ssl ) );
3013}
3014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003016
Paul Bakker5121ce52009-01-03 21:22:43 +00003017/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003018 * Fill the input message buffer by appending data to it.
3019 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003020 *
3021 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3022 * available (from this read and/or a previous one). Otherwise, an error code
3023 * is returned (possibly EOF or WANT_READ).
3024 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003025 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3026 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3027 * since we always read a whole datagram at once.
3028 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003029 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003030 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003033{
Paul Bakker23986e52011-04-24 08:57:21 +00003034 int ret;
3035 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003039 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003042 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003044 }
3045
Angus Grattond8213d02016-05-25 20:56:48 +10003046 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003050 }
3051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003053 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003055 uint32_t timeout;
3056
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003057 /* Just to be sure */
3058 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3059 {
3060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3061 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3063 }
3064
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003065 /*
3066 * The point is, we need to always read a full datagram at once, so we
3067 * sometimes read more then requested, and handle the additional data.
3068 * It could be the rest of the current record (while fetching the
3069 * header) and/or some other records in the same datagram.
3070 */
3071
3072 /*
3073 * Move to the next record in the already read datagram if applicable
3074 */
3075 if( ssl->next_record_offset != 0 )
3076 {
3077 if( ssl->in_left < ssl->next_record_offset )
3078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3080 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003081 }
3082
3083 ssl->in_left -= ssl->next_record_offset;
3084
3085 if( ssl->in_left != 0 )
3086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003088 ssl->next_record_offset ) );
3089 memmove( ssl->in_hdr,
3090 ssl->in_hdr + ssl->next_record_offset,
3091 ssl->in_left );
3092 }
3093
3094 ssl->next_record_offset = 0;
3095 }
3096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003099
3100 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003101 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003102 */
3103 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003106 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003107 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003108
3109 /*
3110 * A record can't be split accross datagrams. If we need to read but
3111 * are not at the beginning of a new record, the caller did something
3112 * wrong.
3113 */
3114 if( ssl->in_left != 0 )
3115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003118 }
3119
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003120 /*
3121 * Don't even try to read if time's out already.
3122 * This avoids by-passing the timer when repeatedly receiving messages
3123 * that will end up being dropped.
3124 */
3125 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003126 {
3127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003128 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003129 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003130 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003131 {
Angus Grattond8213d02016-05-25 20:56:48 +10003132 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003135 timeout = ssl->handshake->retransmit_timeout;
3136 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003137 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003140
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003141 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003142 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3143 timeout );
3144 else
3145 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003148
3149 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003151 }
3152
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003153 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003156 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003159 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003160 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003163 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003164 }
3165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003169 return( ret );
3170 }
3171
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003172 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003173 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003175 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003177 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003178 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003181 return( ret );
3182 }
3183
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003184 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003185 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003187 }
3188
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 if( ret < 0 )
3190 return( ret );
3191
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003192 ssl->in_left = ret;
3193 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003194 MBEDTLS_SSL_TRANSPORT_ELSE
3195#endif /* MBEDTLS_SSL_PROTO_DTLS */
3196#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003199 ssl->in_left, nb_want ) );
3200
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003201 while( ssl->in_left < nb_want )
3202 {
3203 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003204
3205 if( ssl_check_timer( ssl ) != 0 )
3206 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3207 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003208 {
3209 if( ssl->f_recv_timeout != NULL )
3210 {
3211 ret = ssl->f_recv_timeout( ssl->p_bio,
3212 ssl->in_hdr + ssl->in_left, len,
3213 ssl->conf->read_timeout );
3214 }
3215 else
3216 {
3217 ret = ssl->f_recv( ssl->p_bio,
3218 ssl->in_hdr + ssl->in_left, len );
3219 }
3220 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003223 ssl->in_left, nb_want ) );
3224 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003225
3226 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003228
3229 if( ret < 0 )
3230 return( ret );
3231
mohammad160352aecb92018-03-28 23:41:40 -07003232 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003233 {
Darryl Green11999bb2018-03-13 15:22:58 +00003234 MBEDTLS_SSL_DEBUG_MSG( 1,
3235 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003236 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3238 }
3239
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003240 ssl->in_left += ret;
3241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003243#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
3247 return( 0 );
3248}
3249
3250/*
3251 * Flush any data not yet written
3252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003254{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003255 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003256 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003260 if( ssl->f_send == NULL )
3261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003263 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003265 }
3266
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003267 /* Avoid incrementing counter if data is flushed */
3268 if( ssl->out_left == 0 )
3269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003271 return( 0 );
3272 }
3273
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 while( ssl->out_left > 0 )
3275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003277 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Hanno Becker2b1e3542018-08-06 11:19:13 +01003279 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003280 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
3284 if( ret <= 0 )
3285 return( ret );
3286
mohammad160352aecb92018-03-28 23:41:40 -07003287 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003288 {
Darryl Green11999bb2018-03-13 15:22:58 +00003289 MBEDTLS_SSL_DEBUG_MSG( 1,
3290 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003291 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3293 }
3294
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 ssl->out_left -= ret;
3296 }
3297
Hanno Becker2b1e3542018-08-06 11:19:13 +01003298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003299 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003300 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003301 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003302 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003303 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003304#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003305#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003306 {
3307 ssl->out_hdr = ssl->out_buf + 8;
3308 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003309#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003310 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003313
3314 return( 0 );
3315}
3316
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003317/*
3318 * Functions to handle the DTLS retransmission state machine
3319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321/*
3322 * Append current handshake message to current outgoing flight
3323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3328 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3329 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003330
3331 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003332 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003336 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003337 }
3338
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003339 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003340 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003343 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003344 }
3345
3346 /* Copy current handshake message with headers */
3347 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3348 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003349 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003350 msg->next = NULL;
3351
3352 /* Append to the current flight */
3353 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003354 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003355 else
3356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003358 while( cur->next != NULL )
3359 cur = cur->next;
3360 cur->next = msg;
3361 }
3362
Hanno Becker3b235902018-08-06 09:54:53 +01003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364 return( 0 );
3365}
3366
3367/*
3368 * Free the current flight of handshake messages
3369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_ssl_flight_item *cur = flight;
3373 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003374
3375 while( cur != NULL )
3376 {
3377 next = cur->next;
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 mbedtls_free( cur->p );
3380 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003381
3382 cur = next;
3383 }
3384}
3385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3387static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003388#endif
3389
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003390/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003391 * Swap transform_out and out_ctr with the alternative ones
3392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003396 unsigned char tmp_out_ctr[8];
3397
3398 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003401 return;
3402 }
3403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003405
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003406 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407 tmp_transform = ssl->transform_out;
3408 ssl->transform_out = ssl->handshake->alt_transform_out;
3409 ssl->handshake->alt_transform_out = tmp_transform;
3410
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003411 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003412 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3413 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003414 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003415
3416 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003417 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3420 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3425 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003426 }
3427 }
3428#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003429}
3430
3431/*
3432 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003433 */
3434int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3435{
3436 int ret = 0;
3437
3438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3439
3440 ret = mbedtls_ssl_flight_transmit( ssl );
3441
3442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3443
3444 return( ret );
3445}
3446
3447/*
3448 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003449 *
3450 * Need to remember the current message in case flush_output returns
3451 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003452 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003453 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003454int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003456 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003460 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003462
3463 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003464 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003465 ssl_swap_epochs( ssl );
3466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003468 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003469
3470 while( ssl->handshake->cur_msg != NULL )
3471 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003472 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003473 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003474
Hanno Beckere1dcb032018-08-17 16:47:58 +01003475 int const is_finished =
3476 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3477 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3478
Hanno Becker04da1892018-08-14 13:22:10 +01003479 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3480 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3481
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003482 /* Swap epochs before sending Finished: we can't do it after
3483 * sending ChangeCipherSpec, in case write returns WANT_READ.
3484 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003485 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003486 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003488 ssl_swap_epochs( ssl );
3489 }
3490
Hanno Becker67bc7c32018-08-06 11:33:50 +01003491 ret = ssl_get_remaining_payload_in_datagram( ssl );
3492 if( ret < 0 )
3493 return( ret );
3494 max_frag_len = (size_t) ret;
3495
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003496 /* CCS is copied as is, while HS messages may need fragmentation */
3497 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3498 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003499 if( max_frag_len == 0 )
3500 {
3501 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3502 return( ret );
3503
3504 continue;
3505 }
3506
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003507 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003508 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003509 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003510
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003511 /* Update position inside current message */
3512 ssl->handshake->cur_msg_p += cur->len;
3513 }
3514 else
3515 {
3516 const unsigned char * const p = ssl->handshake->cur_msg_p;
3517 const size_t hs_len = cur->len - 12;
3518 const size_t frag_off = p - ( cur->p + 12 );
3519 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003520 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003521
Hanno Beckere1dcb032018-08-17 16:47:58 +01003522 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003523 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003524 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003525 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003526
Hanno Becker67bc7c32018-08-06 11:33:50 +01003527 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3528 return( ret );
3529
3530 continue;
3531 }
3532 max_hs_frag_len = max_frag_len - 12;
3533
3534 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3535 max_hs_frag_len : rem_len;
3536
3537 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003538 {
3539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003540 (unsigned) cur_hs_frag_len,
3541 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003542 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003543
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003544 /* Messages are stored with handshake headers as if not fragmented,
3545 * copy beginning of headers then fill fragmentation fields.
3546 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3547 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003548
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003549 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3550 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3551 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3552
Hanno Becker67bc7c32018-08-06 11:33:50 +01003553 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3554 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3555 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003556
3557 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3558
Hanno Becker3f7b9732018-08-28 09:53:25 +01003559 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003560 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3561 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003562 ssl->out_msgtype = cur->type;
3563
3564 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003565 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003566 }
3567
3568 /* If done with the current message move to the next one if any */
3569 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3570 {
3571 if( cur->next != NULL )
3572 {
3573 ssl->handshake->cur_msg = cur->next;
3574 ssl->handshake->cur_msg_p = cur->next->p + 12;
3575 }
3576 else
3577 {
3578 ssl->handshake->cur_msg = NULL;
3579 ssl->handshake->cur_msg_p = NULL;
3580 }
3581 }
3582
3583 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003584 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 return( ret );
3588 }
3589 }
3590
Hanno Becker67bc7c32018-08-06 11:33:50 +01003591 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3592 return( ret );
3593
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003594 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3596 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003597 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003600 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3601 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003602
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604
3605 return( 0 );
3606}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003607
3608/*
3609 * To be called when the last message of an incoming flight is received.
3610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003612{
3613 /* We won't need to resend that one any more */
3614 ssl_flight_free( ssl->handshake->flight );
3615 ssl->handshake->flight = NULL;
3616 ssl->handshake->cur_msg = NULL;
3617
3618 /* The next incoming flight will start with this msg_seq */
3619 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3620
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003621 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003622 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003623
Hanno Becker0271f962018-08-16 13:23:47 +01003624 /* Clear future message buffering structure. */
3625 ssl_buffering_free( ssl );
3626
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003627 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003628 ssl_set_timer( ssl, 0 );
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3631 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003634 }
3635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003637}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003638
3639/*
3640 * To be called when the last message of an outgoing flight is send.
3641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003643{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003644 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003645 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3648 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003651 }
3652 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003656
Paul Bakker5121ce52009-01-03 21:22:43 +00003657/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003658 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003660
3661/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003662 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003663 *
3664 * - fill in handshake headers
3665 * - update handshake checksum
3666 * - DTLS: save message for resending
3667 * - then pass to the record layer
3668 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003669 * DTLS: except for HelloRequest, messages are only queued, and will only be
3670 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003671 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003672 * Inputs:
3673 * - ssl->out_msglen: 4 + actual handshake message len
3674 * (4 is the size of handshake headers for TLS)
3675 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3676 * - ssl->out_msg + 4: the handshake message body
3677 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003678 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003679 * - ssl->out_msglen: the length of the record contents
3680 * (including handshake headers but excluding record headers)
3681 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003682 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003683int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003684{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003685 int ret;
3686 const size_t hs_len = ssl->out_msglen - 4;
3687 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003688
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3690
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003691 /*
3692 * Sanity checks
3693 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003694 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003695 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3696 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003697 /* In SSLv3, the client might send a NoCertificate alert. */
3698#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3699 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3700 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3701 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3702#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3703 {
3704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3706 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003707 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003708
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003709 /* Whenever we send anything different from a
3710 * HelloRequest we should be in a handshake - double check. */
3711 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3712 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003713 ssl->handshake == NULL )
3714 {
3715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3717 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003720 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003721 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003726 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003727#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003728
Hanno Beckerb50a2532018-08-06 11:52:54 +01003729 /* Double-check that we did not exceed the bounds
3730 * of the outgoing record buffer.
3731 * This should never fail as the various message
3732 * writing functions must obey the bounds of the
3733 * outgoing record buffer, but better be safe.
3734 *
3735 * Note: We deliberately do not check for the MTU or MFL here.
3736 */
3737 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3740 "size %u, maximum %u",
3741 (unsigned) ssl->out_msglen,
3742 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3744 }
3745
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003746 /*
3747 * Fill handshake headers
3748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003750 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003751 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3752 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3753 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003754
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003755 /*
3756 * DTLS has additional fields in the Handshake layer,
3757 * between the length field and the actual payload:
3758 * uint16 message_seq;
3759 * uint24 fragment_offset;
3760 * uint24 fragment_length;
3761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003763 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003764 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003765 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003766 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003767 {
3768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3769 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003770 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003771 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3773 }
3774
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003775 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003776 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003777
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003778 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003779 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003780 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003781 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3782 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3783 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003784 }
3785 else
3786 {
3787 ssl->out_msg[4] = 0;
3788 ssl->out_msg[5] = 0;
3789 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003790
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003791 /* Handshake hashes are computed without fragmentation,
3792 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003793 memset( ssl->out_msg + 6, 0x00, 3 );
3794 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003795 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003797
Hanno Becker0207e532018-08-28 10:28:28 +01003798 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003799 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3800 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 }
3802
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003803 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003805 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003806 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3807 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003808 {
3809 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003812 return( ret );
3813 }
3814 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003815 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003816#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003817 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003818 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003819 {
3820 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3821 return( ret );
3822 }
3823 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003824
3825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3826
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003827 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003828}
3829
3830/*
3831 * Record layer functions
3832 */
3833
3834/*
3835 * Write current record.
3836 *
3837 * Uses:
3838 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3839 * - ssl->out_msglen: length of the record content (excl headers)
3840 * - ssl->out_msg: record content
3841 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003842int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003843{
3844 int ret, done = 0;
3845 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003846 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003847
3848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003851 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003853 {
3854 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003857 return( ret );
3858 }
3859
3860 len = ssl->out_msglen;
3861 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3865 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 ret = mbedtls_ssl_hw_record_write( ssl );
3870 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3873 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003874 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003875
3876 if( ret == 0 )
3877 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003878 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003880 if( !done )
3881 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003882 unsigned i;
3883 size_t protected_record_size;
3884
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003885 /* Skip writing the record content type to after the encryption,
3886 * as it may change when using the CID extension. */
3887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003889 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003890
Hanno Becker19859472018-08-06 09:40:20 +01003891 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003892 ssl->out_len[0] = (unsigned char)( len >> 8 );
3893 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003894
Paul Bakker48916f92012-09-16 19:57:18 +00003895 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003896 {
Hanno Becker3307b532017-12-27 21:37:21 +00003897 mbedtls_record rec;
3898
3899 rec.buf = ssl->out_iv;
3900 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3901 ( ssl->out_iv - ssl->out_buf );
3902 rec.data_len = ssl->out_msglen;
3903 rec.data_offset = ssl->out_msg - rec.buf;
3904
3905 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3906 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3907 ssl->conf->transport, rec.ver );
3908 rec.type = ssl->out_msgtype;
3909
Hanno Beckera5a2b082019-05-15 14:03:01 +01003910#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003911 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003912 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003913#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003914
Hanno Becker611a83b2018-01-03 14:27:32 +00003915 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003916 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003919 return( ret );
3920 }
3921
Hanno Becker3307b532017-12-27 21:37:21 +00003922 if( rec.data_offset != 0 )
3923 {
3924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3926 }
3927
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003928 /* Update the record content type and CID. */
3929 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003930#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003931 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003932#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003933 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003934 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3935 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003936 }
3937
Hanno Becker43395762019-05-03 14:46:38 +01003938 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003939
3940#if defined(MBEDTLS_SSL_PROTO_DTLS)
3941 /* In case of DTLS, double-check that we don't exceed
3942 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003943 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003944 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003945 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003946 if( ret < 0 )
3947 return( ret );
3948
3949 if( protected_record_size > (size_t) ret )
3950 {
3951 /* Should never happen */
3952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3953 }
3954 }
3955#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003956
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003957 /* Now write the potentially updated record content type. */
3958 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003961 "version = [%d:%d], msglen = %d",
3962 ssl->out_hdr[0], ssl->out_hdr[1],
3963 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003966 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003967
3968 ssl->out_left += protected_record_size;
3969 ssl->out_hdr += protected_record_size;
3970 ssl_update_out_pointers( ssl, ssl->transform_out );
3971
Hanno Becker04484622018-08-06 09:49:38 +01003972 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3973 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3974 break;
3975
3976 /* The loop goes to its end iff the counter is wrapping */
3977 if( i == ssl_ep_len( ssl ) )
3978 {
3979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3980 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3981 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 }
3983
Hanno Becker67bc7c32018-08-06 11:33:50 +01003984#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003985 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003986 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003987 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003988 size_t remaining;
3989 ret = ssl_get_remaining_payload_in_datagram( ssl );
3990 if( ret < 0 )
3991 {
3992 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3993 ret );
3994 return( ret );
3995 }
3996
3997 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003998 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003999 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004000 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004001 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004002 else
4003 {
Hanno Becker513815a2018-08-20 11:56:09 +01004004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004005 }
4006 }
4007#endif /* MBEDTLS_SSL_PROTO_DTLS */
4008
4009 if( ( flush == SSL_FORCE_FLUSH ) &&
4010 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004013 return( ret );
4014 }
4015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004017
4018 return( 0 );
4019}
4020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004022
4023static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4024{
4025 if( ssl->in_msglen < ssl->in_hslen ||
4026 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4027 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4028 {
4029 return( 1 );
4030 }
4031 return( 0 );
4032}
Hanno Becker44650b72018-08-16 12:51:11 +01004033
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004034static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004035{
4036 return( ( ssl->in_msg[9] << 16 ) |
4037 ( ssl->in_msg[10] << 8 ) |
4038 ssl->in_msg[11] );
4039}
4040
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004041static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004042{
4043 return( ( ssl->in_msg[6] << 16 ) |
4044 ( ssl->in_msg[7] << 8 ) |
4045 ssl->in_msg[8] );
4046}
4047
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004048static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004049{
4050 uint32_t msg_len, frag_off, frag_len;
4051
4052 msg_len = ssl_get_hs_total_len( ssl );
4053 frag_off = ssl_get_hs_frag_off( ssl );
4054 frag_len = ssl_get_hs_frag_len( ssl );
4055
4056 if( frag_off > msg_len )
4057 return( -1 );
4058
4059 if( frag_len > msg_len - frag_off )
4060 return( -1 );
4061
4062 if( frag_len + 12 > ssl->in_msglen )
4063 return( -1 );
4064
4065 return( 0 );
4066}
4067
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004068/*
4069 * Mark bits in bitmask (used for DTLS HS reassembly)
4070 */
4071static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4072{
4073 unsigned int start_bits, end_bits;
4074
4075 start_bits = 8 - ( offset % 8 );
4076 if( start_bits != 8 )
4077 {
4078 size_t first_byte_idx = offset / 8;
4079
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004080 /* Special case */
4081 if( len <= start_bits )
4082 {
4083 for( ; len != 0; len-- )
4084 mask[first_byte_idx] |= 1 << ( start_bits - len );
4085
4086 /* Avoid potential issues with offset or len becoming invalid */
4087 return;
4088 }
4089
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004090 offset += start_bits; /* Now offset % 8 == 0 */
4091 len -= start_bits;
4092
4093 for( ; start_bits != 0; start_bits-- )
4094 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4095 }
4096
4097 end_bits = len % 8;
4098 if( end_bits != 0 )
4099 {
4100 size_t last_byte_idx = ( offset + len ) / 8;
4101
4102 len -= end_bits; /* Now len % 8 == 0 */
4103
4104 for( ; end_bits != 0; end_bits-- )
4105 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4106 }
4107
4108 memset( mask + offset / 8, 0xFF, len / 8 );
4109}
4110
4111/*
4112 * Check that bitmask is full
4113 */
4114static int ssl_bitmask_check( unsigned char *mask, size_t len )
4115{
4116 size_t i;
4117
4118 for( i = 0; i < len / 8; i++ )
4119 if( mask[i] != 0xFF )
4120 return( -1 );
4121
4122 for( i = 0; i < len % 8; i++ )
4123 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4124 return( -1 );
4125
4126 return( 0 );
4127}
4128
Hanno Becker56e205e2018-08-16 09:06:12 +01004129/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004130static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004131 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004132{
Hanno Becker56e205e2018-08-16 09:06:12 +01004133 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004134
Hanno Becker56e205e2018-08-16 09:06:12 +01004135 alloc_len = 12; /* Handshake header */
4136 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004137
Hanno Beckerd07df862018-08-16 09:14:58 +01004138 if( add_bitmap )
4139 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004140
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004141 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004142}
Hanno Becker56e205e2018-08-16 09:06:12 +01004143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004145
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004146static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004147{
4148 return( ( ssl->in_msg[1] << 16 ) |
4149 ( ssl->in_msg[2] << 8 ) |
4150 ssl->in_msg[3] );
4151}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004152
Simon Butcher99000142016-10-13 17:21:01 +01004153int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004158 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004160 }
4161
Hanno Becker12555c62018-08-16 12:47:53 +01004162 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004165 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004166 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004169 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004170 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004171 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004172 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004173
Hanno Becker44650b72018-08-16 12:51:11 +01004174 if( ssl_check_hs_header( ssl ) != 0 )
4175 {
4176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4177 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4178 }
4179
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004180 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004181 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4182 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4183 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4184 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004185 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004186 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4187 {
4188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4189 recv_msg_seq,
4190 ssl->handshake->in_msg_seq ) );
4191 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4192 }
4193
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004194 /* Retransmit only on last message from previous flight, to avoid
4195 * too many retransmissions.
4196 * Besides, No sane server ever retransmits HelloVerifyRequest */
4197 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004201 "message_seq = %d, start_of_flight = %d",
4202 recv_msg_seq,
4203 ssl->handshake->in_flight_start_seq ) );
4204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004208 return( ret );
4209 }
4210 }
4211 else
4212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004214 "message_seq = %d, expected = %d",
4215 recv_msg_seq,
4216 ssl->handshake->in_msg_seq ) );
4217 }
4218
Hanno Becker90333da2017-10-10 11:27:13 +01004219 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004220 }
4221 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004222
Hanno Becker6d97ef52018-08-16 13:09:04 +01004223 /* Message reassembly is handled alongside buffering of future
4224 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004225 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004226 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004227 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004230 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004231 }
4232 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004233 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004235#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004236 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004237 /* With TLS we don't handle fragmentation (for now) */
4238 if( ssl->in_msglen < ssl->in_hslen )
4239 {
4240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4241 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4242 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004243 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004244#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004245
Simon Butcher99000142016-10-13 17:21:01 +01004246 return( 0 );
4247}
4248
4249void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4250{
Hanno Becker0271f962018-08-16 13:23:47 +01004251 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004252
Hanno Becker0271f962018-08-16 13:23:47 +01004253 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004254 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004255 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004256 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004257
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004258 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004260 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004261 ssl->handshake != NULL )
4262 {
Hanno Becker0271f962018-08-16 13:23:47 +01004263 unsigned offset;
4264 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004265
Hanno Becker0271f962018-08-16 13:23:47 +01004266 /* Increment handshake sequence number */
4267 hs->in_msg_seq++;
4268
4269 /*
4270 * Clear up handshake buffering and reassembly structure.
4271 */
4272
4273 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004274 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004275
4276 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004277 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4278 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004279 offset++, hs_buf++ )
4280 {
4281 *hs_buf = *(hs_buf + 1);
4282 }
4283
4284 /* Create a fresh last entry */
4285 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004286 }
4287#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004288}
4289
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004290/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004291 * DTLS anti-replay: RFC 6347 4.1.2.6
4292 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004293 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4294 * Bit n is set iff record number in_window_top - n has been seen.
4295 *
4296 * Usually, in_window_top is the last record number seen and the lsb of
4297 * in_window is set. The only exception is the initial state (record number 0
4298 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4301static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004302{
4303 ssl->in_window_top = 0;
4304 ssl->in_window = 0;
4305}
4306
4307static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4308{
4309 return( ( (uint64_t) buf[0] << 40 ) |
4310 ( (uint64_t) buf[1] << 32 ) |
4311 ( (uint64_t) buf[2] << 24 ) |
4312 ( (uint64_t) buf[3] << 16 ) |
4313 ( (uint64_t) buf[4] << 8 ) |
4314 ( (uint64_t) buf[5] ) );
4315}
4316
4317/*
4318 * Return 0 if sequence number is acceptable, -1 otherwise
4319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321{
4322 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4323 uint64_t bit;
4324
Hanno Becker7f376f42019-06-12 16:20:48 +01004325 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4326 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4327 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004328 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004329 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004330
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004331 if( rec_seqnum > ssl->in_window_top )
4332 return( 0 );
4333
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004334 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004335
4336 if( bit >= 64 )
4337 return( -1 );
4338
4339 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4340 return( -1 );
4341
4342 return( 0 );
4343}
4344
4345/*
4346 * Update replay window on new validated record
4347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004349{
4350 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4351
Hanno Becker7f376f42019-06-12 16:20:48 +01004352 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4353 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4354 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004355 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004356 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004357
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004358 if( rec_seqnum > ssl->in_window_top )
4359 {
4360 /* Update window_top and the contents of the window */
4361 uint64_t shift = rec_seqnum - ssl->in_window_top;
4362
4363 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004364 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004365 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004366 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004367 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004368 ssl->in_window |= 1;
4369 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004370
4371 ssl->in_window_top = rec_seqnum;
4372 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004373 else
4374 {
4375 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004376 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004377
4378 if( bit < 64 ) /* Always true, but be extra sure */
4379 ssl->in_window |= (uint64_t) 1 << bit;
4380 }
4381}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004383
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004384#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004385/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004386static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4387
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004388/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004389 * Without any SSL context, check if a datagram looks like a ClientHello with
4390 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004391 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004392 *
4393 * - if cookie is valid, return 0
4394 * - if ClientHello looks superficially valid but cookie is not,
4395 * fill obuf and set olen, then
4396 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4397 * - otherwise return a specific error code
4398 */
4399static int ssl_check_dtls_clihlo_cookie(
4400 mbedtls_ssl_cookie_write_t *f_cookie_write,
4401 mbedtls_ssl_cookie_check_t *f_cookie_check,
4402 void *p_cookie,
4403 const unsigned char *cli_id, size_t cli_id_len,
4404 const unsigned char *in, size_t in_len,
4405 unsigned char *obuf, size_t buf_len, size_t *olen )
4406{
4407 size_t sid_len, cookie_len;
4408 unsigned char *p;
4409
4410 if( f_cookie_write == NULL || f_cookie_check == NULL )
4411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4412
4413 /*
4414 * Structure of ClientHello with record and handshake headers,
4415 * and expected values. We don't need to check a lot, more checks will be
4416 * done when actually parsing the ClientHello - skipping those checks
4417 * avoids code duplication and does not make cookie forging any easier.
4418 *
4419 * 0-0 ContentType type; copied, must be handshake
4420 * 1-2 ProtocolVersion version; copied
4421 * 3-4 uint16 epoch; copied, must be 0
4422 * 5-10 uint48 sequence_number; copied
4423 * 11-12 uint16 length; (ignored)
4424 *
4425 * 13-13 HandshakeType msg_type; (ignored)
4426 * 14-16 uint24 length; (ignored)
4427 * 17-18 uint16 message_seq; copied
4428 * 19-21 uint24 fragment_offset; copied, must be 0
4429 * 22-24 uint24 fragment_length; (ignored)
4430 *
4431 * 25-26 ProtocolVersion client_version; (ignored)
4432 * 27-58 Random random; (ignored)
4433 * 59-xx SessionID session_id; 1 byte len + sid_len content
4434 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4435 * ...
4436 *
4437 * Minimum length is 61 bytes.
4438 */
4439 if( in_len < 61 ||
4440 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4441 in[3] != 0 || in[4] != 0 ||
4442 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4443 {
4444 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4445 }
4446
4447 sid_len = in[59];
4448 if( sid_len > in_len - 61 )
4449 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4450
4451 cookie_len = in[60 + sid_len];
4452 if( cookie_len > in_len - 60 )
4453 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4454
4455 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4456 cli_id, cli_id_len ) == 0 )
4457 {
4458 /* Valid cookie */
4459 return( 0 );
4460 }
4461
4462 /*
4463 * If we get here, we've got an invalid cookie, let's prepare HVR.
4464 *
4465 * 0-0 ContentType type; copied
4466 * 1-2 ProtocolVersion version; copied
4467 * 3-4 uint16 epoch; copied
4468 * 5-10 uint48 sequence_number; copied
4469 * 11-12 uint16 length; olen - 13
4470 *
4471 * 13-13 HandshakeType msg_type; hello_verify_request
4472 * 14-16 uint24 length; olen - 25
4473 * 17-18 uint16 message_seq; copied
4474 * 19-21 uint24 fragment_offset; copied
4475 * 22-24 uint24 fragment_length; olen - 25
4476 *
4477 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4478 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4479 *
4480 * Minimum length is 28.
4481 */
4482 if( buf_len < 28 )
4483 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4484
4485 /* Copy most fields and adapt others */
4486 memcpy( obuf, in, 25 );
4487 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4488 obuf[25] = 0xfe;
4489 obuf[26] = 0xff;
4490
4491 /* Generate and write actual cookie */
4492 p = obuf + 28;
4493 if( f_cookie_write( p_cookie,
4494 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4495 {
4496 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4497 }
4498
4499 *olen = p - obuf;
4500
4501 /* Go back and fill length fields */
4502 obuf[27] = (unsigned char)( *olen - 28 );
4503
4504 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4505 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4506 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4507
4508 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4509 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4510
4511 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4512}
4513
4514/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004515 * Handle possible client reconnect with the same UDP quadruplet
4516 * (RFC 6347 Section 4.2.8).
4517 *
4518 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4519 * that looks like a ClientHello.
4520 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004521 * - if the input looks like a ClientHello without cookies,
4522 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004524 * - if the input looks like a ClientHello with a valid cookie,
4525 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004526 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004527 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004528 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004529 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004530 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4531 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004532 */
4533static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4534{
4535 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004536 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004537
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004538 ret = ssl_check_dtls_clihlo_cookie(
4539 ssl->conf->f_cookie_write,
4540 ssl->conf->f_cookie_check,
4541 ssl->conf->p_cookie,
4542 ssl->cli_id, ssl->cli_id_len,
4543 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004544 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004545
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004546 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4547
4548 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004549 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004550 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004551 * If the error is permanent we'll catch it later,
4552 * if it's not, then hopefully it'll work next time. */
4553 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4554
4555 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004556 }
4557
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004558 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004559 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004560 /* Got a valid cookie, partially reset context */
4561 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4562 {
4563 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4564 return( ret );
4565 }
4566
4567 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004568 }
4569
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004570 return( ret );
4571}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004572#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004573
Hanno Becker46483f12019-05-03 13:25:54 +01004574static int ssl_check_record_type( uint8_t record_type )
4575{
4576 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4577 record_type != MBEDTLS_SSL_MSG_ALERT &&
4578 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4579 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4580 {
4581 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4582 }
4583
4584 return( 0 );
4585}
4586
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004587/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004588 * ContentType type;
4589 * ProtocolVersion version;
4590 * uint16 epoch; // DTLS only
4591 * uint48 sequence_number; // DTLS only
4592 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004593 *
4594 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004595 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004596 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4597 *
4598 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004599 * 1. proceed with the record if this function returns 0
4600 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4601 * 3. return CLIENT_RECONNECT if this function return that value
4602 * 4. drop the whole datagram if this function returns anything else.
4603 * Point 2 is needed when the peer is resending, and we have already received
4604 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004607{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004608 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004609 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004610
Hanno Becker8b09b732019-05-08 12:03:28 +01004611 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004612
Paul Bakker5121ce52009-01-03 21:22:43 +00004613 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004614 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004615
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004616 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004617#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004618 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004619 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4620 ssl->conf->cid_len != 0 )
4621 {
4622 /* Shift pointers to account for record header including CID
4623 * struct {
4624 * ContentType special_type = tls12_cid;
4625 * ProtocolVersion version;
4626 * uint16 epoch;
4627 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004628 * opaque cid[cid_length]; // Additional field compared to
4629 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004630 * uint16 length;
4631 * opaque enc_content[DTLSCiphertext.length];
4632 * } DTLSCiphertext;
4633 */
4634
4635 /* So far, we only support static CID lengths
4636 * fixed in the configuration. */
4637 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4638 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4639 }
4640 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004641#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004642 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004645
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004646#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004647 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004648 * Section 4.1.2.7, that is, send alert only with TLS */
4649 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4650 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004651 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4652 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004653 }
4654#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004657 }
4658
4659 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004660 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4663 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004664 }
4665
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004666 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4669 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 }
4671
Hanno Becker8b09b732019-05-08 12:03:28 +01004672 /* Now that the total length of the record header is known, ensure
4673 * that the current datagram is large enough to hold it.
4674 * This would fail, for example, if we received a datagram of
4675 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4676 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4677 if( ret != 0 )
4678 {
4679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4680 return( ret );
4681 }
4682 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4683
4684 /* Parse and validate record length
4685 * This must happen after the CID parsing because
4686 * its position in the record header depends on
4687 * the presence of a CID. */
4688
4689 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004690 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004691 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4694 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004695 }
4696
Hanno Becker8b09b732019-05-08 12:03:28 +01004697 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004698 "version = [%d:%d], msglen = %d",
4699 ssl->in_msgtype,
4700 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004701
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004702 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004703 * DTLS-related tests.
4704 * Check epoch before checking length constraint because
4705 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4706 * message gets duplicated before the corresponding Finished message,
4707 * the second ChangeCipherSpec should be discarded because it belongs
4708 * to an old epoch, but not because its length is shorter than
4709 * the minimum record length for packets using the new record transform.
4710 * Note that these two kinds of failures are handled differently,
4711 * as an unexpected record is silently skipped but an invalid
4712 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004713 */
4714#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004715 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004716 {
4717 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4718
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004719 /* Check epoch (and sequence number) with DTLS */
4720 if( rec_epoch != ssl->in_epoch )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4723 "expected %d, received %d",
4724 ssl->in_epoch, rec_epoch ) );
4725
4726#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4727 /*
4728 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4729 * access the first byte of record content (handshake type), as we
4730 * have an active transform (possibly iv_len != 0), so use the
4731 * fact that the record header len is 13 instead.
4732 */
4733 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4734 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4735 rec_epoch == 0 &&
4736 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4737 ssl->in_left > 13 &&
4738 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4739 {
4740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4741 "from the same port" ) );
4742 return( ssl_handle_possible_reconnect( ssl ) );
4743 }
4744 else
4745#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004746 {
4747 /* Consider buffering the record. */
4748 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4751 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4752 }
4753
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004754 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004755 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004756 }
4757
4758#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4759 /* Replay detection only works for the current epoch */
4760 if( rec_epoch == ssl->in_epoch &&
4761 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4762 {
4763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4764 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4765 }
4766#endif
4767 }
4768#endif /* MBEDTLS_SSL_PROTO_DTLS */
4769
Hanno Becker52c6dc62017-05-26 16:07:36 +01004770
4771 /* Check length against bounds of the current transform and version */
4772 if( ssl->transform_in == NULL )
4773 {
4774 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004775 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004776 {
4777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4778 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4779 }
4780 }
4781 else
4782 {
4783 if( ssl->in_msglen < ssl->transform_in->minlen )
4784 {
4785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4786 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4787 }
4788
4789#if defined(MBEDTLS_SSL_PROTO_SSL3)
4790 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004791 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004792 {
4793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4794 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4795 }
4796#endif
4797#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4798 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4799 /*
4800 * TLS encrypted messages can have up to 256 bytes of padding
4801 */
4802 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4803 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004804 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004805 {
4806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4807 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4808 }
4809#endif
4810 }
4811
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004812 return( 0 );
4813}
Paul Bakker5121ce52009-01-03 21:22:43 +00004814
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004815/*
4816 * If applicable, decrypt (and decompress) record content
4817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004819{
4820 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004823 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4826 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830 ret = mbedtls_ssl_hw_record_read( ssl );
4831 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4834 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004835 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004836
4837 if( ret == 0 )
4838 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004841 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004842 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004843 mbedtls_record rec;
4844
4845 rec.buf = ssl->in_iv;
4846 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4847 - ( ssl->in_iv - ssl->in_buf );
4848 rec.data_len = ssl->in_msglen;
4849 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004850#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004851 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004852 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004853#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004854
4855 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4856 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4857 ssl->conf->transport, rec.ver );
4858 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004859 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4860 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004863
Hanno Beckera5a2b082019-05-15 14:03:01 +01004864#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004865 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4866 ssl->conf->ignore_unexpected_cid
4867 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4868 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004869 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004870 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004871 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004872#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004873
Paul Bakker5121ce52009-01-03 21:22:43 +00004874 return( ret );
4875 }
4876
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004877 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004878 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004879 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4880 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004881 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004882
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004883 /* The record content type may change during decryption,
4884 * so re-read it. */
4885 ssl->in_msgtype = rec.type;
4886 /* Also update the input buffer, because unfortunately
4887 * the server-side ssl_parse_client_hello() reparses the
4888 * record header when receiving a ClientHello initiating
4889 * a renegotiation. */
4890 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004891 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004892 ssl->in_msglen = rec.data_len;
4893 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4894 ssl->in_len[1] = (unsigned char)( rec.data_len );
4895
Paul Bakker5121ce52009-01-03 21:22:43 +00004896 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4897 ssl->in_msg, ssl->in_msglen );
4898
Hanno Beckera5a2b082019-05-15 14:03:01 +01004899#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004900 /* We have already checked the record content type
4901 * in ssl_parse_record_header(), failing or silently
4902 * dropping the record in the case of an unknown type.
4903 *
4904 * Since with the use of CIDs, the record content type
4905 * might change during decryption, re-check the record
4906 * content type, but treat a failure as fatal this time. */
4907 if( ssl_check_record_type( ssl->in_msgtype ) )
4908 {
4909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4910 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4911 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004912#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004913
Angus Grattond8213d02016-05-25 20:56:48 +10004914 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4917 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004918 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004919 else if( ssl->in_msglen == 0 )
4920 {
4921#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4922 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4923 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4924 {
4925 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4927 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4928 }
4929#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4930
4931 ssl->nb_zero++;
4932
4933 /*
4934 * Three or more empty messages may be a DoS attack
4935 * (excessive CPU consumption).
4936 */
4937 if( ssl->nb_zero > 3 )
4938 {
4939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004940 "messages, possible DoS attack" ) );
4941 /* Treat the records as if they were not properly authenticated,
4942 * thereby failing the connection if we see more than allowed
4943 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004944 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4945 }
4946 }
4947 else
4948 ssl->nb_zero = 0;
4949
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004950 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4951#if defined(MBEDTLS_SSL_PROTO_TLS)
4952 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004953 {
4954 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004955 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004956 if( ++ssl->in_ctr[i - 1] != 0 )
4957 break;
4958
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004959 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004960 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004961 {
4962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4963 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4964 }
4965 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004966#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004967
Paul Bakker5121ce52009-01-03 21:22:43 +00004968 }
4969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004971 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004973 {
4974 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004977 return( ret );
4978 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004983 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004986 }
4987#endif
4988
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004989 return( 0 );
4990}
4991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004993
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004994/*
4995 * Read a record.
4996 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004997 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4998 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4999 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005000 */
Hanno Becker1097b342018-08-15 14:09:41 +01005001
5002/* Helper functions for mbedtls_ssl_read_record(). */
5003static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005004static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5005static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005006
Hanno Becker327c93b2018-08-15 13:56:18 +01005007int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005008 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005009{
5010 int ret;
5011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005013
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005014 if( ssl->keep_current_message == 0 )
5015 {
5016 do {
Simon Butcher99000142016-10-13 17:21:01 +01005017
Hanno Becker26994592018-08-15 14:14:59 +01005018 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005019 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005020 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005021
Hanno Beckere74d5562018-08-15 14:26:08 +01005022 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005023 {
Hanno Becker40f50842018-08-15 14:48:01 +01005024#if defined(MBEDTLS_SSL_PROTO_DTLS)
5025 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005026
Hanno Becker40f50842018-08-15 14:48:01 +01005027 /* We only check for buffered messages if the
5028 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005029 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005030 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005031 {
Hanno Becker40f50842018-08-15 14:48:01 +01005032 if( ssl_load_buffered_message( ssl ) == 0 )
5033 have_buffered = 1;
5034 }
5035
5036 if( have_buffered == 0 )
5037#endif /* MBEDTLS_SSL_PROTO_DTLS */
5038 {
5039 ret = ssl_get_next_record( ssl );
5040 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5041 continue;
5042
5043 if( ret != 0 )
5044 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005045 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005046 return( ret );
5047 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005048 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005049 }
5050
5051 ret = mbedtls_ssl_handle_message_type( ssl );
5052
Hanno Becker40f50842018-08-15 14:48:01 +01005053#if defined(MBEDTLS_SSL_PROTO_DTLS)
5054 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5055 {
5056 /* Buffer future message */
5057 ret = ssl_buffer_message( ssl );
5058 if( ret != 0 )
5059 return( ret );
5060
5061 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5062 }
5063#endif /* MBEDTLS_SSL_PROTO_DTLS */
5064
Hanno Becker90333da2017-10-10 11:27:13 +01005065 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5066 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005067
5068 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005069 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005070 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005071 return( ret );
5072 }
5073
Hanno Becker327c93b2018-08-15 13:56:18 +01005074 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005075 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005076 {
5077 mbedtls_ssl_update_handshake_status( ssl );
5078 }
Simon Butcher99000142016-10-13 17:21:01 +01005079 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005080 else
Simon Butcher99000142016-10-13 17:21:01 +01005081 {
Hanno Becker02f59072018-08-15 14:00:24 +01005082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005083 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005084 }
5085
5086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5087
5088 return( 0 );
5089}
5090
Hanno Becker40f50842018-08-15 14:48:01 +01005091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005092static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005093{
Hanno Becker40f50842018-08-15 14:48:01 +01005094 if( ssl->in_left > ssl->next_record_offset )
5095 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005096
Hanno Becker40f50842018-08-15 14:48:01 +01005097 return( 0 );
5098}
5099
5100static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5101{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005102 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005103 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005104 int ret = 0;
5105
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005106 if( hs == NULL )
5107 return( -1 );
5108
Hanno Beckere00ae372018-08-20 09:39:42 +01005109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5110
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005111 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5112 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5113 {
5114 /* Check if we have seen a ChangeCipherSpec before.
5115 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005116 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005117 {
5118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5119 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005120 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005121 }
5122
Hanno Becker39b8bc92018-08-28 17:17:13 +01005123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005124 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5125 ssl->in_msglen = 1;
5126 ssl->in_msg[0] = 1;
5127
5128 /* As long as they are equal, the exact value doesn't matter. */
5129 ssl->in_left = 0;
5130 ssl->next_record_offset = 0;
5131
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005132 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005133 goto exit;
5134 }
Hanno Becker37f95322018-08-16 13:55:32 +01005135
Hanno Beckerb8f50142018-08-28 10:01:34 +01005136#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005137 /* Debug only */
5138 {
5139 unsigned offset;
5140 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5141 {
5142 hs_buf = &hs->buffering.hs[offset];
5143 if( hs_buf->is_valid == 1 )
5144 {
5145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5146 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005147 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005148 }
5149 }
5150 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005151#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005152
5153 /* Check if we have buffered and/or fully reassembled the
5154 * next handshake message. */
5155 hs_buf = &hs->buffering.hs[0];
5156 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5157 {
5158 /* Synthesize a record containing the buffered HS message. */
5159 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5160 ( hs_buf->data[2] << 8 ) |
5161 hs_buf->data[3];
5162
5163 /* Double-check that we haven't accidentally buffered
5164 * a message that doesn't fit into the input buffer. */
5165 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5166 {
5167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5168 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5169 }
5170
5171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5172 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5173 hs_buf->data, msg_len + 12 );
5174
5175 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5176 ssl->in_hslen = msg_len + 12;
5177 ssl->in_msglen = msg_len + 12;
5178 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5179
5180 ret = 0;
5181 goto exit;
5182 }
5183 else
5184 {
5185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5186 hs->in_msg_seq ) );
5187 }
5188
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005189 ret = -1;
5190
5191exit:
5192
5193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5194 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005195}
5196
Hanno Beckera02b0b42018-08-21 17:20:27 +01005197static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5198 size_t desired )
5199{
5200 int offset;
5201 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5203 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005204
Hanno Becker01315ea2018-08-21 17:22:17 +01005205 /* Get rid of future records epoch first, if such exist. */
5206 ssl_free_buffered_record( ssl );
5207
5208 /* Check if we have enough space available now. */
5209 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5210 hs->buffering.total_bytes_buffered ) )
5211 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005213 return( 0 );
5214 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005215
Hanno Becker4f432ad2018-08-28 10:02:32 +01005216 /* We don't have enough space to buffer the next expected handshake
5217 * message. Remove buffers used for future messages to gain space,
5218 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005219 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5220 offset >= 0; offset-- )
5221 {
5222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5223 offset ) );
5224
Hanno Beckerb309b922018-08-23 13:18:05 +01005225 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005226
5227 /* Check if we have enough space available now. */
5228 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5229 hs->buffering.total_bytes_buffered ) )
5230 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005232 return( 0 );
5233 }
5234 }
5235
5236 return( -1 );
5237}
5238
Hanno Becker40f50842018-08-15 14:48:01 +01005239static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5240{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005241 int ret = 0;
5242 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5243
5244 if( hs == NULL )
5245 return( 0 );
5246
5247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5248
5249 switch( ssl->in_msgtype )
5250 {
5251 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005253
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005254 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005255 break;
5256
5257 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005258 {
5259 unsigned recv_msg_seq_offset;
5260 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5261 mbedtls_ssl_hs_buffer *hs_buf;
5262 size_t msg_len = ssl->in_hslen - 12;
5263
5264 /* We should never receive an old handshake
5265 * message - double-check nonetheless. */
5266 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5267 {
5268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5270 }
5271
5272 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5273 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5274 {
5275 /* Silently ignore -- message too far in the future */
5276 MBEDTLS_SSL_DEBUG_MSG( 2,
5277 ( "Ignore future HS message with sequence number %u, "
5278 "buffering window %u - %u",
5279 recv_msg_seq, ssl->handshake->in_msg_seq,
5280 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5281
5282 goto exit;
5283 }
5284
5285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5286 recv_msg_seq, recv_msg_seq_offset ) );
5287
5288 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5289
5290 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005291 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005292 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005293 size_t reassembly_buf_sz;
5294
Hanno Becker37f95322018-08-16 13:55:32 +01005295 hs_buf->is_fragmented =
5296 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5297
5298 /* We copy the message back into the input buffer
5299 * after reassembly, so check that it's not too large.
5300 * This is an implementation-specific limitation
5301 * and not one from the standard, hence it is not
5302 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005303 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005304 {
5305 /* Ignore message */
5306 goto exit;
5307 }
5308
Hanno Beckere0b150f2018-08-21 15:51:03 +01005309 /* Check if we have enough space to buffer the message. */
5310 if( hs->buffering.total_bytes_buffered >
5311 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5312 {
5313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5315 }
5316
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005317 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5318 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005319
5320 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5321 hs->buffering.total_bytes_buffered ) )
5322 {
5323 if( recv_msg_seq_offset > 0 )
5324 {
5325 /* If we can't buffer a future message because
5326 * of space limitations -- ignore. */
5327 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",
5328 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5329 (unsigned) hs->buffering.total_bytes_buffered ) );
5330 goto exit;
5331 }
Hanno Beckere1801392018-08-21 16:51:05 +01005332 else
5333 {
5334 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",
5335 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5336 (unsigned) hs->buffering.total_bytes_buffered ) );
5337 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005338
Hanno Beckera02b0b42018-08-21 17:20:27 +01005339 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005340 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005341 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",
5342 (unsigned) msg_len,
5343 (unsigned) reassembly_buf_sz,
5344 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005345 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005346 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5347 goto exit;
5348 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005349 }
5350
5351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5352 msg_len ) );
5353
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005354 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5355 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005356 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005357 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005358 goto exit;
5359 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005360 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005361
5362 /* Prepare final header: copy msg_type, length and message_seq,
5363 * then add standardised fragment_offset and fragment_length */
5364 memcpy( hs_buf->data, ssl->in_msg, 6 );
5365 memset( hs_buf->data + 6, 0, 3 );
5366 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5367
5368 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005369
5370 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005371 }
5372 else
5373 {
5374 /* Make sure msg_type and length are consistent */
5375 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5376 {
5377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5378 /* Ignore */
5379 goto exit;
5380 }
5381 }
5382
Hanno Becker4422bbb2018-08-20 09:40:19 +01005383 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005384 {
5385 size_t frag_len, frag_off;
5386 unsigned char * const msg = hs_buf->data + 12;
5387
5388 /*
5389 * Check and copy current fragment
5390 */
5391
5392 /* Validation of header fields already done in
5393 * mbedtls_ssl_prepare_handshake_record(). */
5394 frag_off = ssl_get_hs_frag_off( ssl );
5395 frag_len = ssl_get_hs_frag_len( ssl );
5396
5397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5398 frag_off, frag_len ) );
5399 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5400
5401 if( hs_buf->is_fragmented )
5402 {
5403 unsigned char * const bitmask = msg + msg_len;
5404 ssl_bitmask_set( bitmask, frag_off, frag_len );
5405 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5406 msg_len ) == 0 );
5407 }
5408 else
5409 {
5410 hs_buf->is_complete = 1;
5411 }
5412
5413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5414 hs_buf->is_complete ? "" : "not yet " ) );
5415 }
5416
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005417 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005418 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005419
5420 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005421 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005422 break;
5423 }
5424
5425exit:
5426
5427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5428 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005429}
5430#endif /* MBEDTLS_SSL_PROTO_DTLS */
5431
Hanno Becker1097b342018-08-15 14:09:41 +01005432static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005433{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005434 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005435 * Consume last content-layer message and potentially
5436 * update in_msglen which keeps track of the contents'
5437 * consumption state.
5438 *
5439 * (1) Handshake messages:
5440 * Remove last handshake message, move content
5441 * and adapt in_msglen.
5442 *
5443 * (2) Alert messages:
5444 * Consume whole record content, in_msglen = 0.
5445 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005446 * (3) Change cipher spec:
5447 * Consume whole record content, in_msglen = 0.
5448 *
5449 * (4) Application data:
5450 * Don't do anything - the record layer provides
5451 * the application data as a stream transport
5452 * and consumes through mbedtls_ssl_read only.
5453 *
5454 */
5455
5456 /* Case (1): Handshake messages */
5457 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005458 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005459 /* Hard assertion to be sure that no application data
5460 * is in flight, as corrupting ssl->in_msglen during
5461 * ssl->in_offt != NULL is fatal. */
5462 if( ssl->in_offt != NULL )
5463 {
5464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5465 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5466 }
5467
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005468 /*
5469 * Get next Handshake message in the current record
5470 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005471
Hanno Becker4a810fb2017-05-24 16:27:30 +01005472 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005473 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005474 * current handshake content: If DTLS handshake
5475 * fragmentation is used, that's the fragment
5476 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005477 * size here is faulty and should be changed at
5478 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005479 * (2) While it doesn't seem to cause problems, one
5480 * has to be very careful not to assume that in_hslen
5481 * is always <= in_msglen in a sensible communication.
5482 * Again, it's wrong for DTLS handshake fragmentation.
5483 * The following check is therefore mandatory, and
5484 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005485 * Additionally, ssl->in_hslen might be arbitrarily out of
5486 * bounds after handling a DTLS message with an unexpected
5487 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005488 */
5489 if( ssl->in_hslen < ssl->in_msglen )
5490 {
5491 ssl->in_msglen -= ssl->in_hslen;
5492 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5493 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005494
Hanno Becker4a810fb2017-05-24 16:27:30 +01005495 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5496 ssl->in_msg, ssl->in_msglen );
5497 }
5498 else
5499 {
5500 ssl->in_msglen = 0;
5501 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005502
Hanno Becker4a810fb2017-05-24 16:27:30 +01005503 ssl->in_hslen = 0;
5504 }
5505 /* Case (4): Application data */
5506 else if( ssl->in_offt != NULL )
5507 {
5508 return( 0 );
5509 }
5510 /* Everything else (CCS & Alerts) */
5511 else
5512 {
5513 ssl->in_msglen = 0;
5514 }
5515
Hanno Becker1097b342018-08-15 14:09:41 +01005516 return( 0 );
5517}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005518
Hanno Beckere74d5562018-08-15 14:26:08 +01005519static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5520{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005521 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005522 return( 1 );
5523
5524 return( 0 );
5525}
5526
Hanno Becker5f066e72018-08-16 14:56:31 +01005527#if defined(MBEDTLS_SSL_PROTO_DTLS)
5528
5529static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5530{
5531 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5532 if( hs == NULL )
5533 return;
5534
Hanno Becker01315ea2018-08-21 17:22:17 +01005535 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005536 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005537 hs->buffering.total_bytes_buffered -=
5538 hs->buffering.future_record.len;
5539
5540 mbedtls_free( hs->buffering.future_record.data );
5541 hs->buffering.future_record.data = NULL;
5542 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005543}
5544
5545static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5546{
5547 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5548 unsigned char * rec;
5549 size_t rec_len;
5550 unsigned rec_epoch;
5551
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005552 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005553 return( 0 );
5554
5555 if( hs == NULL )
5556 return( 0 );
5557
Hanno Becker5f066e72018-08-16 14:56:31 +01005558 rec = hs->buffering.future_record.data;
5559 rec_len = hs->buffering.future_record.len;
5560 rec_epoch = hs->buffering.future_record.epoch;
5561
5562 if( rec == NULL )
5563 return( 0 );
5564
Hanno Becker4cb782d2018-08-20 11:19:05 +01005565 /* Only consider loading future records if the
5566 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005567 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005568 return( 0 );
5569
Hanno Becker5f066e72018-08-16 14:56:31 +01005570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5571
5572 if( rec_epoch != ssl->in_epoch )
5573 {
5574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5575 goto exit;
5576 }
5577
5578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5579
5580 /* Double-check that the record is not too large */
5581 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5582 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5583 {
5584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5586 }
5587
5588 memcpy( ssl->in_hdr, rec, rec_len );
5589 ssl->in_left = rec_len;
5590 ssl->next_record_offset = 0;
5591
5592 ssl_free_buffered_record( ssl );
5593
5594exit:
5595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5596 return( 0 );
5597}
5598
5599static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5600{
5601 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5602 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005603 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005604
5605 /* Don't buffer future records outside handshakes. */
5606 if( hs == NULL )
5607 return( 0 );
5608
5609 /* Only buffer handshake records (we are only interested
5610 * in Finished messages). */
5611 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5612 return( 0 );
5613
5614 /* Don't buffer more than one future epoch record. */
5615 if( hs->buffering.future_record.data != NULL )
5616 return( 0 );
5617
Hanno Becker01315ea2018-08-21 17:22:17 +01005618 /* Don't buffer record if there's not enough buffering space remaining. */
5619 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5620 hs->buffering.total_bytes_buffered ) )
5621 {
5622 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",
5623 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5624 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005625 return( 0 );
5626 }
5627
Hanno Becker5f066e72018-08-16 14:56:31 +01005628 /* Buffer record */
5629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5630 ssl->in_epoch + 1 ) );
5631 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5632 rec_hdr_len + ssl->in_msglen );
5633
5634 /* ssl_parse_record_header() only considers records
5635 * of the next epoch as candidates for buffering. */
5636 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005637 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005638
5639 hs->buffering.future_record.data =
5640 mbedtls_calloc( 1, hs->buffering.future_record.len );
5641 if( hs->buffering.future_record.data == NULL )
5642 {
5643 /* If we run out of RAM trying to buffer a
5644 * record from the next epoch, just ignore. */
5645 return( 0 );
5646 }
5647
Hanno Becker01315ea2018-08-21 17:22:17 +01005648 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005649
Hanno Becker01315ea2018-08-21 17:22:17 +01005650 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005651 return( 0 );
5652}
5653
5654#endif /* MBEDTLS_SSL_PROTO_DTLS */
5655
Hanno Beckere74d5562018-08-15 14:26:08 +01005656static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005657{
5658 int ret;
5659
Hanno Becker5f066e72018-08-16 14:56:31 +01005660#if defined(MBEDTLS_SSL_PROTO_DTLS)
5661 /* We might have buffered a future record; if so,
5662 * and if the epoch matches now, load it.
5663 * On success, this call will set ssl->in_left to
5664 * the length of the buffered record, so that
5665 * the calls to ssl_fetch_input() below will
5666 * essentially be no-ops. */
5667 ret = ssl_load_buffered_record( ssl );
5668 if( ret != 0 )
5669 return( ret );
5670#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005671
Hanno Becker8b09b732019-05-08 12:03:28 +01005672 /* Reset in pointers to default state for TLS/DTLS records,
5673 * assuming no CID and no offset between record content and
5674 * record plaintext. */
5675 ssl_update_in_pointers( ssl );
5676
5677 /* Ensure that we have enough space available for the default form
5678 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5679 * with no space for CIDs counted in). */
5680 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5681 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005684 return( ret );
5685 }
5686
5687 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005690 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005691 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005692 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005693 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5694 {
5695 ret = ssl_buffer_future_record( ssl );
5696 if( ret != 0 )
5697 return( ret );
5698
5699 /* Fall through to handling of unexpected records */
5700 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5701 }
5702
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005703 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5704 {
5705 /* Skip unexpected record (but not whole datagram) */
5706 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005707 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005708
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5710 "(header)" ) );
5711 }
5712 else
5713 {
5714 /* Skip invalid record and the rest of the datagram */
5715 ssl->next_record_offset = 0;
5716 ssl->in_left = 0;
5717
5718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5719 "(header)" ) );
5720 }
5721
5722 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005723 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005724 }
5725#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005726 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005727 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005728
5729 /*
5730 * Read and optionally decrypt the message contents
5731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005733 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005735 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005736 return( ret );
5737 }
5738
5739 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005742 {
Hanno Becker43395762019-05-03 14:46:38 +01005743 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005744 if( ssl->next_record_offset < ssl->in_left )
5745 {
5746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5747 }
5748 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005749 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005750#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005751#if defined(MBEDTLS_SSL_PROTO_TLS)
5752 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005753 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005754 }
5755#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005756
5757 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005760 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005761 {
5762 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005763 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005764 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005765 /* Except when waiting for Finished as a bad mac here
5766 * probably means something went wrong in the handshake
5767 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5768 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5769 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5770 {
5771#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5772 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5773 {
5774 mbedtls_ssl_send_alert_message( ssl,
5775 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5776 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5777 }
5778#endif
5779 return( ret );
5780 }
5781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01005783 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
5784 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5787 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005788 }
5789#endif
5790
Hanno Becker4a810fb2017-05-24 16:27:30 +01005791 /* As above, invalid records cause
5792 * dismissal of the whole datagram. */
5793
5794 ssl->next_record_offset = 0;
5795 ssl->in_left = 0;
5796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005798 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005799 }
5800
5801 return( ret );
5802 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005803 MBEDTLS_SSL_TRANSPORT_ELSE
5804#endif /* MBEDTLS_SSL_PROTO_DTLS */
5805#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005806 {
5807 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5809 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 mbedtls_ssl_send_alert_message( ssl,
5812 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5813 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005814 }
5815#endif
5816 return( ret );
5817 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005818#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005819 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005820
Simon Butcher99000142016-10-13 17:21:01 +01005821 return( 0 );
5822}
5823
5824int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5825{
5826 int ret;
5827
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005828 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005829 * Handle particular types of records
5830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005832 {
Simon Butcher99000142016-10-13 17:21:01 +01005833 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5834 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005835 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005836 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 }
5838
Hanno Beckere678eaa2018-08-21 14:57:46 +01005839 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005840 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005841 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005842 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5844 ssl->in_msglen ) );
5845 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005846 }
5847
Hanno Beckere678eaa2018-08-21 14:57:46 +01005848 if( ssl->in_msg[0] != 1 )
5849 {
5850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5851 ssl->in_msg[0] ) );
5852 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5853 }
5854
5855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005856 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005857 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5858 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5859 {
5860 if( ssl->handshake == NULL )
5861 {
5862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5863 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5864 }
5865
5866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5867 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5868 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005869#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005870 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005873 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005874 if( ssl->in_msglen != 2 )
5875 {
5876 /* Note: Standard allows for more than one 2 byte alert
5877 to be packed in a single message, but Mbed TLS doesn't
5878 currently support this. */
5879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5880 ssl->in_msglen ) );
5881 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5882 }
5883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005885 ssl->in_msg[0], ssl->in_msg[1] ) );
5886
5887 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005888 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005893 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 }
5896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5898 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5901 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005902 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005903
5904#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5905 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5906 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5907 {
Hanno Becker90333da2017-10-10 11:27:13 +01005908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005909 /* Will be handled when trying to parse ServerHello */
5910 return( 0 );
5911 }
5912#endif
5913
5914#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5915 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5916 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5917 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5918 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5919 {
5920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5921 /* Will be handled in mbedtls_ssl_parse_certificate() */
5922 return( 0 );
5923 }
5924#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5925
5926 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005927 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005928 }
5929
Hanno Beckerc76c6192017-06-06 10:03:17 +01005930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005931 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005932 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005933 /* Drop unexpected ApplicationData records,
5934 * except at the beginning of renegotiations */
5935 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5936 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5937#if defined(MBEDTLS_SSL_RENEGOTIATION)
5938 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5939 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005940#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005941 )
5942 {
5943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5944 return( MBEDTLS_ERR_SSL_NON_FATAL );
5945 }
5946
5947 if( ssl->handshake != NULL &&
5948 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5949 {
5950 ssl_handshake_wrapup_free_hs_transform( ssl );
5951 }
5952 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005953#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005954
Paul Bakker5121ce52009-01-03 21:22:43 +00005955 return( 0 );
5956}
5957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005959{
5960 int ret;
5961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005962 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5963 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5964 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005965 {
5966 return( ret );
5967 }
5968
5969 return( 0 );
5970}
5971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005973 unsigned char level,
5974 unsigned char message )
5975{
5976 int ret;
5977
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005978 if( ssl == NULL || ssl->conf == NULL )
5979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005985 ssl->out_msglen = 2;
5986 ssl->out_msg[0] = level;
5987 ssl->out_msg[1] = message;
5988
Hanno Becker67bc7c32018-08-06 11:33:50 +01005989 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005992 return( ret );
5993 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005995
5996 return( 0 );
5997}
5998
Hanno Becker17572472019-02-08 07:19:04 +00005999#if defined(MBEDTLS_X509_CRT_PARSE_C)
6000static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6001{
6002#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6003 if( session->peer_cert != NULL )
6004 {
6005 mbedtls_x509_crt_free( session->peer_cert );
6006 mbedtls_free( session->peer_cert );
6007 session->peer_cert = NULL;
6008 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006009#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006010 if( session->peer_cert_digest != NULL )
6011 {
6012 /* Zeroization is not necessary. */
6013 mbedtls_free( session->peer_cert_digest );
6014 session->peer_cert_digest = NULL;
6015 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6016 session->peer_cert_digest_len = 0;
6017 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006018#else
6019 ((void) session);
6020#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006021}
6022#endif /* MBEDTLS_X509_CRT_PARSE_C */
6023
Paul Bakker5121ce52009-01-03 21:22:43 +00006024/*
6025 * Handshake functions
6026 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006027#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006028/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006030{
Hanno Becker8759e162017-12-27 21:34:08 +00006031 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006034
Hanno Becker5097cba2019-02-05 13:36:46 +00006035 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006038 ssl->state++;
6039 return( 0 );
6040 }
6041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6043 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006044}
6045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006047{
Hanno Becker8759e162017-12-27 21:34:08 +00006048 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006051
Hanno Becker5097cba2019-02-05 13:36:46 +00006052 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006055 ssl->state++;
6056 return( 0 );
6057 }
6058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006061}
Gilles Peskinef9828522017-05-03 12:28:43 +02006062
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006063#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006064/* Some certificate support -> implement write and parse */
6065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006069 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006071 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006074
Hanno Becker5097cba2019-02-05 13:36:46 +00006075 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006078 ssl->state++;
6079 return( 0 );
6080 }
6081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006083 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006084 {
6085 if( ssl->client_auth == 0 )
6086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006088 ssl->state++;
6089 return( 0 );
6090 }
6091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 /*
6094 * If using SSLv3 and got no cert, send an Alert message
6095 * (otherwise an empty Certificate message will be sent).
6096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6098 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
6100 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6102 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6103 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 goto write_msg;
6107 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110#endif /* MBEDTLS_SSL_CLI_C */
6111#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006112 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6117 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006118 }
6119 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006120#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006123
6124 /*
6125 * 0 . 0 handshake type
6126 * 1 . 3 handshake length
6127 * 4 . 6 length of all certs
6128 * 7 . 9 length of cert. 1
6129 * 10 . n-1 peer certificate
6130 * n . n+2 length of cert. 2
6131 * n+3 . ... upper level cert, etc.
6132 */
6133 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006135
Paul Bakker29087132010-03-21 21:03:34 +00006136 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006137 {
6138 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006139 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006142 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006144 }
6145
6146 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6147 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6148 ssl->out_msg[i + 2] = (unsigned char)( n );
6149
6150 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6151 i += n; crt = crt->next;
6152 }
6153
6154 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6155 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6156 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6157
6158 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6160 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006161
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006162#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006163write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006164#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006165
6166 ssl->state++;
6167
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006168 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006169 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006171 return( ret );
6172 }
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006175
Paul Bakkered27a042013-04-18 22:46:23 +02006176 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006177}
6178
Hanno Becker285ff0c2019-01-31 07:44:03 +00006179#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006180
6181#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006182static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6183 unsigned char *crt_buf,
6184 size_t crt_buf_len )
6185{
6186 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6187
6188 if( peer_crt == NULL )
6189 return( -1 );
6190
6191 if( peer_crt->raw.len != crt_buf_len )
6192 return( -1 );
6193
Hanno Becker68b856d2019-02-08 14:00:04 +00006194 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006195}
Hanno Becker5882dd02019-06-06 16:25:57 +01006196#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006197static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6198 unsigned char *crt_buf,
6199 size_t crt_buf_len )
6200{
6201 int ret;
6202 unsigned char const * const peer_cert_digest =
6203 ssl->session->peer_cert_digest;
6204 mbedtls_md_type_t const peer_cert_digest_type =
6205 ssl->session->peer_cert_digest_type;
6206 mbedtls_md_info_t const * const digest_info =
6207 mbedtls_md_info_from_type( peer_cert_digest_type );
6208 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6209 size_t digest_len;
6210
6211 if( peer_cert_digest == NULL || digest_info == NULL )
6212 return( -1 );
6213
6214 digest_len = mbedtls_md_get_size( digest_info );
6215 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6216 return( -1 );
6217
6218 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6219 if( ret != 0 )
6220 return( -1 );
6221
6222 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6223}
Hanno Becker5882dd02019-06-06 16:25:57 +01006224#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006225#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006226
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006227/*
6228 * Once the certificate message is read, parse it into a cert chain and
6229 * perform basic checks, but leave actual verification to the caller
6230 */
Hanno Becker35e41772019-02-05 15:37:23 +00006231static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6232 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006233{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006234 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006235#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6236 int crt_cnt=0;
6237#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006238 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006239 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006244 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6245 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006247 }
6248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6250 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006253 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6254 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006256 }
6257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006259
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006262 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006263 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006264
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006265 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006269 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6270 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006272 }
6273
Hanno Becker33c3dc82019-01-30 14:46:46 +00006274 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6275 i += 3;
6276
Hanno Becker33c3dc82019-01-30 14:46:46 +00006277 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006278 while( i < ssl->in_hslen )
6279 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006280 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006281 if ( i + 3 > ssl->in_hslen ) {
6282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006283 mbedtls_ssl_send_alert_message( ssl,
6284 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6285 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006286 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6287 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006288 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6289 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006290 if( ssl->in_msg[i] != 0 )
6291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006293 mbedtls_ssl_send_alert_message( ssl,
6294 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6295 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006297 }
6298
Hanno Becker33c3dc82019-01-30 14:46:46 +00006299 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006300 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6301 | (unsigned int) ssl->in_msg[i + 2];
6302 i += 3;
6303
6304 if( n < 128 || i + n > ssl->in_hslen )
6305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006307 mbedtls_ssl_send_alert_message( ssl,
6308 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6309 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006311 }
6312
Hanno Becker33c3dc82019-01-30 14:46:46 +00006313 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006314#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6315 if( crt_cnt++ == 0 &&
6316 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6317 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006318 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006319 /* During client-side renegotiation, check that the server's
6320 * end-CRTs hasn't changed compared to the initial handshake,
6321 * mitigating the triple handshake attack. On success, reuse
6322 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6324 if( ssl_check_peer_crt_unchanged( ssl,
6325 &ssl->in_msg[i],
6326 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006327 {
Hanno Becker35e41772019-02-05 15:37:23 +00006328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6329 mbedtls_ssl_send_alert_message( ssl,
6330 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6331 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6332 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006333 }
Hanno Becker35e41772019-02-05 15:37:23 +00006334
6335 /* Now we can safely free the original chain. */
6336 ssl_clear_peer_cert( ssl->session );
6337 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006338#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6339
Hanno Becker33c3dc82019-01-30 14:46:46 +00006340 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006341#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006342 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006343#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006344 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006345 * it in-place from the input buffer instead of making a copy. */
6346 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6347#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006348 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006349 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006350 case 0: /*ok*/
6351 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6352 /* Ignore certificate with an unknown algorithm: maybe a
6353 prior certificate was already trusted. */
6354 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006355
Hanno Becker33c3dc82019-01-30 14:46:46 +00006356 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6357 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6358 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006359
Hanno Becker33c3dc82019-01-30 14:46:46 +00006360 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6361 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6362 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006363
Hanno Becker33c3dc82019-01-30 14:46:46 +00006364 default:
6365 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6366 crt_parse_der_failed:
6367 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6368 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6369 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006370 }
6371
6372 i += n;
6373 }
6374
Hanno Becker35e41772019-02-05 15:37:23 +00006375 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006376 return( 0 );
6377}
6378
Hanno Beckerb8a08572019-02-05 12:49:06 +00006379#if defined(MBEDTLS_SSL_SRV_C)
6380static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6381{
6382 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6383 return( -1 );
6384
6385#if defined(MBEDTLS_SSL_PROTO_SSL3)
6386 /*
6387 * Check if the client sent an empty certificate
6388 */
6389 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6390 {
6391 if( ssl->in_msglen == 2 &&
6392 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6393 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6394 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6395 {
6396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6397 return( 0 );
6398 }
6399
6400 return( -1 );
6401 }
6402#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6403
6404#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6405 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6406 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6407 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6408 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6409 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6410 {
6411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6412 return( 0 );
6413 }
6414
Hanno Beckerb8a08572019-02-05 12:49:06 +00006415#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6416 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01006417
6418 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00006419}
6420#endif /* MBEDTLS_SSL_SRV_C */
6421
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006422/* Check if a certificate message is expected.
6423 * Return either
6424 * - SSL_CERTIFICATE_EXPECTED, or
6425 * - SSL_CERTIFICATE_SKIP
6426 * indicating whether a Certificate message is expected or not.
6427 */
6428#define SSL_CERTIFICATE_EXPECTED 0
6429#define SSL_CERTIFICATE_SKIP 1
6430static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6431 int authmode )
6432{
6433 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6434 ssl->handshake->ciphersuite_info;
6435
6436 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6437 return( SSL_CERTIFICATE_SKIP );
6438
6439#if defined(MBEDTLS_SSL_SRV_C)
6440 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6441 {
6442 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6443 return( SSL_CERTIFICATE_SKIP );
6444
6445 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6446 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006447 ssl->session_negotiate->verify_result =
6448 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6449 return( SSL_CERTIFICATE_SKIP );
6450 }
6451 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006452#else
6453 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006454#endif /* MBEDTLS_SSL_SRV_C */
6455
6456 return( SSL_CERTIFICATE_EXPECTED );
6457}
6458
Hanno Becker3cf50612019-02-05 14:36:34 +00006459static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6460 int authmode,
6461 mbedtls_x509_crt *chain,
6462 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006463{
Hanno Becker613d4902019-02-05 13:11:17 +00006464 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006465 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6466 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006467 mbedtls_x509_crt *ca_chain;
6468 mbedtls_x509_crl *ca_crl;
6469
6470 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6471 return( 0 );
6472
6473#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6474 if( ssl->handshake->sni_ca_chain != NULL )
6475 {
6476 ca_chain = ssl->handshake->sni_ca_chain;
6477 ca_crl = ssl->handshake->sni_ca_crl;
6478 }
6479 else
6480#endif
6481 {
6482 ca_chain = ssl->conf->ca_chain;
6483 ca_crl = ssl->conf->ca_crl;
6484 }
6485
6486 /*
6487 * Main check: verify certificate
6488 */
6489 ret = mbedtls_x509_crt_verify_restartable(
6490 chain,
6491 ca_chain, ca_crl,
6492 ssl->conf->cert_profile,
6493 ssl->hostname,
6494 &ssl->session_negotiate->verify_result,
6495 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6496
6497 if( ret != 0 )
6498 {
6499 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6500 }
6501
6502#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6503 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6504 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6505#endif
6506
6507 /*
6508 * Secondary checks: always done, but change 'ret' only if it was 0
6509 */
6510
6511#if defined(MBEDTLS_ECP_C)
6512 {
6513 const mbedtls_pk_context *pk = &chain->pk;
6514
6515 /* If certificate uses an EC key, make sure the curve is OK */
6516 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6517 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6518 {
6519 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6520
6521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6522 if( ret == 0 )
6523 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6524 }
6525 }
6526#endif /* MBEDTLS_ECP_C */
6527
6528 if( mbedtls_ssl_check_cert_usage( chain,
6529 ciphersuite_info,
6530 ! ssl->conf->endpoint,
6531 &ssl->session_negotiate->verify_result ) != 0 )
6532 {
6533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6534 if( ret == 0 )
6535 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6536 }
6537
6538 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6539 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6540 * with details encoded in the verification flags. All other kinds
6541 * of error codes, including those from the user provided f_vrfy
6542 * functions, are treated as fatal and lead to a failure of
6543 * ssl_parse_certificate even if verification was optional. */
6544 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6545 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6546 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6547 {
6548 ret = 0;
6549 }
6550
6551 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6552 {
6553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6554 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6555 }
6556
6557 if( ret != 0 )
6558 {
6559 uint8_t alert;
6560
6561 /* The certificate may have been rejected for several reasons.
6562 Pick one and send the corresponding alert. Which alert to send
6563 may be a subject of debate in some cases. */
6564 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6565 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6566 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6567 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6568 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6569 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6570 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6571 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6572 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6573 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6574 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6575 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6576 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6577 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6578 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6579 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6580 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6581 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6582 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6583 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6584 else
6585 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6586 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6587 alert );
6588 }
6589
6590#if defined(MBEDTLS_DEBUG_C)
6591 if( ssl->session_negotiate->verify_result != 0 )
6592 {
6593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6594 ssl->session_negotiate->verify_result ) );
6595 }
6596 else
6597 {
6598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6599 }
6600#endif /* MBEDTLS_DEBUG_C */
6601
6602 return( ret );
6603}
6604
Hanno Becker34106f62019-02-08 14:59:05 +00006605#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01006606
6607#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006608static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6609 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006610{
6611 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00006612 /* Remember digest of the peer's end-CRT. */
6613 ssl->session_negotiate->peer_cert_digest =
6614 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6615 if( ssl->session_negotiate->peer_cert_digest == NULL )
6616 {
6617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6618 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6619 mbedtls_ssl_send_alert_message( ssl,
6620 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6621 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6622
6623 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6624 }
6625
6626 ret = mbedtls_md( mbedtls_md_info_from_type(
6627 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6628 start, len,
6629 ssl->session_negotiate->peer_cert_digest );
6630
6631 ssl->session_negotiate->peer_cert_digest_type =
6632 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6633 ssl->session_negotiate->peer_cert_digest_len =
6634 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6635
6636 return( ret );
6637}
Hanno Becker5882dd02019-06-06 16:25:57 +01006638#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006639
6640static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6641 unsigned char *start, size_t len )
6642{
6643 unsigned char *end = start + len;
6644 int ret;
6645
6646 /* Make a copy of the peer's raw public key. */
6647 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6648 ret = mbedtls_pk_parse_subpubkey( &start, end,
6649 &ssl->handshake->peer_pubkey );
6650 if( ret != 0 )
6651 {
6652 /* We should have parsed the public key before. */
6653 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6654 }
6655
6656 return( 0 );
6657}
6658#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6659
Hanno Becker3cf50612019-02-05 14:36:34 +00006660int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6661{
6662 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006663 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006664#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6665 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6666 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01006667 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006668#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01006669 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006670#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006671 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006672 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006673
6674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6675
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006676 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6677 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006678 {
6679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006680 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006681 }
6682
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006683#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6684 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006685 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006686 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006687 chain = ssl->handshake->ecrs_peer_cert;
6688 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006689 goto crt_verify;
6690 }
6691#endif
6692
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006693 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006694 {
6695 /* mbedtls_ssl_read_record may have sent an alert already. We
6696 let it decide whether to alert. */
6697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006698 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006699 }
6700
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006701#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00006702 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6703 {
6704 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006705
Hanno Beckerb8a08572019-02-05 12:49:06 +00006706 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006707 ret = 0;
6708 else
6709 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006710
Hanno Becker613d4902019-02-05 13:11:17 +00006711 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006712 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00006713#endif /* MBEDTLS_SSL_SRV_C */
6714
Hanno Becker35e41772019-02-05 15:37:23 +00006715 /* Clear existing peer CRT structure in case we tried to
6716 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006717 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006718
6719 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6720 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006721 {
6722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6723 sizeof( mbedtls_x509_crt ) ) );
6724 mbedtls_ssl_send_alert_message( ssl,
6725 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6726 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006727
Hanno Beckere4aeb762019-02-05 17:19:52 +00006728 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6729 goto exit;
6730 }
6731 mbedtls_x509_crt_init( chain );
6732
6733 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006734 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006735 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006736
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006737#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6738 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006739 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006740
6741crt_verify:
6742 if( ssl->handshake->ecrs_enabled)
6743 rs_ctx = &ssl->handshake->ecrs_ctx;
6744#endif
6745
Hanno Becker3cf50612019-02-05 14:36:34 +00006746 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006747 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006748 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006749 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006750
Hanno Becker3008d282019-02-05 17:02:28 +00006751#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00006752 {
Hanno Becker5882dd02019-06-06 16:25:57 +01006753 size_t pk_len;
6754 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00006755
Hanno Becker34106f62019-02-08 14:59:05 +00006756 /* We parse the CRT chain without copying, so
6757 * these pointers point into the input buffer,
6758 * and are hence still valid after freeing the
6759 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006760
Hanno Becker5882dd02019-06-06 16:25:57 +01006761#if defined(MBEDTLS_SSL_RENEGOTIATION)
6762 unsigned char *crt_start;
6763 size_t crt_len;
6764
Hanno Becker34106f62019-02-08 14:59:05 +00006765 crt_start = chain->raw.p;
6766 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01006767#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006768
Hanno Becker34106f62019-02-08 14:59:05 +00006769 pk_start = chain->pk_raw.p;
6770 pk_len = chain->pk_raw.len;
6771
6772 /* Free the CRT structures before computing
6773 * digest and copying the peer's public key. */
6774 mbedtls_x509_crt_free( chain );
6775 mbedtls_free( chain );
6776 chain = NULL;
6777
Hanno Becker5882dd02019-06-06 16:25:57 +01006778#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006779 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006781 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01006782#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006783
Hanno Becker34106f62019-02-08 14:59:05 +00006784 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00006786 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006787 }
Hanno Becker34106f62019-02-08 14:59:05 +00006788#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6789 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006790 ssl->session_negotiate->peer_cert = chain;
6791 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006792#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006795
Hanno Becker613d4902019-02-05 13:11:17 +00006796exit:
6797
Hanno Beckere4aeb762019-02-05 17:19:52 +00006798 if( ret == 0 )
6799 ssl->state++;
6800
6801#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6802 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6803 {
6804 ssl->handshake->ecrs_peer_cert = chain;
6805 chain = NULL;
6806 }
6807#endif
6808
6809 if( chain != NULL )
6810 {
6811 mbedtls_x509_crt_free( chain );
6812 mbedtls_free( chain );
6813 }
6814
Paul Bakker5121ce52009-01-03 21:22:43 +00006815 return( ret );
6816}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006817#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006820{
6821 int ret;
6822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006826 ssl->out_msglen = 1;
6827 ssl->out_msg[0] = 1;
6828
Paul Bakker5121ce52009-01-03 21:22:43 +00006829 ssl->state++;
6830
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006831 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006832 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834 return( ret );
6835 }
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006838
6839 return( 0 );
6840}
6841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006843{
6844 int ret;
6845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006847
Hanno Becker327c93b2018-08-15 13:56:18 +01006848 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006851 return( ret );
6852 }
6853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006857 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6858 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006860 }
6861
Hanno Beckere678eaa2018-08-21 14:57:46 +01006862 /* CCS records are only accepted if they have length 1 and content '1',
6863 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006864
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006865 /*
6866 * Switch to our negotiated transform and session parameters for inbound
6867 * data.
6868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006870 ssl->transform_in = ssl->transform_negotiate;
6871 ssl->session_in = ssl->session_negotiate;
6872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006874 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006877 ssl_dtls_replay_reset( ssl );
6878#endif
6879
6880 /* Increment epoch */
6881 if( ++ssl->in_epoch == 0 )
6882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006884 /* This is highly unlikely to happen for legitimate reasons, so
6885 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006887 }
6888 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006889 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006891#if defined(MBEDTLS_SSL_PROTO_TLS)
6892 {
6893 memset( ssl->in_ctr, 0, 8 );
6894 }
6895#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006896
Hanno Beckerf5970a02019-05-08 09:38:41 +01006897 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6900 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006905 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6906 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006908 }
6909 }
6910#endif
6911
Paul Bakker5121ce52009-01-03 21:22:43 +00006912 ssl->state++;
6913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006915
6916 return( 0 );
6917}
6918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6920 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006921{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006922 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6925 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6926 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006927 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006928 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6931#if defined(MBEDTLS_SHA512_C)
6932 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006933 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6934 else
6935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936#if defined(MBEDTLS_SHA256_C)
6937 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006938 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006939 else
6940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006944 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006945 }
Paul Bakker380da532012-04-18 16:10:25 +00006946}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006949{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6951 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006952 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6953 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6956#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006957 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006960 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006963}
6964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006966 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006967{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6969 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006970 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6971 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006972#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6974#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006975 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006978 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006979#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006981}
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6984 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6985static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006986 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006987{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006988 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6989 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006990}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006991#endif
Paul Bakker380da532012-04-18 16:10:25 +00006992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6994#if defined(MBEDTLS_SHA256_C)
6995static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006996 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006997{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006998 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006999}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007000#endif
Paul Bakker380da532012-04-18 16:10:25 +00007001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002#if defined(MBEDTLS_SHA512_C)
7003static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007004 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007005{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007006 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007007}
Paul Bakker769075d2012-11-24 11:26:46 +01007008#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007012static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007014{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007015 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007016 mbedtls_md5_context md5;
7017 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007018
Paul Bakker5121ce52009-01-03 21:22:43 +00007019 unsigned char padbuf[48];
7020 unsigned char md5sum[16];
7021 unsigned char sha1sum[20];
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007024 if( !session )
7025 session = ssl->session;
7026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007028
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007029 mbedtls_md5_init( &md5 );
7030 mbedtls_sha1_init( &sha1 );
7031
7032 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7033 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007034
7035 /*
7036 * SSLv3:
7037 * hash =
7038 * MD5( master + pad2 +
7039 * MD5( handshake + sender + master + pad1 ) )
7040 * + SHA1( master + pad2 +
7041 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007042 */
7043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007045 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7046 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007050 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7051 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007055 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007056
Paul Bakker1ef83d62012-04-11 12:09:53 +00007057 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007058
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007059 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7060 mbedtls_md5_update_ret( &md5, session->master, 48 );
7061 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7062 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007063
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007064 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7065 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7066 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7067 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007068
Paul Bakker1ef83d62012-04-11 12:09:53 +00007069 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007071 mbedtls_md5_starts_ret( &md5 );
7072 mbedtls_md5_update_ret( &md5, session->master, 48 );
7073 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7074 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7075 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007076
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007077 mbedtls_sha1_starts_ret( &sha1 );
7078 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7079 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7080 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7081 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007084
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007085 mbedtls_md5_free( &md5 );
7086 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007088 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7089 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7090 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007097static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007099{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007100 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007101 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007102 mbedtls_md5_context md5;
7103 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007104 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007107 if( !session )
7108 session = ssl->session;
7109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007111
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007112 mbedtls_md5_init( &md5 );
7113 mbedtls_sha1_init( &sha1 );
7114
7115 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7116 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007117
Paul Bakker1ef83d62012-04-11 12:09:53 +00007118 /*
7119 * TLSv1:
7120 * hash = PRF( master, finished_label,
7121 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7122 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007125 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7126 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007127#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007130 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7131 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007132#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007135 ? "client finished"
7136 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007137
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007138 mbedtls_md5_finish_ret( &md5, padbuf );
7139 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007140
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007141 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007142 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007145
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007146 mbedtls_md5_free( &md5 );
7147 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007148
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007149 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7156#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007157static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007159{
7160 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007161 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007162 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007163 unsigned char padbuf[32];
7164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007166 if( !session )
7167 session = ssl->session;
7168
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007169 mbedtls_sha256_init( &sha256 );
7170
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007172
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007173 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007174
7175 /*
7176 * TLSv1.2:
7177 * hash = PRF( master, finished_label,
7178 * Hash( handshake ) )[0.11]
7179 */
7180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181#if !defined(MBEDTLS_SHA256_ALT)
7182 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007183 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007184#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007187 ? "client finished"
7188 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007189
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007190 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007191
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007192 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007193 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007196
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007197 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007198
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007199 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007206static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007208{
7209 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007210 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007211 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007212 unsigned char padbuf[48];
7213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007215 if( !session )
7216 session = ssl->session;
7217
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007218 mbedtls_sha512_init( &sha512 );
7219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007221
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007222 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007223
7224 /*
7225 * TLSv1.2:
7226 * hash = PRF( master, finished_label,
7227 * Hash( handshake ) )[0.11]
7228 */
7229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007231 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7232 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007233#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007236 ? "client finished"
7237 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007238
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007239 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007240
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007241 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007242 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007245
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007246 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007247
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007248 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007251}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252#endif /* MBEDTLS_SHA512_C */
7253#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007258
7259 /*
7260 * Free our handshake params
7261 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007262 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007264 ssl->handshake = NULL;
7265
7266 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007267 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007268 */
7269 if( ssl->transform )
7270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 mbedtls_ssl_transform_free( ssl->transform );
7272 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007273 }
7274 ssl->transform = ssl->transform_negotiate;
7275 ssl->transform_negotiate = NULL;
7276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007278}
7279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007281{
7282 int resume = ssl->handshake->resume;
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286#if defined(MBEDTLS_SSL_RENEGOTIATION)
7287 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007290 ssl->renego_records_seen = 0;
7291 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007292#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007293
7294 /*
7295 * Free the previous session and switch in the current one
7296 */
Paul Bakker0a597072012-09-25 21:55:46 +00007297 if( ssl->session )
7298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007300 /* RFC 7366 3.1: keep the EtM state */
7301 ssl->session_negotiate->encrypt_then_mac =
7302 ssl->session->encrypt_then_mac;
7303#endif
7304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 mbedtls_ssl_session_free( ssl->session );
7306 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007307 }
7308 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007309 ssl->session_negotiate = NULL;
7310
Paul Bakker0a597072012-09-25 21:55:46 +00007311 /*
7312 * Add cache entry
7313 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007314 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007315 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007316 resume == 0 )
7317 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007318 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007320 }
Paul Bakker0a597072012-09-25 21:55:46 +00007321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007323 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007324 ssl->handshake->flight != NULL )
7325 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007326 /* Cancel handshake timer */
7327 ssl_set_timer( ssl, 0 );
7328
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007329 /* Keep last flight around in case we need to resend it:
7330 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007332 }
7333 else
7334#endif
7335 ssl_handshake_wrapup_free_hs_transform( ssl );
7336
Paul Bakker48916f92012-09-16 19:57:18 +00007337 ssl->state++;
7338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007340}
7341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007343{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007344 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007347
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007348 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007349
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007350 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007351
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007352 /*
7353 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7354 * may define some other value. Currently (early 2016), no defined
7355 * ciphersuite does this (and this is unlikely to change as activity has
7356 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007361 ssl->verify_data_len = hash_len;
7362 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007363#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007364
Paul Bakker5121ce52009-01-03 21:22:43 +00007365 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7367 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007368
7369 /*
7370 * In case of session resuming, invert the client and server
7371 * ChangeCipherSpec messages order.
7372 */
Paul Bakker0a597072012-09-25 21:55:46 +00007373 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007376 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007378#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007380 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007382#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007383 }
7384 else
7385 ssl->state++;
7386
Paul Bakker48916f92012-09-16 19:57:18 +00007387 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007388 * Switch to our negotiated transform and session parameters for outbound
7389 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007394 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007395 {
7396 unsigned char i;
7397
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007398 /* Remember current epoch settings for resending */
7399 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007400 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007401
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007402 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007403 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007404
7405 /* Increment epoch */
7406 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007407 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007408 break;
7409
7410 /* The loop goes to its end iff the counter is wrapping */
7411 if( i == 0 )
7412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7414 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007415 }
7416 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007417 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007419#if defined(MBEDTLS_SSL_PROTO_TLS)
7420 {
7421 memset( ssl->cur_out_ctr, 0, 8 );
7422 }
7423#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007424
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007425 ssl->transform_out = ssl->transform_negotiate;
7426 ssl->session_out = ssl->session_negotiate;
7427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7429 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7434 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007435 }
7436 }
7437#endif
7438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007440 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007442#endif
7443
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007444 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007445 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007447 return( ret );
7448 }
7449
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007450#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007451 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007452 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7453 {
7454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7455 return( ret );
7456 }
7457#endif
7458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007460
7461 return( 0 );
7462}
7463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007465#define SSL_MAX_HASH_LEN 36
7466#else
7467#define SSL_MAX_HASH_LEN 12
7468#endif
7469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007471{
Paul Bakker23986e52011-04-24 08:57:21 +00007472 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007473 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007474 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007477
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007478 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007479
Hanno Becker327c93b2018-08-15 13:56:18 +01007480 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007483 return( ret );
7484 }
7485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007489 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7490 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007492 }
7493
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007494 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SSL_PROTO_SSL3)
7496 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007497 hash_len = 36;
7498 else
7499#endif
7500 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7503 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007506 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7507 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007509 }
7510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007512 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007515 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7516 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007518 }
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007521 ssl->verify_data_len = hash_len;
7522 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007523#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007524
Paul Bakker0a597072012-09-25 21:55:46 +00007525 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007528 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007532 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007534#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007535 }
7536 else
7537 ssl->state++;
7538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007540 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007542#endif
7543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007545
7546 return( 0 );
7547}
7548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7554 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7555 mbedtls_md5_init( &handshake->fin_md5 );
7556 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007557 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7558 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007559#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7561#if defined(MBEDTLS_SHA256_C)
7562 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007563 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#if defined(MBEDTLS_SHA512_C)
7566 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007567 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007570
7571 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007572
7573#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7574 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7575 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7576#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#if defined(MBEDTLS_DHM_C)
7579 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007580#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#if defined(MBEDTLS_ECDH_C)
7582 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007583#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007584#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007585 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007586#if defined(MBEDTLS_SSL_CLI_C)
7587 handshake->ecjpake_cache = NULL;
7588 handshake->ecjpake_cache_len = 0;
7589#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007590#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007591
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007592#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007593 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007594#endif
7595
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007596#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7597 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7598#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007599
7600#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7601 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7602 mbedtls_pk_init( &handshake->peer_pubkey );
7603#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007604}
7605
Hanno Becker611a83b2018-01-03 14:27:32 +00007606void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7611 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007612
Hanno Becker92231322018-01-03 15:32:51 +00007613#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614 mbedtls_md_init( &transform->md_ctx_enc );
7615 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007616#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007617}
7618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007622}
7623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007625{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007626 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007627 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007629 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007631 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007632 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007633
7634 /*
7635 * Either the pointers are now NULL or cleared properly and can be freed.
7636 * Now allocate missing structures.
7637 */
7638 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007639 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007640 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007641 }
Paul Bakker48916f92012-09-16 19:57:18 +00007642
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007643 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007644 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007645 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007646 }
Paul Bakker48916f92012-09-16 19:57:18 +00007647
Paul Bakker82788fb2014-10-20 13:59:19 +02007648 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007649 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007650 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007651 }
Paul Bakker48916f92012-09-16 19:57:18 +00007652
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007653 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007654 if( ssl->handshake == NULL ||
7655 ssl->transform_negotiate == NULL ||
7656 ssl->session_negotiate == NULL )
7657 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 mbedtls_free( ssl->handshake );
7661 mbedtls_free( ssl->transform_negotiate );
7662 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007663
7664 ssl->handshake = NULL;
7665 ssl->transform_negotiate = NULL;
7666 ssl->session_negotiate = NULL;
7667
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007668 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007669 }
7670
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007671 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007673 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007674 ssl_handshake_params_init( ssl->handshake );
7675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007677 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007678 {
7679 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007680
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007681 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7682 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7683 else
7684 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007685
7686 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007687 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007688#endif
7689
Paul Bakker48916f92012-09-16 19:57:18 +00007690 return( 0 );
7691}
7692
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007693#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007694/* Dummy cookie callbacks for defaults */
7695static int ssl_cookie_write_dummy( void *ctx,
7696 unsigned char **p, unsigned char *end,
7697 const unsigned char *cli_id, size_t cli_id_len )
7698{
7699 ((void) ctx);
7700 ((void) p);
7701 ((void) end);
7702 ((void) cli_id);
7703 ((void) cli_id_len);
7704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007706}
7707
7708static int ssl_cookie_check_dummy( void *ctx,
7709 const unsigned char *cookie, size_t cookie_len,
7710 const unsigned char *cli_id, size_t cli_id_len )
7711{
7712 ((void) ctx);
7713 ((void) cookie);
7714 ((void) cookie_len);
7715 ((void) cli_id);
7716 ((void) cli_id_len);
7717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007719}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007720#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007721
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007722/* Once ssl->out_hdr as the address of the beginning of the
7723 * next outgoing record is set, deduce the other pointers.
7724 *
7725 * Note: For TLS, we save the implicit record sequence number
7726 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7727 * and the caller has to make sure there's space for this.
7728 */
7729
7730static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7731 mbedtls_ssl_transform *transform )
7732{
7733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007734 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007735 {
7736 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007737#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007738 ssl->out_cid = ssl->out_ctr + 8;
7739 ssl->out_len = ssl->out_cid;
7740 if( transform != NULL )
7741 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007742#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007743 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007744#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007745 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007746 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007747 MBEDTLS_SSL_TRANSPORT_ELSE
7748#endif /* MBEDTLS_SSL_PROTO_DTLS */
7749#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007750 {
7751 ssl->out_ctr = ssl->out_hdr - 8;
7752 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007753#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007754 ssl->out_cid = ssl->out_len;
7755#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007756 ssl->out_iv = ssl->out_hdr + 5;
7757 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007758#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007759
7760 /* Adjust out_msg to make space for explicit IV, if used. */
7761 if( transform != NULL &&
7762 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7763 {
7764 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7765 }
7766 else
7767 ssl->out_msg = ssl->out_iv;
7768}
7769
7770/* Once ssl->in_hdr as the address of the beginning of the
7771 * next incoming record is set, deduce the other pointers.
7772 *
7773 * Note: For TLS, we save the implicit record sequence number
7774 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7775 * and the caller has to make sure there's space for this.
7776 */
7777
Hanno Beckerf5970a02019-05-08 09:38:41 +01007778static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007779{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007780 /* This function sets the pointers to match the case
7781 * of unprotected TLS/DTLS records, with both ssl->in_iv
7782 * and ssl->in_msg pointing to the beginning of the record
7783 * content.
7784 *
7785 * When decrypting a protected record, ssl->in_msg
7786 * will be shifted to point to the beginning of the
7787 * record plaintext.
7788 */
7789
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007790#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007791 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007792 {
Hanno Becker70e79282019-05-03 14:34:53 +01007793 /* This sets the header pointers to match records
7794 * without CID. When we receive a record containing
7795 * a CID, the fields are shifted accordingly in
7796 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007797 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007798#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007799 ssl->in_cid = ssl->in_ctr + 8;
7800 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007801#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007802 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007803#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007804 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007805 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007806 MBEDTLS_SSL_TRANSPORT_ELSE
7807#endif /* MBEDTLS_SSL_PROTO_DTLS */
7808#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007809 {
7810 ssl->in_ctr = ssl->in_hdr - 8;
7811 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007812#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007813 ssl->in_cid = ssl->in_len;
7814#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007815 ssl->in_iv = ssl->in_hdr + 5;
7816 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007817#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007818
Hanno Beckerf5970a02019-05-08 09:38:41 +01007819 /* This will be adjusted at record decryption time. */
7820 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007821}
7822
Paul Bakker5121ce52009-01-03 21:22:43 +00007823/*
7824 * Initialize an SSL context
7825 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007826void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7827{
7828 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7829}
7830
7831/*
7832 * Setup an SSL context
7833 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007834
7835static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7836{
7837 /* Set the incoming and outgoing record pointers. */
7838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007839 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007840 {
7841 ssl->out_hdr = ssl->out_buf;
7842 ssl->in_hdr = ssl->in_buf;
7843 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007844 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007845#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007846#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007847 {
7848 ssl->out_hdr = ssl->out_buf + 8;
7849 ssl->in_hdr = ssl->in_buf + 8;
7850 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007851#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007852
7853 /* Derive other internal pointers. */
7854 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007855 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007856}
7857
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007858int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007859 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007860{
Paul Bakker48916f92012-09-16 19:57:18 +00007861 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007862
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007863 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007864
7865 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007866 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007867 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007868
7869 /* Set to NULL in case of an error condition */
7870 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007871
Angus Grattond8213d02016-05-25 20:56:48 +10007872 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7873 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007874 {
Angus Grattond8213d02016-05-25 20:56:48 +10007875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007876 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007877 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007878 }
7879
7880 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7881 if( ssl->out_buf == NULL )
7882 {
7883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007884 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007885 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007886 }
7887
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007888 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007889
Paul Bakker48916f92012-09-16 19:57:18 +00007890 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007891 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007892
7893 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007894
7895error:
7896 mbedtls_free( ssl->in_buf );
7897 mbedtls_free( ssl->out_buf );
7898
7899 ssl->conf = NULL;
7900
7901 ssl->in_buf = NULL;
7902 ssl->out_buf = NULL;
7903
7904 ssl->in_hdr = NULL;
7905 ssl->in_ctr = NULL;
7906 ssl->in_len = NULL;
7907 ssl->in_iv = NULL;
7908 ssl->in_msg = NULL;
7909
7910 ssl->out_hdr = NULL;
7911 ssl->out_ctr = NULL;
7912 ssl->out_len = NULL;
7913 ssl->out_iv = NULL;
7914 ssl->out_msg = NULL;
7915
k-stachowiak9f7798e2018-07-31 16:52:32 +02007916 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007917}
7918
7919/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007920 * Reset an initialized and used SSL context for re-use while retaining
7921 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007922 *
7923 * If partial is non-zero, keep data in the input buffer and client ID.
7924 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007925 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007926static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007927{
Paul Bakker48916f92012-09-16 19:57:18 +00007928 int ret;
7929
Hanno Becker7e772132018-08-10 12:38:21 +01007930#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7931 !defined(MBEDTLS_SSL_SRV_C)
7932 ((void) partial);
7933#endif
7934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007936
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007937 /* Cancel any possibly running timer */
7938 ssl_set_timer( ssl, 0 );
7939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940#if defined(MBEDTLS_SSL_RENEGOTIATION)
7941 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007942 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007943
7944 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7946 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007949
Paul Bakker7eb013f2011-10-06 12:37:39 +00007950 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007951 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007952
7953 ssl->in_msgtype = 0;
7954 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007956 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007957 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007960 ssl_dtls_replay_reset( ssl );
7961#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007962
7963 ssl->in_hslen = 0;
7964 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007965
7966 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007967
7968 ssl->out_msgtype = 0;
7969 ssl->out_msglen = 0;
7970 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7972 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007973 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007974#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007975
Hanno Becker19859472018-08-06 09:40:20 +01007976 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7977
Paul Bakker48916f92012-09-16 19:57:18 +00007978 ssl->transform_in = NULL;
7979 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007980
Hanno Becker78640902018-08-13 16:35:15 +01007981 ssl->session_in = NULL;
7982 ssl->session_out = NULL;
7983
Angus Grattond8213d02016-05-25 20:56:48 +10007984 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007985
7986#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007987 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007988#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7989 {
7990 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007991 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007992 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7995 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7998 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8001 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008002 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008003 }
8004#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008005
Paul Bakker48916f92012-09-16 19:57:18 +00008006 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008 mbedtls_ssl_transform_free( ssl->transform );
8009 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008010 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008011 }
Paul Bakker48916f92012-09-16 19:57:18 +00008012
Paul Bakkerc0463502013-02-14 11:19:38 +01008013 if( ssl->session )
8014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 mbedtls_ssl_session_free( ssl->session );
8016 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008017 ssl->session = NULL;
8018 }
8019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008021 ssl->alpn_chosen = NULL;
8022#endif
8023
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008024#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008025#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008026 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008027#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008028 {
8029 mbedtls_free( ssl->cli_id );
8030 ssl->cli_id = NULL;
8031 ssl->cli_id_len = 0;
8032 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008033#endif
8034
Paul Bakker48916f92012-09-16 19:57:18 +00008035 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8036 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008037
8038 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008039}
8040
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008041/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008042 * Reset an initialized and used SSL context for re-use while retaining
8043 * all application-set variables, function pointers and data.
8044 */
8045int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8046{
8047 return( ssl_session_reset_int( ssl, 0 ) );
8048}
8049
8050/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008051 * SSL set accessors
8052 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008053void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008054{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008055 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008056}
8057
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008058void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008060 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008061}
8062
Hanno Becker7f376f42019-06-12 16:20:48 +01008063#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8064 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008065void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008066{
Hanno Becker7f376f42019-06-12 16:20:48 +01008067 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008068}
Hanno Becker7f376f42019-06-12 16:20:48 +01008069#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008070
Hanno Beckerde671542019-06-12 16:30:46 +01008071#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8072 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8073void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8074 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008075{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008076 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008077}
Hanno Beckerde671542019-06-12 16:30:46 +01008078#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008081
Hanno Becker1841b0a2018-08-24 11:13:57 +01008082void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8083 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008084{
8085 ssl->disable_datagram_packing = !allow_packing;
8086}
8087
8088void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8089 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008090{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008091 conf->hs_timeout_min = min;
8092 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008093}
8094#endif
8095
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008096void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008097{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008098#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8099 conf->authmode = authmode;
8100#else
8101 ((void) conf);
8102 ((void) authmode);
8103#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008104}
8105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008107void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008108 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008109 void *p_vrfy )
8110{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008111 conf->f_vrfy = f_vrfy;
8112 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008113}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008115
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008116void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008117 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008118 void *p_rng )
8119{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008120 conf->f_rng = f_rng;
8121 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008122}
8123
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008124void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008125 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008126 void *p_dbg )
8127{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008128 conf->f_dbg = f_dbg;
8129 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008130}
8131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008133 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008134 mbedtls_ssl_send_t *f_send,
8135 mbedtls_ssl_recv_t *f_recv,
8136 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008137{
8138 ssl->p_bio = p_bio;
8139 ssl->f_send = f_send;
8140 ssl->f_recv = f_recv;
8141 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008142}
8143
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008144#if defined(MBEDTLS_SSL_PROTO_DTLS)
8145void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8146{
8147 ssl->mtu = mtu;
8148}
8149#endif
8150
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008151void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008152{
8153 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008154}
8155
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008156void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8157 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008158 mbedtls_ssl_set_timer_t *f_set_timer,
8159 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008160{
8161 ssl->p_timer = p_timer;
8162 ssl->f_set_timer = f_set_timer;
8163 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008164
8165 /* Make sure we start with no timer running */
8166 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008167}
8168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008170void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008171 void *p_cache,
8172 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8173 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008174{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008175 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008176 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008177 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181#if defined(MBEDTLS_SSL_CLI_C)
8182int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008183{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008184 int ret;
8185
8186 if( ssl == NULL ||
8187 session == NULL ||
8188 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008189 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008192 }
8193
Hanno Becker58fccf22019-02-06 14:30:46 +00008194 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8195 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008196 return( ret );
8197
Paul Bakker0a597072012-09-25 21:55:46 +00008198 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008199
8200 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008202#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008203
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008204void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008205 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008206{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008207 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8208 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8209 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8210 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008211}
8212
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008213void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008214 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008215 int major, int minor )
8216{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008218 return;
8219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008221 return;
8222
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008223 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008224}
8225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008227void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008228 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008229{
8230 conf->cert_profile = profile;
8231}
8232
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008233/* Append a new keycert entry to a (possibly empty) list */
8234static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8235 mbedtls_x509_crt *cert,
8236 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008237{
niisato8ee24222018-06-25 19:05:48 +09008238 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008239
niisato8ee24222018-06-25 19:05:48 +09008240 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8241 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008242 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008243
niisato8ee24222018-06-25 19:05:48 +09008244 new_cert->cert = cert;
8245 new_cert->key = key;
8246 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008247
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008248 /* Update head is the list was null, else add to the end */
8249 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008250 {
niisato8ee24222018-06-25 19:05:48 +09008251 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008252 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008253 else
8254 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008255 mbedtls_ssl_key_cert *cur = *head;
8256 while( cur->next != NULL )
8257 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008258 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008259 }
8260
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008261 return( 0 );
8262}
8263
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008264int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008265 mbedtls_x509_crt *own_cert,
8266 mbedtls_pk_context *pk_key )
8267{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008268 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008269}
8270
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008271void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008272 mbedtls_x509_crt *ca_chain,
8273 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008274{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008275 conf->ca_chain = ca_chain;
8276 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008279
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008280#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8281int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8282 mbedtls_x509_crt *own_cert,
8283 mbedtls_pk_context *pk_key )
8284{
8285 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8286 own_cert, pk_key ) );
8287}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008288
8289void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8290 mbedtls_x509_crt *ca_chain,
8291 mbedtls_x509_crl *ca_crl )
8292{
8293 ssl->handshake->sni_ca_chain = ca_chain;
8294 ssl->handshake->sni_ca_crl = ca_crl;
8295}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008296
8297void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8298 int authmode )
8299{
8300 ssl->handshake->sni_authmode = authmode;
8301}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008302#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8303
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008304#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008305/*
8306 * Set EC J-PAKE password for current handshake
8307 */
8308int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8309 const unsigned char *pw,
8310 size_t pw_len )
8311{
8312 mbedtls_ecjpake_role role;
8313
Janos Follath8eb64132016-06-03 15:40:57 +01008314 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8316
8317 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8318 role = MBEDTLS_ECJPAKE_SERVER;
8319 else
8320 role = MBEDTLS_ECJPAKE_CLIENT;
8321
8322 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8323 role,
8324 MBEDTLS_MD_SHA256,
8325 MBEDTLS_ECP_DP_SECP256R1,
8326 pw, pw_len ) );
8327}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008328#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008331int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008332 const unsigned char *psk, size_t psk_len,
8333 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008334{
Paul Bakker6db455e2013-09-18 17:29:31 +02008335 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008340
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008341 /* Identity len will be encoded on two bytes */
8342 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008343 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008344 {
8345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8346 }
8347
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008348 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008349 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008350 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008351
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008352 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008353 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008354 conf->psk_len = 0;
8355 }
8356 if( conf->psk_identity != NULL )
8357 {
8358 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008359 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008360 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008361 }
8362
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008363 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8364 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008365 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008366 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008367 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008368 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008369 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008370 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008371 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008372
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008373 conf->psk_len = psk_len;
8374 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008375
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008376 memcpy( conf->psk, psk, conf->psk_len );
8377 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008378
8379 return( 0 );
8380}
8381
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008382int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8383 const unsigned char *psk, size_t psk_len )
8384{
8385 if( psk == NULL || ssl->handshake == NULL )
8386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8387
8388 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8390
8391 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008392 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008393 mbedtls_platform_zeroize( ssl->handshake->psk,
8394 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008395 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008396 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008397 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008398
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008399 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008400 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008401
8402 ssl->handshake->psk_len = psk_len;
8403 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8404
8405 return( 0 );
8406}
8407
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008408void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008410 size_t),
8411 void *p_psk )
8412{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008413 conf->f_psk = f_psk;
8414 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008417
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008418#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008419
8420#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008421int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008422{
8423 int ret;
8424
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008425 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8426 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8427 {
8428 mbedtls_mpi_free( &conf->dhm_P );
8429 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008430 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008431 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008432
8433 return( 0 );
8434}
Hanno Becker470a8c42017-10-04 15:28:46 +01008435#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008436
Hanno Beckera90658f2017-10-04 15:29:08 +01008437int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8438 const unsigned char *dhm_P, size_t P_len,
8439 const unsigned char *dhm_G, size_t G_len )
8440{
8441 int ret;
8442
8443 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8444 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8445 {
8446 mbedtls_mpi_free( &conf->dhm_P );
8447 mbedtls_mpi_free( &conf->dhm_G );
8448 return( ret );
8449 }
8450
8451 return( 0 );
8452}
Paul Bakker5121ce52009-01-03 21:22:43 +00008453
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008454int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008455{
8456 int ret;
8457
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008458 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8459 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8460 {
8461 mbedtls_mpi_free( &conf->dhm_P );
8462 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008463 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008464 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008465
8466 return( 0 );
8467}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008468#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008469
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008470#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8471/*
8472 * Set the minimum length for Diffie-Hellman parameters
8473 */
8474void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8475 unsigned int bitlen )
8476{
8477 conf->dhm_min_bitlen = bitlen;
8478}
8479#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8480
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008481#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008482/*
8483 * Set allowed/preferred hashes for handshake signatures
8484 */
8485void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8486 const int *hashes )
8487{
8488 conf->sig_hashes = hashes;
8489}
Hanno Becker947194e2017-04-07 13:25:49 +01008490#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008491
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008492#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008493/*
8494 * Set the allowed elliptic curves
8495 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008496void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008497 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008499 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008500}
Hanno Becker947194e2017-04-07 13:25:49 +01008501#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008502
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008503#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008505{
Hanno Becker947194e2017-04-07 13:25:49 +01008506 /* Initialize to suppress unnecessary compiler warning */
8507 size_t hostname_len = 0;
8508
8509 /* Check if new hostname is valid before
8510 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008511 if( hostname != NULL )
8512 {
8513 hostname_len = strlen( hostname );
8514
8515 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8517 }
8518
8519 /* Now it's clear that we will overwrite the old hostname,
8520 * so we can free it safely */
8521
8522 if( ssl->hostname != NULL )
8523 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008524 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008525 mbedtls_free( ssl->hostname );
8526 }
8527
8528 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008529
Paul Bakker5121ce52009-01-03 21:22:43 +00008530 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008531 {
8532 ssl->hostname = NULL;
8533 }
8534 else
8535 {
8536 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008537 if( ssl->hostname == NULL )
8538 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008539
Hanno Becker947194e2017-04-07 13:25:49 +01008540 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008541
Hanno Becker947194e2017-04-07 13:25:49 +01008542 ssl->hostname[hostname_len] = '\0';
8543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008544
8545 return( 0 );
8546}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008547#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008548
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008549#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008550void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008552 const unsigned char *, size_t),
8553 void *p_sni )
8554{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008555 conf->f_sni = f_sni;
8556 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008561int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008562{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008563 size_t cur_len, tot_len;
8564 const char **p;
8565
8566 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008567 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8568 * MUST NOT be truncated."
8569 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008570 */
8571 tot_len = 0;
8572 for( p = protos; *p != NULL; p++ )
8573 {
8574 cur_len = strlen( *p );
8575 tot_len += cur_len;
8576
8577 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008579 }
8580
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008581 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008582
8583 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008584}
8585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008587{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008588 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008591
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008592void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008593{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008594 conf->max_major_ver = major;
8595 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008596}
8597
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008598void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008599{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008600 conf->min_major_ver = major;
8601 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008602}
8603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008605void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008606{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008607 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008608}
8609#endif
8610
Janos Follath088ce432017-04-10 12:42:31 +01008611#if defined(MBEDTLS_SSL_SRV_C)
8612void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8613 char cert_req_ca_list )
8614{
8615 conf->cert_req_ca_list = cert_req_ca_list;
8616}
8617#endif
8618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008622 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008623}
8624#endif
8625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01008627#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008628void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008629{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008630 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008631}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008632#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
8633#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008634void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008635 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008636{
8637 conf->enforce_extended_master_secret = ems_enf;
8638}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008639#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01008640#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008641
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008642#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008643void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008644{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008645 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008646}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008647#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008650int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008651{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008653 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008656 }
8657
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008658 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008659
8660 return( 0 );
8661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008662#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008665void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008666{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008667 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008672void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008673{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008674 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008675}
8676#endif
8677
Hanno Beckerb0b2b672019-06-12 16:58:10 +01008678#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008679void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008681 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008682}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01008683#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008686void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008687{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008688 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008689}
8690
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008691void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008692{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008693 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008694}
8695
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008696void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008697 const unsigned char period[8] )
8698{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008699 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008700}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008704#if defined(MBEDTLS_SSL_CLI_C)
8705void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008706{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008707 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008708}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008709#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008710
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008711#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008712void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8713 mbedtls_ssl_ticket_write_t *f_ticket_write,
8714 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8715 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008716{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008717 conf->f_ticket_write = f_ticket_write;
8718 conf->f_ticket_parse = f_ticket_parse;
8719 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008720}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008723
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008724#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8725void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8726 mbedtls_ssl_export_keys_t *f_export_keys,
8727 void *p_export_keys )
8728{
8729 conf->f_export_keys = f_export_keys;
8730 conf->p_export_keys = p_export_keys;
8731}
8732#endif
8733
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008734#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008735void mbedtls_ssl_conf_async_private_cb(
8736 mbedtls_ssl_config *conf,
8737 mbedtls_ssl_async_sign_t *f_async_sign,
8738 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8739 mbedtls_ssl_async_resume_t *f_async_resume,
8740 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008741 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008742{
8743 conf->f_async_sign_start = f_async_sign;
8744 conf->f_async_decrypt_start = f_async_decrypt;
8745 conf->f_async_resume = f_async_resume;
8746 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008747 conf->p_async_config_data = async_config_data;
8748}
8749
Gilles Peskine8f97af72018-04-26 11:46:10 +02008750void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8751{
8752 return( conf->p_async_config_data );
8753}
8754
Gilles Peskine1febfef2018-04-30 11:54:39 +02008755void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008756{
8757 if( ssl->handshake == NULL )
8758 return( NULL );
8759 else
8760 return( ssl->handshake->user_async_ctx );
8761}
8762
Gilles Peskine1febfef2018-04-30 11:54:39 +02008763void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008764 void *ctx )
8765{
8766 if( ssl->handshake != NULL )
8767 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008768}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008769#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008770
Paul Bakker5121ce52009-01-03 21:22:43 +00008771/*
8772 * SSL get accessors
8773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008775{
8776 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8777}
8778
Hanno Becker8b170a02017-10-10 11:51:19 +01008779int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8780{
8781 /*
8782 * Case A: We're currently holding back
8783 * a message for further processing.
8784 */
8785
8786 if( ssl->keep_current_message == 1 )
8787 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008788 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008789 return( 1 );
8790 }
8791
8792 /*
8793 * Case B: Further records are pending in the current datagram.
8794 */
8795
8796#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008797 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008798 ssl->in_left > ssl->next_record_offset )
8799 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008801 return( 1 );
8802 }
8803#endif /* MBEDTLS_SSL_PROTO_DTLS */
8804
8805 /*
8806 * Case C: A handshake message is being processed.
8807 */
8808
Hanno Becker8b170a02017-10-10 11:51:19 +01008809 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8810 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008812 return( 1 );
8813 }
8814
8815 /*
8816 * Case D: An application data message is being processed
8817 */
8818 if( ssl->in_offt != NULL )
8819 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008821 return( 1 );
8822 }
8823
8824 /*
8825 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008826 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008827 * we implement support for multiple alerts in single records.
8828 */
8829
8830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8831 return( 0 );
8832}
8833
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008834uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008835{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008836 if( ssl->session != NULL )
8837 return( ssl->session->verify_result );
8838
8839 if( ssl->session_negotiate != NULL )
8840 return( ssl->session_negotiate->verify_result );
8841
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008842 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008843}
8844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008846{
Paul Bakker926c8e42013-03-06 10:23:34 +01008847 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008848 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008851}
8852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008854{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008856 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008857 {
8858 switch( ssl->minor_ver )
8859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008861 return( "DTLSv1.0" );
8862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008864 return( "DTLSv1.2" );
8865
8866 default:
8867 return( "unknown (DTLS)" );
8868 }
8869 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008870 MBEDTLS_SSL_TRANSPORT_ELSE
8871#endif /* MBEDTLS_SSL_PROTO_DTLS */
8872#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008873 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008874 switch( ssl->minor_ver )
8875 {
8876 case MBEDTLS_SSL_MINOR_VERSION_0:
8877 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008878
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008879 case MBEDTLS_SSL_MINOR_VERSION_1:
8880 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008881
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008882 case MBEDTLS_SSL_MINOR_VERSION_2:
8883 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008884
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008885 case MBEDTLS_SSL_MINOR_VERSION_3:
8886 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008887
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008888 default:
8889 return( "unknown" );
8890 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008891 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008892#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008893}
8894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008896{
Hanno Becker3136ede2018-08-17 15:28:19 +01008897 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008899 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008900
Hanno Becker43395762019-05-03 14:46:38 +01008901 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8902
Hanno Becker78640902018-08-13 16:35:15 +01008903 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008904 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008906#if defined(MBEDTLS_ZLIB_SUPPORT)
8907 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8908 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008909#endif
8910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913 case MBEDTLS_MODE_GCM:
8914 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008915 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008916 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008917 transform_expansion = transform->minlen;
8918 break;
8919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008920 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008921
8922 block_size = mbedtls_cipher_get_block_size(
8923 &transform->cipher_ctx_enc );
8924
Hanno Becker3136ede2018-08-17 15:28:19 +01008925 /* Expansion due to the addition of the MAC. */
8926 transform_expansion += transform->maclen;
8927
8928 /* Expansion due to the addition of CBC padding;
8929 * Theoretically up to 256 bytes, but we never use
8930 * more than the block size of the underlying cipher. */
8931 transform_expansion += block_size;
8932
8933 /* For TLS 1.1 or higher, an explicit IV is added
8934 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008935#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8936 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008937 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008938#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008939
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008940 break;
8941
8942 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008945 }
8946
Hanno Beckera5a2b082019-05-15 14:03:01 +01008947#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008948 if( transform->out_cid_len != 0 )
8949 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008950#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008951
Hanno Becker43395762019-05-03 14:46:38 +01008952 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008953}
8954
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008955#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8956size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8957{
8958 size_t max_len;
8959
8960 /*
8961 * Assume mfl_code is correct since it was checked when set
8962 */
Angus Grattond8213d02016-05-25 20:56:48 +10008963 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008964
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008965 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008966 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008967 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008968 {
Angus Grattond8213d02016-05-25 20:56:48 +10008969 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008970 }
8971
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008972 /* During a handshake, use the value being negotiated */
8973 if( ssl->session_negotiate != NULL &&
8974 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8975 {
8976 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8977 }
8978
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008979 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008980}
8981#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8982
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008983#if defined(MBEDTLS_SSL_PROTO_DTLS)
8984static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8985{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008986 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8987 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8988 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8989 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8990 return ( 0 );
8991
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008992 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8993 return( ssl->mtu );
8994
8995 if( ssl->mtu == 0 )
8996 return( ssl->handshake->mtu );
8997
8998 return( ssl->mtu < ssl->handshake->mtu ?
8999 ssl->mtu : ssl->handshake->mtu );
9000}
9001#endif /* MBEDTLS_SSL_PROTO_DTLS */
9002
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009003int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9004{
9005 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9006
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009007#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9008 !defined(MBEDTLS_SSL_PROTO_DTLS)
9009 (void) ssl;
9010#endif
9011
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009012#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9013 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9014
9015 if( max_len > mfl )
9016 max_len = mfl;
9017#endif
9018
9019#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009020 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009021 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009022 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009023 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9024 const size_t overhead = (size_t) ret;
9025
9026 if( ret < 0 )
9027 return( ret );
9028
9029 if( mtu <= overhead )
9030 {
9031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9032 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9033 }
9034
9035 if( max_len > mtu - overhead )
9036 max_len = mtu - overhead;
9037 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009038#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009039
Hanno Becker0defedb2018-08-10 12:35:02 +01009040#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9041 !defined(MBEDTLS_SSL_PROTO_DTLS)
9042 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009043#endif
9044
9045 return( (int) max_len );
9046}
9047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048#if defined(MBEDTLS_X509_CRT_PARSE_C)
9049const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009050{
9051 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009052 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009053
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009054#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009055 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009056#else
9057 return( NULL );
9058#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009063int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9064 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009065{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009066 if( ssl == NULL ||
9067 dst == NULL ||
9068 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009069 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009072 }
9073
Hanno Becker58fccf22019-02-06 14:30:46 +00009074 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009077
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009078const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9079{
9080 if( ssl == NULL )
9081 return( NULL );
9082
9083 return( ssl->session );
9084}
9085
Paul Bakker5121ce52009-01-03 21:22:43 +00009086/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009087 * Define ticket header determining Mbed TLS version
9088 * and structure of the ticket.
9089 */
9090
Hanno Becker41527622019-05-16 12:50:45 +01009091/*
Hanno Becker26829e92019-05-28 14:30:45 +01009092 * Define bitflag determining compile-time settings influencing
9093 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009094 */
9095
Hanno Becker26829e92019-05-28 14:30:45 +01009096#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009097#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009098#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009099#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009100#endif /* MBEDTLS_HAVE_TIME */
9101
9102#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009103#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009104#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009105#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009106#endif /* MBEDTLS_X509_CRT_PARSE_C */
9107
9108#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009109#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009110#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009111#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009112#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9113
9114#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009115#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009116#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009117#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009118#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9119
9120#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009121#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009122#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009123#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009124#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9125
9126#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009127#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009128#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009129#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009130#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9131
Hanno Becker41527622019-05-16 12:50:45 +01009132#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9133#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9134#else
9135#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9136#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9137
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009138#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9139#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9140#else
9141#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9142#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9143
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009144#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9145#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9146#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9147#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9148#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9149#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9150#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009151#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009152
Hanno Becker26829e92019-05-28 14:30:45 +01009153#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009154 ( (uint16_t) ( \
9155 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9156 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9157 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9158 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9159 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9160 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009161 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9162 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009163
Hanno Becker557fe9f2019-05-16 12:41:07 +01009164static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009165 MBEDTLS_VERSION_MAJOR,
9166 MBEDTLS_VERSION_MINOR,
9167 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009168 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9169 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009170};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009171
9172/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009173 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009174 * (in the presentation language of TLS, RFC 8446 section 3)
9175 *
Hanno Becker26829e92019-05-28 14:30:45 +01009176 * opaque mbedtls_version[3]; // major, minor, patch
9177 * opaque session_format[2]; // version-specific 16-bit field determining
9178 * // the format of the remaining
9179 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009180 *
9181 * Note: When updating the format, remember to keep
9182 * these version+format bytes.
9183 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009184 * // In this version, `session_format` determines
9185 * // the setting of those compile-time
9186 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009187 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009188 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009189 * uint8 ciphersuite[2]; // defined by the standard
9190 * uint8 compression; // 0 or 1
9191 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009192 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009193 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009194 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009195 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9196 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9197 * case disabled: uint8_t peer_cert_digest_type;
9198 * opaque peer_cert_digest<0..2^8-1>;
9199 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009200 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009201 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009202 * uint8 mfl_code; // up to 255 according to standard
9203 * uint8 trunc_hmac; // 0 or 1
9204 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009205 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009206 * The order is the same as in the definition of the structure, except
9207 * verify_result is put before peer_cert so that all mandatory fields come
9208 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009209 */
9210int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9211 unsigned char *buf,
9212 size_t buf_len,
9213 size_t *olen )
9214{
9215 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009216 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009217#if defined(MBEDTLS_HAVE_TIME)
9218 uint64_t start;
9219#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009220#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009221#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009222 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009223#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009224#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009225
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009226 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009227 * Add version identifier
9228 */
9229
9230 used += sizeof( ssl_serialized_session_header );
9231
9232 if( used <= buf_len )
9233 {
9234 memcpy( p, ssl_serialized_session_header,
9235 sizeof( ssl_serialized_session_header ) );
9236 p += sizeof( ssl_serialized_session_header );
9237 }
9238
9239 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009240 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009241 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009242#if defined(MBEDTLS_HAVE_TIME)
9243 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009244
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009245 if( used <= buf_len )
9246 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009247 start = (uint64_t) session->start;
9248
9249 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9250 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9251 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9252 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9253 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9254 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9255 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9256 *p++ = (unsigned char)( ( start ) & 0xFF );
9257 }
9258#endif /* MBEDTLS_HAVE_TIME */
9259
9260 /*
9261 * Basic mandatory fields
9262 */
9263 used += 2 /* ciphersuite */
9264 + 1 /* compression */
9265 + 1 /* id_len */
9266 + sizeof( session->id )
9267 + sizeof( session->master )
9268 + 4; /* verify_result */
9269
9270 if( used <= buf_len )
9271 {
9272 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9273 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9274
9275 *p++ = (unsigned char)( session->compression & 0xFF );
9276
9277 *p++ = (unsigned char)( session->id_len & 0xFF );
9278 memcpy( p, session->id, 32 );
9279 p += 32;
9280
9281 memcpy( p, session->master, 48 );
9282 p += 48;
9283
9284 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9285 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9286 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9287 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009288 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009289
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009290 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009291 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009292 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009293#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009294#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009295 if( session->peer_cert == NULL )
9296 cert_len = 0;
9297 else
9298 cert_len = session->peer_cert->raw.len;
9299
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009300 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009301
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009302 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009303 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009304 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9305 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9306 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9307
9308 if( session->peer_cert != NULL )
9309 {
9310 memcpy( p, session->peer_cert->raw.p, cert_len );
9311 p += cert_len;
9312 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009313 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009314
Hanno Becker5882dd02019-06-06 16:25:57 +01009315#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009316 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009317 if( session->peer_cert_digest != NULL )
9318 {
9319 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9320 if( used <= buf_len )
9321 {
9322 *p++ = (unsigned char) session->peer_cert_digest_type;
9323 *p++ = (unsigned char) session->peer_cert_digest_len;
9324 memcpy( p, session->peer_cert_digest,
9325 session->peer_cert_digest_len );
9326 p += session->peer_cert_digest_len;
9327 }
9328 }
9329 else
9330 {
9331 used += 2;
9332 if( used <= buf_len )
9333 {
9334 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9335 *p++ = 0;
9336 }
9337 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009338#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009339#endif /* MBEDTLS_X509_CRT_PARSE_C */
9340
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009341 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009342 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009343 */
9344#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009345 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009346
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009347 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009348 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009349 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9350 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9351 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9352
9353 if( session->ticket != NULL )
9354 {
9355 memcpy( p, session->ticket, session->ticket_len );
9356 p += session->ticket_len;
9357 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009358
9359 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9360 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9361 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9362 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009363 }
9364#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9365
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009366 /*
9367 * Misc extension-related info
9368 */
9369#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9370 used += 1;
9371
9372 if( used <= buf_len )
9373 *p++ = session->mfl_code;
9374#endif
9375
9376#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9377 used += 1;
9378
9379 if( used <= buf_len )
9380 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9381#endif
9382
9383#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9384 used += 1;
9385
9386 if( used <= buf_len )
9387 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9388#endif
9389
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009390 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009391 *olen = used;
9392
9393 if( used > buf_len )
9394 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009395
9396 return( 0 );
9397}
9398
9399/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009400 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009401 *
9402 * This internal version is wrapped by a public function that cleans up in
9403 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009404 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009405static int ssl_session_load( mbedtls_ssl_session *session,
9406 const unsigned char *buf,
9407 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009408{
9409 const unsigned char *p = buf;
9410 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009411#if defined(MBEDTLS_HAVE_TIME)
9412 uint64_t start;
9413#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009414#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009415#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009416 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009417#endif
9418#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009419
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009420 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009421 * Check version identifier
9422 */
9423
9424 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009426
9427 if( memcmp( p, ssl_serialized_session_header,
9428 sizeof( ssl_serialized_session_header ) ) != 0 )
9429 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009430 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009431 }
9432 p += sizeof( ssl_serialized_session_header );
9433
9434 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009435 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009436 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009437#if defined(MBEDTLS_HAVE_TIME)
9438 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9440
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009441 start = ( (uint64_t) p[0] << 56 ) |
9442 ( (uint64_t) p[1] << 48 ) |
9443 ( (uint64_t) p[2] << 40 ) |
9444 ( (uint64_t) p[3] << 32 ) |
9445 ( (uint64_t) p[4] << 24 ) |
9446 ( (uint64_t) p[5] << 16 ) |
9447 ( (uint64_t) p[6] << 8 ) |
9448 ( (uint64_t) p[7] );
9449 p += 8;
9450
9451 session->start = (time_t) start;
9452#endif /* MBEDTLS_HAVE_TIME */
9453
9454 /*
9455 * Basic mandatory fields
9456 */
9457 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9459
9460 session->ciphersuite = ( p[0] << 8 ) | p[1];
9461 p += 2;
9462
9463 session->compression = *p++;
9464
9465 session->id_len = *p++;
9466 memcpy( session->id, p, 32 );
9467 p += 32;
9468
9469 memcpy( session->master, p, 48 );
9470 p += 48;
9471
9472 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9473 ( (uint32_t) p[1] << 16 ) |
9474 ( (uint32_t) p[2] << 8 ) |
9475 ( (uint32_t) p[3] );
9476 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009477
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009478 /* Immediately clear invalid pointer values that have been read, in case
9479 * we exit early before we replaced them with valid ones. */
9480#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009481#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009482 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009483#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009484 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009485#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009486#endif
9487#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9488 session->ticket = NULL;
9489#endif
9490
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009491 /*
9492 * Peer certificate
9493 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009494#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009495#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009496 if( 3 > (size_t)( end - p ) )
9497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9498
9499 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9500 p += 3;
9501
9502 if( cert_len == 0 )
9503 {
9504 session->peer_cert = NULL;
9505 }
9506 else
9507 {
9508 int ret;
9509
9510 if( cert_len > (size_t)( end - p ) )
9511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9512
9513 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9514
9515 if( session->peer_cert == NULL )
9516 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9517
9518 mbedtls_x509_crt_init( session->peer_cert );
9519
9520 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9521 p, cert_len ) ) != 0 )
9522 {
9523 mbedtls_x509_crt_free( session->peer_cert );
9524 mbedtls_free( session->peer_cert );
9525 session->peer_cert = NULL;
9526 return( ret );
9527 }
9528
9529 p += cert_len;
9530 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009531#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009532 /* Deserialize CRT digest from the end of the ticket. */
9533 if( 2 > (size_t)( end - p ) )
9534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9535
9536 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9537 session->peer_cert_digest_len = (size_t) *p++;
9538
9539 if( session->peer_cert_digest_len != 0 )
9540 {
Hanno Becker2326d202019-06-06 14:54:55 +01009541 const mbedtls_md_info_t *md_info =
9542 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9543 if( md_info == NULL )
9544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9545 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9547
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009548 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9550
9551 session->peer_cert_digest =
9552 mbedtls_calloc( 1, session->peer_cert_digest_len );
9553 if( session->peer_cert_digest == NULL )
9554 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9555
9556 memcpy( session->peer_cert_digest, p,
9557 session->peer_cert_digest_len );
9558 p += session->peer_cert_digest_len;
9559 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009560#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009561#endif /* MBEDTLS_X509_CRT_PARSE_C */
9562
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009563 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009564 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009565 */
9566#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9567 if( 3 > (size_t)( end - p ) )
9568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9569
9570 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9571 p += 3;
9572
9573 if( session->ticket_len != 0 )
9574 {
9575 if( session->ticket_len > (size_t)( end - p ) )
9576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9577
9578 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9579 if( session->ticket == NULL )
9580 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9581
9582 memcpy( session->ticket, p, session->ticket_len );
9583 p += session->ticket_len;
9584 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009585
9586 if( 4 > (size_t)( end - p ) )
9587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9588
9589 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9590 ( (uint32_t) p[1] << 16 ) |
9591 ( (uint32_t) p[2] << 8 ) |
9592 ( (uint32_t) p[3] );
9593 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009594#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9595
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009596 /*
9597 * Misc extension-related info
9598 */
9599#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9600 if( 1 > (size_t)( end - p ) )
9601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9602
9603 session->mfl_code = *p++;
9604#endif
9605
9606#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9607 if( 1 > (size_t)( end - p ) )
9608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9609
9610 session->trunc_hmac = *p++;
9611#endif
9612
9613#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9614 if( 1 > (size_t)( end - p ) )
9615 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9616
9617 session->encrypt_then_mac = *p++;
9618#endif
9619
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009620 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009621 if( p != end )
9622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9623
9624 return( 0 );
9625}
9626
9627/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009628 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009629 */
9630int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9631 const unsigned char *buf,
9632 size_t len )
9633{
9634 int ret = ssl_session_load( session, buf, len );
9635
9636 if( ret != 0 )
9637 mbedtls_ssl_session_free( session );
9638
9639 return( ret );
9640}
9641
9642/*
Paul Bakker1961b702013-01-25 14:49:24 +01009643 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009648
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009649 if( ssl == NULL || ssl->conf == NULL )
9650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009653 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009655#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009657 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009659#endif
9660
Paul Bakker1961b702013-01-25 14:49:24 +01009661 return( ret );
9662}
9663
9664/*
9665 * Perform the SSL handshake
9666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009668{
9669 int ret = 0;
9670
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009671 if( ssl == NULL || ssl->conf == NULL )
9672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009679
9680 if( ret != 0 )
9681 break;
9682 }
9683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009685
9686 return( ret );
9687}
9688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689#if defined(MBEDTLS_SSL_RENEGOTIATION)
9690#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009691/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009692 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009695{
9696 int ret;
9697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009699
9700 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9702 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009703
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009704 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009705 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009706 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009707 return( ret );
9708 }
9709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009711
9712 return( 0 );
9713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009715
9716/*
9717 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009718 * - any side: calling mbedtls_ssl_renegotiate(),
9719 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9720 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009721 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009722 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009726{
9727 int ret;
9728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009730
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009731 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9732 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009733
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009734 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9735 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009737 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009739 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009740 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009741 ssl->handshake->out_msg_seq = 1;
9742 else
9743 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009744 }
9745#endif
9746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9748 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009753 return( ret );
9754 }
9755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009757
9758 return( 0 );
9759}
9760
9761/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009762 * Renegotiate current connection on client,
9763 * or request renegotiation on server
9764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009768
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009769 if( ssl == NULL || ssl->conf == NULL )
9770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009773 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009774 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009780
9781 /* Did we already try/start sending HelloRequest? */
9782 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009784
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009785 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009786 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009790 /*
9791 * On client, either start the renegotiation process or,
9792 * if already in progress, continue the handshake
9793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009798
9799 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009802 return( ret );
9803 }
9804 }
9805 else
9806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009810 return( ret );
9811 }
9812 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009814
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009815 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009816}
9817
9818/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009819 * Check record counters and renegotiate if they're above the limit.
9820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009822{
Andres AG2196c7f2016-12-15 17:01:16 +00009823 size_t ep_len = ssl_ep_len( ssl );
9824 int in_ctr_cmp;
9825 int out_ctr_cmp;
9826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9828 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009829 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009830 {
9831 return( 0 );
9832 }
9833
Andres AG2196c7f2016-12-15 17:01:16 +00009834 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9835 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009836 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009837 ssl->conf->renego_period + ep_len, 8 - ep_len );
9838
9839 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009840 {
9841 return( 0 );
9842 }
9843
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009848
9849/*
9850 * Receive application data decrypted from the SSL layer
9851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009853{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009854 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009855 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009856
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009857 if( ssl == NULL || ssl->conf == NULL )
9858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009863 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009866 return( ret );
9867
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009868 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009870 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009871 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009872 return( ret );
9873 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009874 }
9875#endif
9876
Hanno Becker4a810fb2017-05-24 16:27:30 +01009877 /*
9878 * Check if renegotiation is necessary and/or handshake is
9879 * in process. If yes, perform/continue, and fall through
9880 * if an unexpected packet is received while the client
9881 * is waiting for the ServerHello.
9882 *
9883 * (There is no equivalent to the last condition on
9884 * the server-side as it is not treated as within
9885 * a handshake while waiting for the ClientHello
9886 * after a renegotiation request.)
9887 */
9888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009890 ret = ssl_check_ctr_renegotiate( ssl );
9891 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9892 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009895 return( ret );
9896 }
9897#endif
9898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009902 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9903 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009906 return( ret );
9907 }
9908 }
9909
Hanno Beckere41158b2017-10-23 13:30:32 +01009910 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009911 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009912 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009913 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009914 if( ssl->f_get_timer != NULL &&
9915 ssl->f_get_timer( ssl->p_timer ) == -1 )
9916 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009917 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009918 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009919
Hanno Becker327c93b2018-08-15 13:56:18 +01009920 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009921 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009922 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9923 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009924
Hanno Becker4a810fb2017-05-24 16:27:30 +01009925 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9926 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009927 }
9928
9929 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009931 {
9932 /*
9933 * OpenSSL sends empty messages to randomize the IV
9934 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009935 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009938 return( 0 );
9939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009941 return( ret );
9942 }
9943 }
9944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009948
Hanno Becker4a810fb2017-05-24 16:27:30 +01009949 /*
9950 * - For client-side, expect SERVER_HELLO_REQUEST.
9951 * - For server-side, expect CLIENT_HELLO.
9952 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9953 */
9954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009956 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009958 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009961
9962 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009964 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009965 {
9966 continue;
9967 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009968 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009969#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009970#if defined(MBEDTLS_SSL_PROTO_TLS)
9971 {
9972 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9973 }
9974#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009975 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009976#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009977
Hanno Becker4a810fb2017-05-24 16:27:30 +01009978#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009979 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009983
9984 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009986 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009987 {
9988 continue;
9989 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009990 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009991#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009992#if defined(MBEDTLS_SSL_PROTO_TLS)
9993 {
9994 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9995 }
9996#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009997 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009998#endif /* MBEDTLS_SSL_SRV_C */
9999
Hanno Becker21df7f92017-10-17 11:03:26 +010010000#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010001 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010002 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10003 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010004 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010005 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10006 {
10007 /*
10008 * Accept renegotiation request
10009 */
Paul Bakker48916f92012-09-16 19:57:18 +000010010
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010011 /* DTLS clients need to know renego is server-initiated */
10012#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010013 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010014 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10015 {
10016 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10017 }
10018#endif
10019 ret = ssl_start_renegotiation( ssl );
10020 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10021 ret != 0 )
10022 {
10023 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10024 return( ret );
10025 }
10026 }
10027 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010028#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010029 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010030 /*
10031 * Refuse renegotiation
10032 */
10033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036#if defined(MBEDTLS_SSL_PROTO_SSL3)
10037 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010038 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010039 /* SSLv3 does not have a "no_renegotiation" warning, so
10040 we send a fatal alert and abort the connection. */
10041 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10042 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10043 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010044 }
10045 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10047#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10048 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10049 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10052 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10053 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010054 {
10055 return( ret );
10056 }
Paul Bakker48916f92012-09-16 19:57:18 +000010057 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10060 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010064 }
Paul Bakker48916f92012-09-16 19:57:18 +000010065 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010066
Hanno Becker90333da2017-10-10 11:27:13 +010010067 /* At this point, we don't know whether the renegotiation has been
10068 * completed or not. The cases to consider are the following:
10069 * 1) The renegotiation is complete. In this case, no new record
10070 * has been read yet.
10071 * 2) The renegotiation is incomplete because the client received
10072 * an application data record while awaiting the ServerHello.
10073 * 3) The renegotiation is incomplete because the client received
10074 * a non-handshake, non-application data message while awaiting
10075 * the ServerHello.
10076 * In each of these case, looping will be the proper action:
10077 * - For 1), the next iteration will read a new record and check
10078 * if it's application data.
10079 * - For 2), the loop condition isn't satisfied as application data
10080 * is present, hence continue is the same as break
10081 * - For 3), the loop condition is satisfied and read_record
10082 * will re-deliver the message that was held back by the client
10083 * when expecting the ServerHello.
10084 */
10085 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010086 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010087#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010089 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010090 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010091 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010092 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010095 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010097 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010098 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010099 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10103 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010106 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010107 }
10108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10112 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010113 }
10114
10115 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010116
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010117 /* We're going to return something now, cancel timer,
10118 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010120 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010121
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010122#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010123 /* If we requested renego but received AppData, resend HelloRequest.
10124 * Do it now, after setting in_offt, to avoid taking this branch
10125 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010127 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010129 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010130 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010132 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010133 return( ret );
10134 }
10135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010136#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010137#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010138 }
10139
10140 n = ( len < ssl->in_msglen )
10141 ? len : ssl->in_msglen;
10142
10143 memcpy( buf, ssl->in_offt, n );
10144 ssl->in_msglen -= n;
10145
10146 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010147 {
10148 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010149 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010150 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010151 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010152 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010153 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010154 /* more data available */
10155 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010156 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010159
Paul Bakker23986e52011-04-24 08:57:21 +000010160 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010161}
10162
10163/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010164 * Send application data to be encrypted by the SSL layer, taking care of max
10165 * fragment length and buffer size.
10166 *
10167 * According to RFC 5246 Section 6.2.1:
10168 *
10169 * Zero-length fragments of Application data MAY be sent as they are
10170 * potentially useful as a traffic analysis countermeasure.
10171 *
10172 * Therefore, it is possible that the input message length is 0 and the
10173 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010174 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010175static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010176 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010177{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010178 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10179 const size_t max_len = (size_t) ret;
10180
10181 if( ret < 0 )
10182 {
10183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10184 return( ret );
10185 }
10186
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010187 if( len > max_len )
10188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010189#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010190 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010193 "maximum fragment length: %d > %d",
10194 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010196 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010197 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010198#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010199#if defined(MBEDTLS_SSL_PROTO_TLS)
10200 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010201 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010202 }
10203#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010204 }
Paul Bakker887bd502011-06-08 13:10:54 +000010205
Paul Bakker5121ce52009-01-03 21:22:43 +000010206 if( ssl->out_left != 0 )
10207 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010208 /*
10209 * The user has previously tried to send the data and
10210 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10211 * written. In this case, we expect the high-level write function
10212 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010217 return( ret );
10218 }
10219 }
Paul Bakker887bd502011-06-08 13:10:54 +000010220 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010221 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010222 /*
10223 * The user is trying to send a message the first time, so we need to
10224 * copy the data into the internal buffers and setup the data structure
10225 * to keep track of partial writes
10226 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010227 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010229 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010230
Hanno Becker67bc7c32018-08-06 11:33:50 +010010231 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010234 return( ret );
10235 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010236 }
10237
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010238 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010239}
10240
10241/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010242 * Write application data, doing 1/n-1 splitting if necessary.
10243 *
10244 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010245 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010246 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010248#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010249static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010250 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010251{
10252 int ret;
10253
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010254 if( ssl->conf->cbc_record_splitting ==
10255 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010256 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10258 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10259 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010260 {
10261 return( ssl_write_real( ssl, buf, len ) );
10262 }
10263
10264 if( ssl->split_done == 0 )
10265 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010266 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010267 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010268 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010269 }
10270
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010271 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10272 return( ret );
10273 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010274
10275 return( ret + 1 );
10276}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010278
10279/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010280 * Write application data (public-facing wrapper)
10281 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010282int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010283{
10284 int ret;
10285
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010287
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010288 if( ssl == NULL || ssl->conf == NULL )
10289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10290
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010291#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010292 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10293 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010294 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010295 return( ret );
10296 }
10297#endif
10298
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010299 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010300 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010301 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010302 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010304 return( ret );
10305 }
10306 }
10307
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010308#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010309 ret = ssl_write_split( ssl, buf, len );
10310#else
10311 ret = ssl_write_real( ssl, buf, len );
10312#endif
10313
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010315
10316 return( ret );
10317}
10318
10319/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010320 * Notify the peer that the connection is being closed
10321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010323{
10324 int ret;
10325
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010326 if( ssl == NULL || ssl->conf == NULL )
10327 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010330
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010331 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10337 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10338 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010341 return( ret );
10342 }
10343 }
10344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010346
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010347 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010348}
10349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010350void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010351{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010352 if( transform == NULL )
10353 return;
10354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010356 deflateEnd( &transform->ctx_deflate );
10357 inflateEnd( &transform->ctx_inflate );
10358#endif
10359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10361 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010362
Hanno Becker92231322018-01-03 15:32:51 +000010363#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010364 mbedtls_md_free( &transform->md_ctx_enc );
10365 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010366#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010367
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010368 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010369}
10370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371#if defined(MBEDTLS_X509_CRT_PARSE_C)
10372static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010375
10376 while( cur != NULL )
10377 {
10378 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010380 cur = next;
10381 }
10382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010383#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010384
Hanno Becker0271f962018-08-16 13:23:47 +010010385#if defined(MBEDTLS_SSL_PROTO_DTLS)
10386
10387static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10388{
10389 unsigned offset;
10390 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10391
10392 if( hs == NULL )
10393 return;
10394
Hanno Becker283f5ef2018-08-24 09:34:47 +010010395 ssl_free_buffered_record( ssl );
10396
Hanno Becker0271f962018-08-16 13:23:47 +010010397 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010398 ssl_buffering_free_slot( ssl, offset );
10399}
10400
10401static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10402 uint8_t slot )
10403{
10404 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10405 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010406
10407 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10408 return;
10409
Hanno Beckere605b192018-08-21 15:59:07 +010010410 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010411 {
Hanno Beckere605b192018-08-21 15:59:07 +010010412 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010413 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010414 mbedtls_free( hs_buf->data );
10415 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010416 }
10417}
10418
10419#endif /* MBEDTLS_SSL_PROTO_DTLS */
10420
Gilles Peskine9b562d52018-04-25 20:32:43 +020010421void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010422{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010423 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10424
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010425 if( handshake == NULL )
10426 return;
10427
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010428#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10429 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10430 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010431 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010432 handshake->async_in_progress = 0;
10433 }
10434#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10435
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010436#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10437 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10438 mbedtls_md5_free( &handshake->fin_md5 );
10439 mbedtls_sha1_free( &handshake->fin_sha1 );
10440#endif
10441#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10442#if defined(MBEDTLS_SHA256_C)
10443 mbedtls_sha256_free( &handshake->fin_sha256 );
10444#endif
10445#if defined(MBEDTLS_SHA512_C)
10446 mbedtls_sha512_free( &handshake->fin_sha512 );
10447#endif
10448#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450#if defined(MBEDTLS_DHM_C)
10451 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010452#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010453#if defined(MBEDTLS_ECDH_C)
10454 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010455#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010456#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010457 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010458#if defined(MBEDTLS_SSL_CLI_C)
10459 mbedtls_free( handshake->ecjpake_cache );
10460 handshake->ecjpake_cache = NULL;
10461 handshake->ecjpake_cache_len = 0;
10462#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010463#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010464
Janos Follath4ae5c292016-02-10 11:27:43 +000010465#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10466 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010467 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010469#endif
10470
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010471#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10472 if( handshake->psk != NULL )
10473 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010474 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010475 mbedtls_free( handshake->psk );
10476 }
10477#endif
10478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10480 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010481 /*
10482 * Free only the linked list wrapper, not the keys themselves
10483 * since the belong to the SNI callback
10484 */
10485 if( handshake->sni_key_cert != NULL )
10486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010488
10489 while( cur != NULL )
10490 {
10491 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010493 cur = next;
10494 }
10495 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010497
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010498#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010499 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010500 if( handshake->ecrs_peer_cert != NULL )
10501 {
10502 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10503 mbedtls_free( handshake->ecrs_peer_cert );
10504 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010505#endif
10506
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010507#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10508 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10509 mbedtls_pk_free( &handshake->peer_pubkey );
10510#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010512#if defined(MBEDTLS_SSL_PROTO_DTLS)
10513 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010514 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010515 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010516#endif
10517
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010518 mbedtls_platform_zeroize( handshake,
10519 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010520}
10521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010522void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010523{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010524 if( session == NULL )
10525 return;
10526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010527#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010528 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010529#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010530
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010531#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010532 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010533#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010534
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010535 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010536}
10537
Paul Bakker5121ce52009-01-03 21:22:43 +000010538/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010539 * Serialize a full SSL context
10540 */
10541int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10542 unsigned char *buf,
10543 size_t buf_len,
10544 size_t *olen )
10545{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010546 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010547 (void) ssl;
10548
10549 if( buf != NULL )
10550 memset( buf, 0, buf_len );
10551
10552 *olen = 0;
10553
10554 return( 0 );
10555}
10556
10557/*
10558 * Deserialize a full SSL context
10559 */
10560int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10561 const unsigned char *buf,
10562 size_t len )
10563{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010564 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010565 (void) ssl;
10566 (void) buf;
10567 (void) len;
10568
10569 return( 0 );
10570}
10571
10572/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010573 * Free an SSL context
10574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010575void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010576{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010577 if( ssl == NULL )
10578 return;
10579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010581
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010582 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010583 {
Angus Grattond8213d02016-05-25 20:56:48 +100010584 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010586 }
10587
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010588 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010589 {
Angus Grattond8213d02016-05-25 20:56:48 +100010590 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010592 }
10593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010595 if( ssl->compress_buf != NULL )
10596 {
Angus Grattond8213d02016-05-25 20:56:48 +100010597 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010599 }
10600#endif
10601
Paul Bakker48916f92012-09-16 19:57:18 +000010602 if( ssl->transform )
10603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604 mbedtls_ssl_transform_free( ssl->transform );
10605 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010606 }
10607
10608 if( ssl->handshake )
10609 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010610 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10612 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614 mbedtls_free( ssl->handshake );
10615 mbedtls_free( ssl->transform_negotiate );
10616 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010617 }
10618
Paul Bakkerc0463502013-02-14 11:19:38 +010010619 if( ssl->session )
10620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621 mbedtls_ssl_session_free( ssl->session );
10622 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010623 }
10624
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010625#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010626 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010627 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010628 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010630 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010631#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10634 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10637 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010638 }
10639#endif
10640
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010641#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010642 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010643#endif
10644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010646
Paul Bakker86f04f42013-02-14 11:20:09 +010010647 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010648 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010649}
10650
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010651/*
10652 * Initialze mbedtls_ssl_config
10653 */
10654void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10655{
10656 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010657
10658#if !defined(MBEDTLS_SSL_PROTO_TLS)
10659 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10660#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010661}
10662
Simon Butcherc97b6972015-12-27 23:48:17 +000010663#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010664static int ssl_preset_default_hashes[] = {
10665#if defined(MBEDTLS_SHA512_C)
10666 MBEDTLS_MD_SHA512,
10667 MBEDTLS_MD_SHA384,
10668#endif
10669#if defined(MBEDTLS_SHA256_C)
10670 MBEDTLS_MD_SHA256,
10671 MBEDTLS_MD_SHA224,
10672#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010673#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010674 MBEDTLS_MD_SHA1,
10675#endif
10676 MBEDTLS_MD_NONE
10677};
Simon Butcherc97b6972015-12-27 23:48:17 +000010678#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010679
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010680static int ssl_preset_suiteb_ciphersuites[] = {
10681 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10682 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10683 0
10684};
10685
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010686#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010687static int ssl_preset_suiteb_hashes[] = {
10688 MBEDTLS_MD_SHA256,
10689 MBEDTLS_MD_SHA384,
10690 MBEDTLS_MD_NONE
10691};
10692#endif
10693
10694#if defined(MBEDTLS_ECP_C)
10695static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10696 MBEDTLS_ECP_DP_SECP256R1,
10697 MBEDTLS_ECP_DP_SECP384R1,
10698 MBEDTLS_ECP_DP_NONE
10699};
10700#endif
10701
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010702/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010703 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010704 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010705int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010706 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010707{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010708#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010709 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010710#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010711
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010712 /* Use the functions here so that they are covered in tests,
10713 * but otherwise access member directly for efficiency */
10714 mbedtls_ssl_conf_endpoint( conf, endpoint );
10715 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010716
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010717 /*
10718 * Things that are common to all presets
10719 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010720#if defined(MBEDTLS_SSL_CLI_C)
10721 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10722 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010010723#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010724 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010010725#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010726#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10727 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10728#endif
10729 }
10730#endif
10731
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010732#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010733 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010734#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010735
10736#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10737 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10738#endif
10739
10740#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010010741#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010742 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010743#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
10744#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030010745 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010746 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010747#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010748#endif
10749
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010750#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10751 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10752#endif
10753
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010754#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010755 conf->f_cookie_write = ssl_cookie_write_dummy;
10756 conf->f_cookie_check = ssl_cookie_check_dummy;
10757#endif
10758
Hanno Becker7f376f42019-06-12 16:20:48 +010010759#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
10760 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010761 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10762#endif
10763
Janos Follath088ce432017-04-10 12:42:31 +010010764#if defined(MBEDTLS_SSL_SRV_C)
10765 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10766#endif
10767
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010768#if defined(MBEDTLS_SSL_PROTO_DTLS)
10769 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10770 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10771#endif
10772
10773#if defined(MBEDTLS_SSL_RENEGOTIATION)
10774 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010775 memset( conf->renego_period, 0x00, 2 );
10776 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010777#endif
10778
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010779#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10780 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10781 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010782 const unsigned char dhm_p[] =
10783 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10784 const unsigned char dhm_g[] =
10785 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10786
Hanno Beckera90658f2017-10-04 15:29:08 +010010787 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10788 dhm_p, sizeof( dhm_p ),
10789 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010790 {
10791 return( ret );
10792 }
10793 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010794#endif
10795
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010796 /*
10797 * Preset-specific defaults
10798 */
10799 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010800 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010801 /*
10802 * NSA Suite B
10803 */
10804 case MBEDTLS_SSL_PRESET_SUITEB:
10805 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10806 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10807 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10808 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10809
10810 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10811 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10812 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10813 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10814 ssl_preset_suiteb_ciphersuites;
10815
10816#if defined(MBEDTLS_X509_CRT_PARSE_C)
10817 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010818#endif
10819
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010820#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010821 conf->sig_hashes = ssl_preset_suiteb_hashes;
10822#endif
10823
10824#if defined(MBEDTLS_ECP_C)
10825 conf->curve_list = ssl_preset_suiteb_curves;
10826#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010827 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010828
10829 /*
10830 * Default
10831 */
10832 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010833 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10834 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10835 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10836 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10837 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10838 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10839 MBEDTLS_SSL_MIN_MINOR_VERSION :
10840 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010841 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10842 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10843
10844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010845 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010846 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10847#endif
10848
10849 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10850 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10851 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10852 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10853 mbedtls_ssl_list_ciphersuites();
10854
10855#if defined(MBEDTLS_X509_CRT_PARSE_C)
10856 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10857#endif
10858
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010859#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010860 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010861#endif
10862
10863#if defined(MBEDTLS_ECP_C)
10864 conf->curve_list = mbedtls_ecp_grp_id_list();
10865#endif
10866
10867#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10868 conf->dhm_min_bitlen = 1024;
10869#endif
10870 }
10871
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010872 return( 0 );
10873}
10874
10875/*
10876 * Free mbedtls_ssl_config
10877 */
10878void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10879{
10880#if defined(MBEDTLS_DHM_C)
10881 mbedtls_mpi_free( &conf->dhm_P );
10882 mbedtls_mpi_free( &conf->dhm_G );
10883#endif
10884
10885#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10886 if( conf->psk != NULL )
10887 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010888 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010889 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010890 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010891 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010892 }
10893
10894 if( conf->psk_identity != NULL )
10895 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010896 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010897 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010898 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010899 conf->psk_identity_len = 0;
10900 }
10901#endif
10902
10903#if defined(MBEDTLS_X509_CRT_PARSE_C)
10904 ssl_key_cert_free( conf->key_cert );
10905#endif
10906
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010907 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010908}
10909
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010910#if defined(MBEDTLS_PK_C) && \
10911 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010912/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010916{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917#if defined(MBEDTLS_RSA_C)
10918 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10919 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010920#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921#if defined(MBEDTLS_ECDSA_C)
10922 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10923 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010924#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010926}
10927
Hanno Becker7e5437a2017-04-28 17:15:26 +010010928unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10929{
10930 switch( type ) {
10931 case MBEDTLS_PK_RSA:
10932 return( MBEDTLS_SSL_SIG_RSA );
10933 case MBEDTLS_PK_ECDSA:
10934 case MBEDTLS_PK_ECKEY:
10935 return( MBEDTLS_SSL_SIG_ECDSA );
10936 default:
10937 return( MBEDTLS_SSL_SIG_ANON );
10938 }
10939}
10940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010941mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010942{
10943 switch( sig )
10944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945#if defined(MBEDTLS_RSA_C)
10946 case MBEDTLS_SSL_SIG_RSA:
10947 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010948#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949#if defined(MBEDTLS_ECDSA_C)
10950 case MBEDTLS_SSL_SIG_ECDSA:
10951 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010952#endif
10953 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010954 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010955 }
10956}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010957#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010958
Hanno Becker7e5437a2017-04-28 17:15:26 +010010959#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10960 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10961
10962/* Find an entry in a signature-hash set matching a given hash algorithm. */
10963mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10964 mbedtls_pk_type_t sig_alg )
10965{
10966 switch( sig_alg )
10967 {
10968 case MBEDTLS_PK_RSA:
10969 return( set->rsa );
10970 case MBEDTLS_PK_ECDSA:
10971 return( set->ecdsa );
10972 default:
10973 return( MBEDTLS_MD_NONE );
10974 }
10975}
10976
10977/* Add a signature-hash-pair to a signature-hash set */
10978void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10979 mbedtls_pk_type_t sig_alg,
10980 mbedtls_md_type_t md_alg )
10981{
10982 switch( sig_alg )
10983 {
10984 case MBEDTLS_PK_RSA:
10985 if( set->rsa == MBEDTLS_MD_NONE )
10986 set->rsa = md_alg;
10987 break;
10988
10989 case MBEDTLS_PK_ECDSA:
10990 if( set->ecdsa == MBEDTLS_MD_NONE )
10991 set->ecdsa = md_alg;
10992 break;
10993
10994 default:
10995 break;
10996 }
10997}
10998
10999/* Allow exactly one hash algorithm for each signature. */
11000void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11001 mbedtls_md_type_t md_alg )
11002{
11003 set->rsa = md_alg;
11004 set->ecdsa = md_alg;
11005}
11006
11007#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11008 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11009
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011010/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011011 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011013mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011014{
11015 switch( hash )
11016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017#if defined(MBEDTLS_MD5_C)
11018 case MBEDTLS_SSL_HASH_MD5:
11019 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011020#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011021#if defined(MBEDTLS_SHA1_C)
11022 case MBEDTLS_SSL_HASH_SHA1:
11023 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025#if defined(MBEDTLS_SHA256_C)
11026 case MBEDTLS_SSL_HASH_SHA224:
11027 return( MBEDTLS_MD_SHA224 );
11028 case MBEDTLS_SSL_HASH_SHA256:
11029 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031#if defined(MBEDTLS_SHA512_C)
11032 case MBEDTLS_SSL_HASH_SHA384:
11033 return( MBEDTLS_MD_SHA384 );
11034 case MBEDTLS_SSL_HASH_SHA512:
11035 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011036#endif
11037 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011039 }
11040}
11041
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011042/*
11043 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11044 */
11045unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11046{
11047 switch( md )
11048 {
11049#if defined(MBEDTLS_MD5_C)
11050 case MBEDTLS_MD_MD5:
11051 return( MBEDTLS_SSL_HASH_MD5 );
11052#endif
11053#if defined(MBEDTLS_SHA1_C)
11054 case MBEDTLS_MD_SHA1:
11055 return( MBEDTLS_SSL_HASH_SHA1 );
11056#endif
11057#if defined(MBEDTLS_SHA256_C)
11058 case MBEDTLS_MD_SHA224:
11059 return( MBEDTLS_SSL_HASH_SHA224 );
11060 case MBEDTLS_MD_SHA256:
11061 return( MBEDTLS_SSL_HASH_SHA256 );
11062#endif
11063#if defined(MBEDTLS_SHA512_C)
11064 case MBEDTLS_MD_SHA384:
11065 return( MBEDTLS_SSL_HASH_SHA384 );
11066 case MBEDTLS_MD_SHA512:
11067 return( MBEDTLS_SSL_HASH_SHA512 );
11068#endif
11069 default:
11070 return( MBEDTLS_SSL_HASH_NONE );
11071 }
11072}
11073
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011074#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011075/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011076 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011077 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011078 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011079int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011081 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011082
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011083 if( ssl->conf->curve_list == NULL )
11084 return( -1 );
11085
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011086 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011087 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011088 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011089
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011090 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011091}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011092#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011093
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011094#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011095/*
11096 * Check if a hash proposed by the peer is in our list.
11097 * Return 0 if we're willing to use it, -1 otherwise.
11098 */
11099int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11100 mbedtls_md_type_t md )
11101{
11102 const int *cur;
11103
11104 if( ssl->conf->sig_hashes == NULL )
11105 return( -1 );
11106
11107 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11108 if( *cur == (int) md )
11109 return( 0 );
11110
11111 return( -1 );
11112}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011113#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011115#if defined(MBEDTLS_X509_CRT_PARSE_C)
11116int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11117 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011118 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011119 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011120{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011121 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011122#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011123 int usage = 0;
11124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011125#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011126 const char *ext_oid;
11127 size_t ext_len;
11128#endif
11129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011130#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11131 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011132 ((void) cert);
11133 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011134 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011135#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011137#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11138 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011139 {
11140 /* Server part of the key exchange */
11141 switch( ciphersuite->key_exchange )
11142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011143 case MBEDTLS_KEY_EXCHANGE_RSA:
11144 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011145 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011146 break;
11147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011148 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11149 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11150 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11151 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011152 break;
11153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011154 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11155 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011156 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011157 break;
11158
11159 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011160 case MBEDTLS_KEY_EXCHANGE_NONE:
11161 case MBEDTLS_KEY_EXCHANGE_PSK:
11162 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11163 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011164 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011165 usage = 0;
11166 }
11167 }
11168 else
11169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011170 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11171 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011172 }
11173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011174 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011175 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011176 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011177 ret = -1;
11178 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011179#else
11180 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011181#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011183#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11184 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011186 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11187 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011188 }
11189 else
11190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11192 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011193 }
11194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011195 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011196 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011197 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011198 ret = -1;
11199 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011200#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011201
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011202 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011204#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011205
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011206/*
11207 * Convert version numbers to/from wire format
11208 * and, for DTLS, to/from TLS equivalent.
11209 *
11210 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011211 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011212 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11213 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011215void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011216 unsigned char ver[2] )
11217{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011218#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11219 ((void) transport);
11220#endif
11221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011223 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011225 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011226 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11227
11228 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11229 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11230 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011231 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011232#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011233#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011234 {
11235 ver[0] = (unsigned char) major;
11236 ver[1] = (unsigned char) minor;
11237 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011238#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011239}
11240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011241void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011242 const unsigned char ver[2] )
11243{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011244#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11245 ((void) transport);
11246#endif
11247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011248#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011249 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011250 {
11251 *major = 255 - ver[0] + 2;
11252 *minor = 255 - ver[1] + 1;
11253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011254 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011255 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11256 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011257 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011258#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011259#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011260 {
11261 *major = ver[0];
11262 *minor = ver[1];
11263 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011264#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011265}
11266
Simon Butcher99000142016-10-13 17:21:01 +010011267int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11268{
11269#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11270 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11271 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11272
11273 switch( md )
11274 {
11275#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11276#if defined(MBEDTLS_MD5_C)
11277 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011278 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011279#endif
11280#if defined(MBEDTLS_SHA1_C)
11281 case MBEDTLS_SSL_HASH_SHA1:
11282 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11283 break;
11284#endif
11285#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11286#if defined(MBEDTLS_SHA512_C)
11287 case MBEDTLS_SSL_HASH_SHA384:
11288 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11289 break;
11290#endif
11291#if defined(MBEDTLS_SHA256_C)
11292 case MBEDTLS_SSL_HASH_SHA256:
11293 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11294 break;
11295#endif
11296 default:
11297 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11298 }
11299
11300 return 0;
11301#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11302 (void) ssl;
11303 (void) md;
11304
11305 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11306#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11307}
11308
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011309#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11310 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11311int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11312 unsigned char *output,
11313 unsigned char *data, size_t data_len )
11314{
11315 int ret = 0;
11316 mbedtls_md5_context mbedtls_md5;
11317 mbedtls_sha1_context mbedtls_sha1;
11318
11319 mbedtls_md5_init( &mbedtls_md5 );
11320 mbedtls_sha1_init( &mbedtls_sha1 );
11321
11322 /*
11323 * digitally-signed struct {
11324 * opaque md5_hash[16];
11325 * opaque sha_hash[20];
11326 * };
11327 *
11328 * md5_hash
11329 * MD5(ClientHello.random + ServerHello.random
11330 * + ServerParams);
11331 * sha_hash
11332 * SHA(ClientHello.random + ServerHello.random
11333 * + ServerParams);
11334 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011335 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011336 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011338 goto exit;
11339 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011340 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011341 ssl->handshake->randbytes, 64 ) ) != 0 )
11342 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011344 goto exit;
11345 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011346 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011347 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011349 goto exit;
11350 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011351 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011352 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354 goto exit;
11355 }
11356
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011357 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011358 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011360 goto exit;
11361 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011362 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011363 ssl->handshake->randbytes, 64 ) ) != 0 )
11364 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011366 goto exit;
11367 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011368 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011369 data_len ) ) != 0 )
11370 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011372 goto exit;
11373 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011374 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011375 output + 16 ) ) != 0 )
11376 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011378 goto exit;
11379 }
11380
11381exit:
11382 mbedtls_md5_free( &mbedtls_md5 );
11383 mbedtls_sha1_free( &mbedtls_sha1 );
11384
11385 if( ret != 0 )
11386 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11387 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11388
11389 return( ret );
11390
11391}
11392#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11393 MBEDTLS_SSL_PROTO_TLS1_1 */
11394
11395#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11396 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11397int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011398 unsigned char *hash, size_t *hashlen,
11399 unsigned char *data, size_t data_len,
11400 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011401{
11402 int ret = 0;
11403 mbedtls_md_context_t ctx;
11404 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011405 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011406
11407 mbedtls_md_init( &ctx );
11408
11409 /*
11410 * digitally-signed struct {
11411 * opaque client_random[32];
11412 * opaque server_random[32];
11413 * ServerDHParams params;
11414 * };
11415 */
11416 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11417 {
11418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11419 goto exit;
11420 }
11421 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11422 {
11423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11424 goto exit;
11425 }
11426 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11427 {
11428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11429 goto exit;
11430 }
11431 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11432 {
11433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11434 goto exit;
11435 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011436 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011437 {
11438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11439 goto exit;
11440 }
11441
11442exit:
11443 mbedtls_md_free( &ctx );
11444
11445 if( ret != 0 )
11446 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11447 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11448
11449 return( ret );
11450}
11451#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11452 MBEDTLS_SSL_PROTO_TLS1_2 */
11453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011454#endif /* MBEDTLS_SSL_TLS_C */