blob: 5ff66eb7dc5e22d6a64154245539ef2d41011f0d [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],
754#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
755 int encrypt_then_mac,
756#endif
757#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
758 int trunc_hmac,
759#endif
760#if defined(MBEDTLS_ZLIB_SUPPORT)
761 int compression,
762#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200763 ssl_tls_prf_t tls_prf,
764 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200765 int minor_ver,
766 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200767 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000768{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200769 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000770 unsigned char keyblk[256];
771 unsigned char *key1;
772 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100773 unsigned char *mac_enc;
774 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000775 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200776 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000777 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000778 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 const mbedtls_cipher_info_t *cipher_info;
780 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100781
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200782#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
783 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
784 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200785 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200786 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000787#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000788
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200789 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200791 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000792#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200793 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200795 /*
796 * Get various info structures
797 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200798 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200799 if( ciphersuite_info == NULL )
800 {
801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200802 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
804 }
805
Hanno Becker8759e162017-12-27 21:34:08 +0000806 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100807 if( cipher_info == NULL )
808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000810 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100812 }
813
Hanno Becker8759e162017-12-27 21:34:08 +0000814 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100815 if( md_info == NULL )
816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000818 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100820 }
821
Hanno Beckera5a2b082019-05-15 14:03:01 +0100822#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100823 /* Copy own and peer's CID if the use of the CID
824 * extension has been negotiated. */
825 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
826 {
827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100828
Hanno Becker4932f9f2019-05-03 15:23:51 +0100829 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100830 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100831 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100832 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100833
834 transform->out_cid_len = ssl->handshake->peer_cid_len;
835 memcpy( transform->out_cid, ssl->handshake->peer_cid,
836 ssl->handshake->peer_cid_len );
837 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
838 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100839 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100840#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100841
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200843 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000844 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200845 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100846 if( ret != 0 )
847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100849 return( ret );
850 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200853 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200854 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200855 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000857
Paul Bakker5121ce52009-01-03 21:22:43 +0000858 /*
859 * Determine the appropriate key, IV and MAC length.
860 */
Paul Bakker68884e32013-01-07 18:20:04 +0100861
Hanno Beckere7f2df02017-12-27 08:17:40 +0000862 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200863
Hanno Beckerf1229442018-01-03 15:32:31 +0000864#if defined(MBEDTLS_GCM_C) || \
865 defined(MBEDTLS_CCM_C) || \
866 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200868 cipher_info->mode == MBEDTLS_MODE_CCM ||
869 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 {
Hanno Becker8759e162017-12-27 21:34:08 +0000871 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200872
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200873 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000874 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000875 transform->taglen =
876 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200878 /* All modes haves 96-bit IVs;
879 * GCM and CCM has 4 implicit and 8 explicit bytes
880 * ChachaPoly has all 12 bytes implicit
881 */
Paul Bakker68884e32013-01-07 18:20:04 +0100882 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200883 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
884 transform->fixed_ivlen = 12;
885 else
886 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200887
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200888 /* Minimum length of encrypted record */
889 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000890 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100891 }
892 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000893#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
894#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
895 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
896 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100897 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200898 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
900 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200903 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100904 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000907 mac_key_len = mbedtls_md_get_size( md_info );
908 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200911 /*
912 * If HMAC is to be truncated, we shall keep the leftmost bytes,
913 * (rfc 6066 page 13 or rfc 2104 section 4),
914 * so we only need to adjust the length here.
915 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200916 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000919
920#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
921 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000922 * HMAC implementation which also truncates the key
923 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000924 mac_key_len = transform->maclen;
925#endif
926 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200928
929 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100930 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200932 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200934 transform->minlen = transform->maclen;
935 else
Paul Bakker68884e32013-01-07 18:20:04 +0100936 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937 /*
938 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100939 * 1. if EtM is in use: one block plus MAC
940 * otherwise: * first multiple of blocklen greater than maclen
941 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200944 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100945 {
946 transform->minlen = transform->maclen
947 + cipher_info->block_size;
948 }
949 else
950#endif
951 {
952 transform->minlen = transform->maclen
953 + cipher_info->block_size
954 - transform->maclen % cipher_info->block_size;
955 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200958 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
959 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200960 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100961 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200962#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200964 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
965 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200966 {
967 transform->minlen += transform->ivlen;
968 }
969 else
970#endif
971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200974 }
Paul Bakker68884e32013-01-07 18:20:04 +0100975 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000977 else
978#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
979 {
980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
982 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
Hanno Beckere7f2df02017-12-27 08:17:40 +0000984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
985 (unsigned) keylen,
986 (unsigned) transform->minlen,
987 (unsigned) transform->ivlen,
988 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 /*
991 * Finally setup the cipher contexts, IVs and MAC secrets.
992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200994 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000996 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000997 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
Paul Bakker68884e32013-01-07 18:20:04 +0100999 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001000 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001002 /*
1003 * This is not used in TLS v1.1.
1004 */
Paul Bakker48916f92012-09-16 19:57:18 +00001005 iv_copy_len = ( transform->fixed_ivlen ) ?
1006 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001007 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1008 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001009 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 }
1011 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_SSL_CLI_C */
1013#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001014 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001016 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001017 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Hanno Becker81c7b182017-11-09 18:39:33 +00001019 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001020 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001022 /*
1023 * This is not used in TLS v1.1.
1024 */
Paul Bakker48916f92012-09-16 19:57:18 +00001025 iv_copy_len = ( transform->fixed_ivlen ) ?
1026 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001027 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1028 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001029 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001031 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Hanno Becker92231322018-01-03 15:32:51 +00001038#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001040 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001041 {
Hanno Becker92231322018-01-03 15:32:51 +00001042 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001046 }
1047
Hanno Becker81c7b182017-11-09 18:39:33 +00001048 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1049 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001050 }
1051 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1053#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1054 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001055 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001056 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001057 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1058 For AEAD-based ciphersuites, there is nothing to do here. */
1059 if( mac_key_len != 0 )
1060 {
1061 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1062 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1063 }
Paul Bakker68884e32013-01-07 18:20:04 +01001064 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001065 else
1066#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1069 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001070 }
Hanno Becker92231322018-01-03 15:32:51 +00001071#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1074 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001075 {
1076 int ret = 0;
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001079
Hanno Beckere7f2df02017-12-27 08:17:40 +00001080 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001081 transform->iv_enc, transform->iv_dec,
1082 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001083 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001084 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1087 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001088 }
1089 }
Hanno Becker92231322018-01-03 15:32:51 +00001090#else
1091 ((void) mac_dec);
1092 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001094
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001095#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1096 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001097 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001098 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001099 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001100 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001101 iv_copy_len );
1102 }
1103#endif
1104
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001105 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001106 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 return( ret );
1110 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001111
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001112 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001113 cipher_info ) ) != 0 )
1114 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 return( ret );
1117 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001120 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001124 return( ret );
1125 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001128 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001132 return( ret );
1133 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#if defined(MBEDTLS_CIPHER_MODE_CBC)
1136 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1139 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001142 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001143 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1146 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001149 return( ret );
1150 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001154 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001156 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001158 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001161
Paul Bakker48916f92012-09-16 19:57:18 +00001162 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1163 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001164
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001165 if( deflateInit( &transform->ctx_deflate,
1166 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001167 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1170 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001171 }
1172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001174
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 return( 0 );
1176}
1177
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001178/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001179 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001180 *
1181 * Inputs:
1182 * - SSL/TLS minor version
1183 * - hash associated with the ciphersuite (only used by TLS 1.2)
1184 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001185 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001186 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1187 */
1188static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1189 int minor_ver,
1190 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001191{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001192#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1193 (void) hash;
1194#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001195
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001196#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001197 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001198 {
1199 handshake->tls_prf = ssl3_prf;
1200 handshake->calc_verify = ssl_calc_verify_ssl;
1201 handshake->calc_finished = ssl_calc_finished_ssl;
1202 }
1203 else
1204#endif
1205#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001206 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001207 {
1208 handshake->tls_prf = tls1_prf;
1209 handshake->calc_verify = ssl_calc_verify_tls;
1210 handshake->calc_finished = ssl_calc_finished_tls;
1211 }
1212 else
1213#endif
1214#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1215#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001216 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1217 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001218 {
1219 handshake->tls_prf = tls_prf_sha384;
1220 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1221 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1222 }
1223 else
1224#endif
1225#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001226 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001227 {
1228 handshake->tls_prf = tls_prf_sha256;
1229 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1230 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1231 }
1232 else
1233#endif
1234#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1235 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1237 }
1238
1239 return( 0 );
1240}
1241
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001242/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001243 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001244 *
1245 * Parameters:
1246 * [in/out] handshake
1247 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1248 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001249 * [out] master
1250 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001251 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001253 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001254 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001255{
1256 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001257
1258#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001259 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001260 (void) ssl;
1261#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001262
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001264 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1266 return( 0 );
1267 }
1268
1269 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1270 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001271
1272#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001273 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001274 {
1275 unsigned char session_hash[48];
1276 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001277
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001278 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001279
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001280 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1281 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001282
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001283 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001284 "extended master secret",
1285 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001286 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001287 }
1288 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001289#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001290 {
1291 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1292 "master secret",
1293 handshake->randbytes, 64,
1294 master, 48 );
1295 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001296 if( ret != 0 )
1297 {
1298 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1299 return( ret );
1300 }
1301
1302 mbedtls_platform_zeroize( handshake->premaster,
1303 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001304
1305 return( 0 );
1306}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001307
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001308int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1309{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001310 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001311 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1312 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001313
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1315
1316 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001317 ret = ssl_set_handshake_prfs( ssl->handshake,
1318 ssl->minor_ver,
1319 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001320 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001321 {
1322 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001323 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001324 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001325
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001326 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001327 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001328 ssl->session_negotiate->master,
1329 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001330 if( ret != 0 )
1331 {
1332 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1333 return( ret );
1334 }
1335
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001336 /* Swap the client and server random values:
1337 * - MS derivation wanted client+server (RFC 5246 8.1)
1338 * - key derivation wants server+client (RFC 5246 6.3) */
1339 {
1340 unsigned char tmp[64];
1341 memcpy( tmp, ssl->handshake->randbytes, 64 );
1342 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1343 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1344 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1345 }
1346
1347 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001348 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001349 ssl->session_negotiate->ciphersuite,
1350 ssl->session_negotiate->master,
1351#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1352 ssl->session_negotiate->encrypt_then_mac,
1353#endif
1354#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1355 ssl->session_negotiate->trunc_hmac,
1356#endif
1357#if defined(MBEDTLS_ZLIB_SUPPORT)
1358 ssl->session_negotiate->compression,
1359#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001360 ssl->handshake->tls_prf,
1361 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001362 ssl->minor_ver,
1363 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001364 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001365 if( ret != 0 )
1366 {
1367 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1368 return( ret );
1369 }
1370
1371 /* We no longer need Server/ClientHello.random values */
1372 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1373 sizeof( ssl->handshake->randbytes ) );
1374
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001375 /* Allocate compression buffer */
1376#if defined(MBEDTLS_ZLIB_SUPPORT)
1377 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1378 ssl->compress_buf == NULL )
1379 {
1380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1381 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1382 if( ssl->compress_buf == NULL )
1383 {
1384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001385 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1387 }
1388 }
1389#endif
1390
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1392
1393 return( 0 );
1394}
1395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001397void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1398 unsigned char hash[36],
1399 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001400{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001401 mbedtls_md5_context md5;
1402 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 unsigned char pad_1[48];
1404 unsigned char pad_2[48];
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001408 mbedtls_md5_init( &md5 );
1409 mbedtls_sha1_init( &sha1 );
1410
1411 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1412 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Paul Bakker380da532012-04-18 16:10:25 +00001414 memset( pad_1, 0x36, 48 );
1415 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001417 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1418 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1419 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001421 mbedtls_md5_starts_ret( &md5 );
1422 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1423 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1424 mbedtls_md5_update_ret( &md5, hash, 16 );
1425 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001427 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1428 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1429 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001431 mbedtls_sha1_starts_ret( &sha1 );
1432 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1433 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1434 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1435 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001436
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001437 *hlen = 36;
1438
1439 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001441
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001442 mbedtls_md5_free( &md5 );
1443 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001444
Paul Bakker380da532012-04-18 16:10:25 +00001445 return;
1446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001450void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1451 unsigned char hash[36],
1452 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001453{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001454 mbedtls_md5_context md5;
1455 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001458
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001459 mbedtls_md5_init( &md5 );
1460 mbedtls_sha1_init( &sha1 );
1461
1462 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1463 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001465 mbedtls_md5_finish_ret( &md5, hash );
1466 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001467
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001468 *hlen = 36;
1469
1470 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001473 mbedtls_md5_free( &md5 );
1474 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001475
Paul Bakker380da532012-04-18 16:10:25 +00001476 return;
1477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1481#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001482void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1483 unsigned char hash[32],
1484 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001485{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001486 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001487
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001488 mbedtls_sha256_init( &sha256 );
1489
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001492 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001493 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001494
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001495 *hlen = 32;
1496
1497 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001500 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001501
Paul Bakker380da532012-04-18 16:10:25 +00001502 return;
1503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001507void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1508 unsigned char hash[48],
1509 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001510{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001511 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001512
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001513 mbedtls_sha512_init( &sha512 );
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001516
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001517 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001518 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001519
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001520 *hlen = 48;
1521
1522 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001525 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001526
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 return;
1528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_SHA512_C */
1530#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1533int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001534{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001535 unsigned char *p = ssl->handshake->premaster;
1536 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001537 const unsigned char *psk = ssl->conf->psk;
1538 size_t psk_len = ssl->conf->psk_len;
1539
1540 /* If the psk callback was called, use its result */
1541 if( ssl->handshake->psk != NULL )
1542 {
1543 psk = ssl->handshake->psk;
1544 psk_len = ssl->handshake->psk_len;
1545 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001546
1547 /*
1548 * PMS = struct {
1549 * opaque other_secret<0..2^16-1>;
1550 * opaque psk<0..2^16-1>;
1551 * };
1552 * with "other_secret" depending on the particular key exchange
1553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1555 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001556 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001557 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001559
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001560 *(p++) = (unsigned char)( psk_len >> 8 );
1561 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001562
1563 if( end < p || (size_t)( end - p ) < psk_len )
1564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1565
1566 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001567 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001568 }
1569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1571#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1572 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001573 {
1574 /*
1575 * other_secret already set by the ClientKeyExchange message,
1576 * and is 48 bytes long
1577 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001578 if( end - p < 2 )
1579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1580
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001581 *p++ = 0;
1582 *p++ = 48;
1583 p += 48;
1584 }
1585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1587#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1588 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001589 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001590 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001591 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001592
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001593 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001595 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001596 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001599 return( ret );
1600 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001601 *(p++) = (unsigned char)( len >> 8 );
1602 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001603 p += len;
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001606 }
1607 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1609#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1610 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001612 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 size_t zlen;
1614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001616 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001617 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001620 return( ret );
1621 }
1622
1623 *(p++) = (unsigned char)( zlen >> 8 );
1624 *(p++) = (unsigned char)( zlen );
1625 p += zlen;
1626
Janos Follath3fbdada2018-08-15 10:26:53 +01001627 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1628 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001629 }
1630 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1634 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001635 }
1636
1637 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001638 if( end - p < 2 )
1639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001640
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001641 *(p++) = (unsigned char)( psk_len >> 8 );
1642 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001643
1644 if( end < p || (size_t)( end - p ) < psk_len )
1645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1646
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001647 memcpy( p, psk, psk_len );
1648 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001649
1650 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1651
1652 return( 0 );
1653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001657/*
1658 * SSLv3.0 MAC functions
1659 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001660#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001661static void ssl_mac( mbedtls_md_context_t *md_ctx,
1662 const unsigned char *secret,
1663 const unsigned char *buf, size_t len,
1664 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001665 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001666{
1667 unsigned char header[11];
1668 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001669 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1671 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001672
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001673 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001675 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001676 else
Paul Bakker68884e32013-01-07 18:20:04 +01001677 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
1679 memcpy( header, ctr, 8 );
1680 header[ 8] = (unsigned char) type;
1681 header[ 9] = (unsigned char)( len >> 8 );
1682 header[10] = (unsigned char)( len );
1683
Paul Bakker68884e32013-01-07 18:20:04 +01001684 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_md_starts( md_ctx );
1686 mbedtls_md_update( md_ctx, secret, md_size );
1687 mbedtls_md_update( md_ctx, padding, padlen );
1688 mbedtls_md_update( md_ctx, header, 11 );
1689 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001690 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Paul Bakker68884e32013-01-07 18:20:04 +01001692 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_md_starts( md_ctx );
1694 mbedtls_md_update( md_ctx, secret, md_size );
1695 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001696 mbedtls_md_update( md_ctx, out, md_size );
1697 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001700
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001701/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001702 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001703#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001704 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1705 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1706 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1707/* This function makes sure every byte in the memory region is accessed
1708 * (in ascending addresses order) */
1709static void ssl_read_memory( unsigned char *p, size_t len )
1710{
1711 unsigned char acc = 0;
1712 volatile unsigned char force;
1713
1714 for( ; len != 0; p++, len-- )
1715 acc ^= *p;
1716
1717 force = acc;
1718 (void) force;
1719}
1720#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1721
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001722/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001723 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001724 */
Hanno Becker3307b532017-12-27 21:37:21 +00001725
Hanno Beckera5a2b082019-05-15 14:03:01 +01001726#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001727/* This functions transforms a DTLS plaintext fragment and a record content
1728 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001729 *
1730 * struct {
1731 * opaque content[DTLSPlaintext.length];
1732 * ContentType real_type;
1733 * uint8 zeros[length_of_padding];
1734 * } DTLSInnerPlaintext;
1735 *
1736 * Input:
1737 * - `content`: The beginning of the buffer holding the
1738 * plaintext to be wrapped.
1739 * - `*content_size`: The length of the plaintext in Bytes.
1740 * - `max_len`: The number of Bytes available starting from
1741 * `content`. This must be `>= *content_size`.
1742 * - `rec_type`: The desired record content type.
1743 *
1744 * Output:
1745 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1746 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1747 *
1748 * Returns:
1749 * - `0` on success.
1750 * - A negative error code if `max_len` didn't offer enough space
1751 * for the expansion.
1752 */
1753static int ssl_cid_build_inner_plaintext( unsigned char *content,
1754 size_t *content_size,
1755 size_t remaining,
1756 uint8_t rec_type )
1757{
1758 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001759 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1760 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1761 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001762
1763 /* Write real content type */
1764 if( remaining == 0 )
1765 return( -1 );
1766 content[ len ] = rec_type;
1767 len++;
1768 remaining--;
1769
1770 if( remaining < pad )
1771 return( -1 );
1772 memset( content + len, 0, pad );
1773 len += pad;
1774 remaining -= pad;
1775
1776 *content_size = len;
1777 return( 0 );
1778}
1779
Hanno Becker7dc25772019-05-20 15:08:01 +01001780/* This function parses a DTLSInnerPlaintext structure.
1781 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001782static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1783 size_t *content_size,
1784 uint8_t *rec_type )
1785{
1786 size_t remaining = *content_size;
1787
1788 /* Determine length of padding by skipping zeroes from the back. */
1789 do
1790 {
1791 if( remaining == 0 )
1792 return( -1 );
1793 remaining--;
1794 } while( content[ remaining ] == 0 );
1795
1796 *content_size = remaining;
1797 *rec_type = content[ remaining ];
1798
1799 return( 0 );
1800}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001801#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001802
Hanno Becker99abf512019-05-20 14:50:53 +01001803/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001804 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001805static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001806 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001807 mbedtls_record *rec )
1808{
Hanno Becker99abf512019-05-20 14:50:53 +01001809 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001810 *
1811 * additional_data = seq_num + TLSCompressed.type +
1812 * TLSCompressed.version + TLSCompressed.length;
1813 *
Hanno Becker99abf512019-05-20 14:50:53 +01001814 * For the CID extension, this is extended as follows
1815 * (quoting draft-ietf-tls-dtls-connection-id-05,
1816 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001817 *
1818 * additional_data = seq_num + DTLSPlaintext.type +
1819 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001820 * cid +
1821 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001822 * length_of_DTLSInnerPlaintext;
1823 */
1824
Hanno Becker3307b532017-12-27 21:37:21 +00001825 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1826 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001827 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001828
Hanno Beckera5a2b082019-05-15 14:03:01 +01001829#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001830 if( rec->cid_len != 0 )
1831 {
1832 memcpy( add_data + 11, rec->cid, rec->cid_len );
1833 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1834 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1835 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1836 *add_data_len = 13 + 1 + rec->cid_len;
1837 }
1838 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001839#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001840 {
1841 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1842 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1843 *add_data_len = 13;
1844 }
Hanno Becker3307b532017-12-27 21:37:21 +00001845}
1846
Hanno Becker611a83b2018-01-03 14:27:32 +00001847int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1848 mbedtls_ssl_transform *transform,
1849 mbedtls_record *rec,
1850 int (*f_rng)(void *, unsigned char *, size_t),
1851 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001854 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001855 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001856 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001857 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001858 size_t post_avail;
1859
1860 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001861#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001862 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001863 ((void) ssl);
1864#endif
1865
1866 /* The PRNG is used for dynamic IV generation that's used
1867 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1868#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1869 ( defined(MBEDTLS_AES_C) || \
1870 defined(MBEDTLS_ARIA_C) || \
1871 defined(MBEDTLS_CAMELLIA_C) ) && \
1872 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1873 ((void) f_rng);
1874 ((void) p_rng);
1875#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Hanno Becker3307b532017-12-27 21:37:21 +00001879 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001880 {
Hanno Becker3307b532017-12-27 21:37:21 +00001881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1882 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1883 }
Hanno Becker505089d2019-05-01 09:45:57 +01001884 if( rec == NULL
1885 || rec->buf == NULL
1886 || rec->buf_len < rec->data_offset
1887 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001889 || rec->cid_len != 0
1890#endif
1891 )
Hanno Becker3307b532017-12-27 21:37:21 +00001892 {
1893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001895 }
1896
Hanno Becker3307b532017-12-27 21:37:21 +00001897 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001898 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001900 data, rec->data_len );
1901
1902 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1903
1904 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1905 {
1906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1907 (unsigned) rec->data_len,
1908 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1910 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001911
Hanno Beckera5a2b082019-05-15 14:03:01 +01001912#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001913 /*
1914 * Add CID information
1915 */
1916 rec->cid_len = transform->out_cid_len;
1917 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1918 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001919
1920 if( rec->cid_len != 0 )
1921 {
1922 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001923 * Wrap plaintext into DTLSInnerPlaintext structure.
1924 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001925 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001926 * Note that this changes `rec->data_len`, and hence
1927 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001928 */
1929 if( ssl_cid_build_inner_plaintext( data,
1930 &rec->data_len,
1931 post_avail,
1932 rec->type ) != 0 )
1933 {
1934 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1935 }
1936
1937 rec->type = MBEDTLS_SSL_MSG_CID;
1938 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001939#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001940
Hanno Becker92c930f2019-04-29 17:31:37 +01001941 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1942
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001944 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001946#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 if( mode == MBEDTLS_MODE_STREAM ||
1948 ( mode == MBEDTLS_MODE_CBC
1949#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001950 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001951#endif
1952 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 {
Hanno Becker3307b532017-12-27 21:37:21 +00001954 if( post_avail < transform->maclen )
1955 {
1956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1957 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1958 }
1959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001961 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001962 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001963 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001964 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1965 data, rec->data_len, rec->ctr, rec->type, mac );
1966 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001967 }
1968 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1971 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001972 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 {
Hanno Becker992b6872017-11-09 18:57:39 +00001974 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1975
Hanno Beckere83efe62019-04-29 13:52:53 +01001976 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001977
Hanno Becker3307b532017-12-27 21:37:21 +00001978 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001979 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001980 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1981 data, rec->data_len );
1982 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1983 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1984
1985 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001986 }
1987 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001988#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001992 }
1993
Hanno Becker3307b532017-12-27 21:37:21 +00001994 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1995 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001996
Hanno Becker3307b532017-12-27 21:37:21 +00001997 rec->data_len += transform->maclen;
1998 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001999 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002000 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002001#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002003 /*
2004 * Encrypt
2005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2007 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002009 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002010 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002012 "including %d bytes of padding",
2013 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Hanno Becker3307b532017-12-27 21:37:21 +00002015 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2016 transform->iv_enc, transform->ivlen,
2017 data, rec->data_len,
2018 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002021 return( ret );
2022 }
2023
Hanno Becker3307b532017-12-27 21:37:21 +00002024 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002028 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 }
Paul Bakker68884e32013-01-07 18:20:04 +01002030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002032
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002033#if defined(MBEDTLS_GCM_C) || \
2034 defined(MBEDTLS_CCM_C) || \
2035 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002037 mode == MBEDTLS_MODE_CCM ||
2038 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002039 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002040 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002041 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002042 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002043
Hanno Becker3307b532017-12-27 21:37:21 +00002044 /* Check that there's space for both the authentication tag
2045 * and the explicit IV before and after the record content. */
2046 if( post_avail < transform->taglen ||
2047 rec->data_offset < explicit_iv_len )
2048 {
2049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2050 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2051 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002052
Paul Bakker68884e32013-01-07 18:20:04 +01002053 /*
2054 * Generate IV
2055 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002056 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2057 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002058 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002059 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002060 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2061 explicit_iv_len );
2062 /* Prefix record content with explicit IV. */
2063 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002064 }
2065 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2066 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002067 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002068 unsigned char i;
2069
2070 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2071
2072 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002073 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002074 }
2075 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002076 {
2077 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002080 }
2081
Hanno Beckere83efe62019-04-29 13:52:53 +01002082 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002083
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002084 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2085 iv, transform->ivlen );
2086 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002087 data - explicit_iv_len, explicit_iv_len );
2088 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002089 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002091 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002092 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002093
Paul Bakker68884e32013-01-07 18:20:04 +01002094 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002095 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002096 */
Hanno Becker3307b532017-12-27 21:37:21 +00002097
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002098 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002099 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002100 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002101 data, rec->data_len, /* source */
2102 data, &rec->data_len, /* destination */
2103 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002106 return( ret );
2107 }
2108
Hanno Becker3307b532017-12-27 21:37:21 +00002109 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2110 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002111
Hanno Becker3307b532017-12-27 21:37:21 +00002112 rec->data_len += transform->taglen + explicit_iv_len;
2113 rec->data_offset -= explicit_iv_len;
2114 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002115 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002116 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2119#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002120 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002123 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002124 size_t padlen, i;
2125 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002126
Hanno Becker3307b532017-12-27 21:37:21 +00002127 /* Currently we're always using minimal padding
2128 * (up to 255 bytes would be allowed). */
2129 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2130 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 padlen = 0;
2132
Hanno Becker3307b532017-12-27 21:37:21 +00002133 /* Check there's enough space in the buffer for the padding. */
2134 if( post_avail < padlen + 1 )
2135 {
2136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2137 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2138 }
2139
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002141 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Hanno Becker3307b532017-12-27 21:37:21 +00002143 rec->data_len += padlen + 1;
2144 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002147 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002148 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2149 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002150 */
Hanno Becker3307b532017-12-27 21:37:21 +00002151 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002152 {
Hanno Becker3307b532017-12-27 21:37:21 +00002153 if( f_rng == NULL )
2154 {
2155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2157 }
2158
2159 if( rec->data_offset < transform->ivlen )
2160 {
2161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2162 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2163 }
2164
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002165 /*
2166 * Generate IV
2167 */
Hanno Becker3307b532017-12-27 21:37:21 +00002168 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002169 if( ret != 0 )
2170 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002171
Hanno Becker3307b532017-12-27 21:37:21 +00002172 memcpy( data - transform->ivlen, transform->iv_enc,
2173 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002174
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002180 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002181 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002182
Hanno Becker3307b532017-12-27 21:37:21 +00002183 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2184 transform->iv_enc,
2185 transform->ivlen,
2186 data, rec->data_len,
2187 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002190 return( ret );
2191 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002192
Hanno Becker3307b532017-12-27 21:37:21 +00002193 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002197 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002200 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002201 {
2202 /*
2203 * Save IV in SSL3 and TLS1
2204 */
Hanno Becker3307b532017-12-27 21:37:21 +00002205 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2206 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 }
Hanno Becker3307b532017-12-27 21:37:21 +00002208 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002209#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002210 {
2211 data -= transform->ivlen;
2212 rec->data_offset -= transform->ivlen;
2213 rec->data_len += transform->ivlen;
2214 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002217 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002218 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002219 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2220
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221 /*
2222 * MAC(MAC_write_key, seq_num +
2223 * TLSCipherText.type +
2224 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002225 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002226 * IV + // except for TLS 1.0
2227 * ENC(content + padding + padding_length));
2228 */
Hanno Becker3307b532017-12-27 21:37:21 +00002229
2230 if( post_avail < transform->maclen)
2231 {
2232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2233 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2234 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002235
Hanno Beckere83efe62019-04-29 13:52:53 +01002236 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002239 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002240 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002241
Hanno Becker3307b532017-12-27 21:37:21 +00002242 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002243 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002244 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2245 data, rec->data_len );
2246 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2247 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002248
Hanno Becker3307b532017-12-27 21:37:21 +00002249 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002250
Hanno Becker3307b532017-12-27 21:37:21 +00002251 rec->data_len += transform->maclen;
2252 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002253 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002254 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002257 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002259 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002265 /* Make extra sure authentication was performed, exactly once */
2266 if( auth_done != 1 )
2267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002270 }
2271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
2274 return( 0 );
2275}
2276
Hanno Becker611a83b2018-01-03 14:27:32 +00002277int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2278 mbedtls_ssl_transform *transform,
2279 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002280{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002281 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002283 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002284#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002285 size_t padlen = 0, correct = 1;
2286#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002287 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002288 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002289 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002290
Hanno Becker611a83b2018-01-03 14:27:32 +00002291#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002292 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002293 ((void) ssl);
2294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002297 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002298 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2301 }
2302 if( rec == NULL ||
2303 rec->buf == NULL ||
2304 rec->buf_len < rec->data_offset ||
2305 rec->buf_len - rec->data_offset < rec->data_len )
2306 {
2307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002309 }
2310
Hanno Becker4c6876b2017-12-27 21:28:58 +00002311 data = rec->buf + rec->data_offset;
2312 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Hanno Beckera5a2b082019-05-15 14:03:01 +01002314#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002315 /*
2316 * Match record's CID with incoming CID.
2317 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002318 if( rec->cid_len != transform->in_cid_len ||
2319 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2320 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002321 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002322 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002323#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2326 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002327 {
2328 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002329 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2330 transform->iv_dec,
2331 transform->ivlen,
2332 data, rec->data_len,
2333 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002336 return( ret );
2337 }
2338
Hanno Becker4c6876b2017-12-27 21:28:58 +00002339 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 }
Paul Bakker68884e32013-01-07 18:20:04 +01002345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347#if defined(MBEDTLS_GCM_C) || \
2348 defined(MBEDTLS_CCM_C) || \
2349 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002351 mode == MBEDTLS_MODE_CCM ||
2352 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002353 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002355 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002356
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 /*
2358 * Compute and update sizes
2359 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002360 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002363 "+ taglen (%d)", rec->data_len,
2364 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002366 }
Paul Bakker68884e32013-01-07 18:20:04 +01002367
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002368 /*
2369 * Prepare IV
2370 */
2371 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2372 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002373 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002374 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002375 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002376
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002377 }
2378 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2379 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002380 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002381 unsigned char i;
2382
2383 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2384
2385 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002387 }
2388 else
2389 {
2390 /* Reminder if we ever add an AEAD mode with a different size */
2391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2393 }
2394
Hanno Becker4c6876b2017-12-27 21:28:58 +00002395 data += explicit_iv_len;
2396 rec->data_offset += explicit_iv_len;
2397 rec->data_len -= explicit_iv_len + transform->taglen;
2398
Hanno Beckere83efe62019-04-29 13:52:53 +01002399 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002400 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002401 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002402
2403 memcpy( transform->iv_dec + transform->fixed_ivlen,
2404 data - explicit_iv_len, explicit_iv_len );
2405
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002406 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002407 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002408 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002409
Paul Bakker68884e32013-01-07 18:20:04 +01002410
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002412 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002413 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002414 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2415 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002416 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002417 data, rec->data_len,
2418 data, &olen,
2419 data + rec->data_len,
2420 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2425 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002426
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002427 return( ret );
2428 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002429 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002430
Hanno Becker4c6876b2017-12-27 21:28:58 +00002431 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002435 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2439#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002440 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002443 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002444
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 /*
Paul Bakker45829992013-01-03 14:52:21 +01002446 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002449 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2450 {
2451 /* The ciphertext is prefixed with the CBC IV. */
2452 minlen += transform->ivlen;
2453 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002454#endif
Paul Bakker45829992013-01-03 14:52:21 +01002455
Hanno Becker4c6876b2017-12-27 21:28:58 +00002456 /* Size considerations:
2457 *
2458 * - The CBC cipher text must not be empty and hence
2459 * at least of size transform->ivlen.
2460 *
2461 * Together with the potential IV-prefix, this explains
2462 * the first of the two checks below.
2463 *
2464 * - The record must contain a MAC, either in plain or
2465 * encrypted, depending on whether Encrypt-then-MAC
2466 * is used or not.
2467 * - If it is, the message contains the IV-prefix,
2468 * the CBC ciphertext, and the MAC.
2469 * - If it is not, the padded plaintext, and hence
2470 * the CBC ciphertext, has at least length maclen + 1
2471 * because there is at least the padding length byte.
2472 *
2473 * As the CBC ciphertext is not empty, both cases give the
2474 * lower bound minlen + maclen + 1 on the record size, which
2475 * we test for in the second check below.
2476 */
2477 if( rec->data_len < minlen + transform->ivlen ||
2478 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002481 "+ 1 ) ( + expl IV )", rec->data_len,
2482 transform->ivlen,
2483 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002485 }
2486
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002487 /*
2488 * Authenticate before decrypt if enabled
2489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002491 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002492 {
Hanno Becker992b6872017-11-09 18:57:39 +00002493 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002496
Hanno Becker4c6876b2017-12-27 21:28:58 +00002497 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2498 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Hanno Beckere83efe62019-04-29 13:52:53 +01002500 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002501
Hanno Beckere83efe62019-04-29 13:52:53 +01002502 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2503 add_data_len );
2504 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2505 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002506 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2507 data, rec->data_len );
2508 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2509 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002510
Hanno Becker4c6876b2017-12-27 21:28:58 +00002511 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2512 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002513 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002514 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002515
Hanno Becker4c6876b2017-12-27 21:28:58 +00002516 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2517 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002521 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002522 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002525
2526 /*
2527 * Check length sanity
2528 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002529 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002532 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002534 }
2535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002537 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002538 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002539 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002540 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002541 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002542 /* This is safe because data_len >= minlen + maclen + 1 initially,
2543 * and at this point we have at most subtracted maclen (note that
2544 * minlen == transform->ivlen here). */
2545 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002546
Hanno Becker4c6876b2017-12-27 21:28:58 +00002547 data += transform->ivlen;
2548 rec->data_offset += transform->ivlen;
2549 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002552
Hanno Becker4c6876b2017-12-27 21:28:58 +00002553 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2554 transform->iv_dec, transform->ivlen,
2555 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002558 return( ret );
2559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002560
Hanno Becker4c6876b2017-12-27 21:28:58 +00002561 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002568 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002569 {
2570 /*
2571 * Save IV in SSL3 and TLS1
2572 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002573 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2574 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002576#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Hanno Becker4c6876b2017-12-27 21:28:58 +00002578 /* Safe since data_len >= minlen + maclen + 1, so after having
2579 * subtracted at most minlen and maclen up to this point,
2580 * data_len > 0. */
2581 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002582
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583 if( auth_done == 1 )
2584 {
2585 correct *= ( rec->data_len >= padlen + 1 );
2586 padlen *= ( rec->data_len >= padlen + 1 );
2587 }
2588 else
Paul Bakker45829992013-01-03 14:52:21 +01002589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 if( rec->data_len < transform->maclen + padlen + 1 )
2592 {
2593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2594 rec->data_len,
2595 transform->maclen,
2596 padlen + 1 ) );
2597 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002598#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002599
2600 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2601 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Hanno Becker4c6876b2017-12-27 21:28:58 +00002604 padlen++;
2605
2606 /* Regardless of the validity of the padding,
2607 * we have data_len >= padlen here. */
2608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002610 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002612 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_DEBUG_ALL)
2615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002616 "should be no more than %d",
2617 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002618#endif
Paul Bakker45829992013-01-03 14:52:21 +01002619 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 }
2621 }
2622 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2624#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2625 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002626 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002628 /* The padding check involves a series of up to 256
2629 * consecutive memory reads at the end of the record
2630 * plaintext buffer. In order to hide the length and
2631 * validity of the padding, always perform exactly
2632 * `min(256,plaintext_len)` reads (but take into account
2633 * only the last `padlen` bytes for the padding check). */
2634 size_t pad_count = 0;
2635 size_t real_count = 0;
2636 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002637
Hanno Becker4c6876b2017-12-27 21:28:58 +00002638 /* Index of first padding byte; it has been ensured above
2639 * that the subtraction is safe. */
2640 size_t const padding_idx = rec->data_len - padlen;
2641 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2642 size_t const start_idx = rec->data_len - num_checks;
2643 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002644
Hanno Becker4c6876b2017-12-27 21:28:58 +00002645 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002646 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002647 real_count |= ( idx >= padding_idx );
2648 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002649 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002653 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002655#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002656 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002658 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2660 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002664 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002665
Hanno Becker4c6876b2017-12-27 21:28:58 +00002666 /* If the padding was found to be invalid, padlen == 0
2667 * and the subtraction is safe. If the padding was found valid,
2668 * padlen hasn't been changed and the previous assertion
2669 * data_len >= padlen still holds. */
2670 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002674 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002680#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002682 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002683#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002686 * Authenticate if not done yet.
2687 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002689#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002690 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002691 {
Hanno Becker992b6872017-11-09 18:57:39 +00002692 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002693
Hanno Becker4c6876b2017-12-27 21:28:58 +00002694 /* If the initial value of padlen was such that
2695 * data_len < maclen + padlen + 1, then padlen
2696 * got reset to 1, and the initial check
2697 * data_len >= minlen + maclen + 1
2698 * guarantees that at this point we still
2699 * have at least data_len >= maclen.
2700 *
2701 * If the initial value of padlen was such that
2702 * data_len >= maclen + padlen + 1, then we have
2703 * subtracted either padlen + 1 (if the padding was correct)
2704 * or 0 (if the padding was incorrect) since then,
2705 * hence data_len >= maclen in any case.
2706 */
2707 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Hanno Beckere83efe62019-04-29 13:52:53 +01002709 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002712 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002713 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002714 ssl_mac( &transform->md_ctx_dec,
2715 transform->mac_dec,
2716 data, rec->data_len,
2717 rec->ctr, rec->type,
2718 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002719 }
2720 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2722#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2723 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002724 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002725 {
2726 /*
2727 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002728 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002729 *
2730 * Known timing attacks:
2731 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2732 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002733 * To compensate for different timings for the MAC calculation
2734 * depending on how much padding was removed (which is determined
2735 * by padlen), process extra_run more blocks through the hash
2736 * function.
2737 *
2738 * The formula in the paper is
2739 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2740 * where L1 is the size of the header plus the decrypted message
2741 * plus CBC padding and L2 is the size of the header plus the
2742 * decrypted message. This is for an underlying hash function
2743 * with 64-byte blocks.
2744 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2745 * correctly. We round down instead of up, so -56 is the correct
2746 * value for our calculations instead of -55.
2747 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002748 * Repeat the formula rather than defining a block_size variable.
2749 * This avoids requiring division by a variable at runtime
2750 * (which would be marginally less efficient and would require
2751 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002752 */
2753 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002754 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002755
2756 /*
2757 * The next two sizes are the minimum and maximum values of
2758 * in_msglen over all padlen values.
2759 *
2760 * They're independent of padlen, since we previously did
2761 * in_msglen -= padlen.
2762 *
2763 * Note that max_len + maclen is never more than the buffer
2764 * length, as we previously did in_msglen -= maclen too.
2765 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002766 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002767 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2768
Hanno Becker4c6876b2017-12-27 21:28:58 +00002769 memset( tmp, 0, sizeof( tmp ) );
2770
2771 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002772 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002773#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2774 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002775 case MBEDTLS_MD_MD5:
2776 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002778 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002779 extra_run =
2780 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2781 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002782 break;
2783#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002784#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002785 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002786 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002787 extra_run =
2788 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2789 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002790 break;
2791#endif
2792 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2795 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002796
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002797 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002798
Hanno Beckere83efe62019-04-29 13:52:53 +01002799 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2800 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002801 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2802 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002803 /* Make sure we access everything even when padlen > 0. This
2804 * makes the synchronisation requirements for just-in-time
2805 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002806 ssl_read_memory( data + rec->data_len, padlen );
2807 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002808
2809 /* Call mbedtls_md_process at least once due to cache attacks
2810 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002811 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002812 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002813
Hanno Becker4c6876b2017-12-27 21:28:58 +00002814 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002815
2816 /* Make sure we access all the memory that could contain the MAC,
2817 * before we check it in the next code block. This makes the
2818 * synchronisation requirements for just-in-time Prime+Probe
2819 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 ssl_read_memory( data + min_len,
2821 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002822 }
2823 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2825 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002829 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002831#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002832 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2833 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002834#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Hanno Becker4c6876b2017-12-27 21:28:58 +00002836 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2837 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#if defined(MBEDTLS_SSL_DEBUG_ALL)
2840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002841#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002842 correct = 0;
2843 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002844 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002845 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002846
2847 /*
2848 * Finally check the correct flag
2849 */
2850 if( correct == 0 )
2851 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002852#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002853
2854 /* Make extra sure authentication was performed, exactly once */
2855 if( auth_done != 1 )
2856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Hanno Beckera5a2b082019-05-15 14:03:01 +01002861#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002862 if( rec->cid_len != 0 )
2863 {
2864 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2865 &rec->type );
2866 if( ret != 0 )
2867 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2868 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002869#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
2873 return( 0 );
2874}
2875
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002876#undef MAC_NONE
2877#undef MAC_PLAINTEXT
2878#undef MAC_CIPHERTEXT
2879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002881/*
2882 * Compression/decompression functions
2883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002885{
2886 int ret;
2887 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002888 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002889 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002890 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002894 if( len_pre == 0 )
2895 return( 0 );
2896
Paul Bakker2770fbd2012-07-03 13:30:23 +00002897 memcpy( msg_pre, ssl->out_msg, len_pre );
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002900 ssl->out_msglen ) );
2901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903 ssl->out_msg, ssl->out_msglen );
2904
Paul Bakker48916f92012-09-16 19:57:18 +00002905 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2906 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2907 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002908 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002909
Paul Bakker48916f92012-09-16 19:57:18 +00002910 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911 if( ret != Z_OK )
2912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2914 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915 }
2916
Angus Grattond8213d02016-05-25 20:56:48 +10002917 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002918 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002921 ssl->out_msglen ) );
2922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924 ssl->out_msg, ssl->out_msglen );
2925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927
2928 return( 0 );
2929}
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002932{
2933 int ret;
2934 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002935 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002936 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002937 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002941 if( len_pre == 0 )
2942 return( 0 );
2943
Paul Bakker2770fbd2012-07-03 13:30:23 +00002944 memcpy( msg_pre, ssl->in_msg, len_pre );
2945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947 ssl->in_msglen ) );
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950 ssl->in_msg, ssl->in_msglen );
2951
Paul Bakker48916f92012-09-16 19:57:18 +00002952 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2953 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2954 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002955 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002956 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002957
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002959 if( ret != Z_OK )
2960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2962 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963 }
2964
Angus Grattond8213d02016-05-25 20:56:48 +10002965 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002966 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002969 ssl->in_msglen ) );
2970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002972 ssl->in_msg, ssl->in_msglen );
2973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975
2976 return( 0 );
2977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2981static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#if defined(MBEDTLS_SSL_PROTO_DTLS)
2984static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002985{
2986 /* If renegotiation is not enforced, retransmit until we would reach max
2987 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002988 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002989 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002990 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002991 unsigned char doublings = 1;
2992
2993 while( ratio != 0 )
2994 {
2995 ++doublings;
2996 ratio >>= 1;
2997 }
2998
2999 if( ++ssl->renego_records_seen > doublings )
3000 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003002 return( 0 );
3003 }
3004 }
3005
3006 return( ssl_write_hello_request( ssl ) );
3007}
3008#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003010
Paul Bakker5121ce52009-01-03 21:22:43 +00003011/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003012 * Fill the input message buffer by appending data to it.
3013 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003014 *
3015 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3016 * available (from this read and/or a previous one). Otherwise, an error code
3017 * is returned (possibly EOF or WANT_READ).
3018 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003019 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3020 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3021 * since we always read a whole datagram at once.
3022 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003023 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003024 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027{
Paul Bakker23986e52011-04-24 08:57:21 +00003028 int ret;
3029 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003033 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003036 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003038 }
3039
Angus Grattond8213d02016-05-25 20:56:48 +10003040 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003044 }
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003047 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003048 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003049 uint32_t timeout;
3050
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003051 /* Just to be sure */
3052 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3053 {
3054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3055 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3057 }
3058
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003059 /*
3060 * The point is, we need to always read a full datagram at once, so we
3061 * sometimes read more then requested, and handle the additional data.
3062 * It could be the rest of the current record (while fetching the
3063 * header) and/or some other records in the same datagram.
3064 */
3065
3066 /*
3067 * Move to the next record in the already read datagram if applicable
3068 */
3069 if( ssl->next_record_offset != 0 )
3070 {
3071 if( ssl->in_left < ssl->next_record_offset )
3072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003075 }
3076
3077 ssl->in_left -= ssl->next_record_offset;
3078
3079 if( ssl->in_left != 0 )
3080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003082 ssl->next_record_offset ) );
3083 memmove( ssl->in_hdr,
3084 ssl->in_hdr + ssl->next_record_offset,
3085 ssl->in_left );
3086 }
3087
3088 ssl->next_record_offset = 0;
3089 }
3090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003093
3094 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003095 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003096 */
3097 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003100 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003101 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003102
3103 /*
3104 * A record can't be split accross datagrams. If we need to read but
3105 * are not at the beginning of a new record, the caller did something
3106 * wrong.
3107 */
3108 if( ssl->in_left != 0 )
3109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3111 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003112 }
3113
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003114 /*
3115 * Don't even try to read if time's out already.
3116 * This avoids by-passing the timer when repeatedly receiving messages
3117 * that will end up being dropped.
3118 */
3119 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003120 {
3121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003122 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003123 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003124 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003125 {
Angus Grattond8213d02016-05-25 20:56:48 +10003126 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 timeout = ssl->handshake->retransmit_timeout;
3130 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003131 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003134
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003135 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003136 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3137 timeout );
3138 else
3139 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003142
3143 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003145 }
3146
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003147 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003150 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003153 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003154 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003157 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003158 }
3159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003163 return( ret );
3164 }
3165
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003166 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003167 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003169 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003171 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003172 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003175 return( ret );
3176 }
3177
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003178 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003181 }
3182
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 if( ret < 0 )
3184 return( ret );
3185
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003186 ssl->in_left = ret;
3187 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003188 MBEDTLS_SSL_TRANSPORT_ELSE
3189#endif /* MBEDTLS_SSL_PROTO_DTLS */
3190#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003193 ssl->in_left, nb_want ) );
3194
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003195 while( ssl->in_left < nb_want )
3196 {
3197 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003198
3199 if( ssl_check_timer( ssl ) != 0 )
3200 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3201 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003202 {
3203 if( ssl->f_recv_timeout != NULL )
3204 {
3205 ret = ssl->f_recv_timeout( ssl->p_bio,
3206 ssl->in_hdr + ssl->in_left, len,
3207 ssl->conf->read_timeout );
3208 }
3209 else
3210 {
3211 ret = ssl->f_recv( ssl->p_bio,
3212 ssl->in_hdr + ssl->in_left, len );
3213 }
3214 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003217 ssl->in_left, nb_want ) );
3218 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003219
3220 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003222
3223 if( ret < 0 )
3224 return( ret );
3225
mohammad160352aecb92018-03-28 23:41:40 -07003226 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003227 {
Darryl Green11999bb2018-03-13 15:22:58 +00003228 MBEDTLS_SSL_DEBUG_MSG( 1,
3229 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003230 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003231 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3232 }
3233
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003234 ssl->in_left += ret;
3235 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003237#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
3241 return( 0 );
3242}
3243
3244/*
3245 * Flush any data not yet written
3246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003248{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003249 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003250 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003254 if( ssl->f_send == NULL )
3255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003257 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003259 }
3260
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003261 /* Avoid incrementing counter if data is flushed */
3262 if( ssl->out_left == 0 )
3263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003265 return( 0 );
3266 }
3267
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 while( ssl->out_left > 0 )
3269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003271 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
Hanno Becker2b1e3542018-08-06 11:19:13 +01003273 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003274 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277
3278 if( ret <= 0 )
3279 return( ret );
3280
mohammad160352aecb92018-03-28 23:41:40 -07003281 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003282 {
Darryl Green11999bb2018-03-13 15:22:58 +00003283 MBEDTLS_SSL_DEBUG_MSG( 1,
3284 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003285 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3287 }
3288
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 ssl->out_left -= ret;
3290 }
3291
Hanno Becker2b1e3542018-08-06 11:19:13 +01003292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003293 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003294 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003295 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003296 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003297 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003298#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003299#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003300 {
3301 ssl->out_hdr = ssl->out_buf + 8;
3302 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003303#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003304 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003307
3308 return( 0 );
3309}
3310
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003311/*
3312 * Functions to handle the DTLS retransmission state machine
3313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003315/*
3316 * Append current handshake message to current outgoing flight
3317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3322 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3323 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324
3325 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003326 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003327 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003331 }
3332
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003333 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003334 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003337 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003338 }
3339
3340 /* Copy current handshake message with headers */
3341 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3342 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003343 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003344 msg->next = NULL;
3345
3346 /* Append to the current flight */
3347 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003348 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349 else
3350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352 while( cur->next != NULL )
3353 cur = cur->next;
3354 cur->next = msg;
3355 }
3356
Hanno Becker3b235902018-08-06 09:54:53 +01003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003358 return( 0 );
3359}
3360
3361/*
3362 * Free the current flight of handshake messages
3363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 mbedtls_ssl_flight_item *cur = flight;
3367 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368
3369 while( cur != NULL )
3370 {
3371 next = cur->next;
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 mbedtls_free( cur->p );
3374 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003375
3376 cur = next;
3377 }
3378}
3379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3381static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003382#endif
3383
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003385 * Swap transform_out and out_ctr with the alternative ones
3386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003390 unsigned char tmp_out_ctr[8];
3391
3392 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003395 return;
3396 }
3397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003399
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003400 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003401 tmp_transform = ssl->transform_out;
3402 ssl->transform_out = ssl->handshake->alt_transform_out;
3403 ssl->handshake->alt_transform_out = tmp_transform;
3404
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003405 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003406 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3407 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003408 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003409
3410 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003411 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3414 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3419 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003420 }
3421 }
3422#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003423}
3424
3425/*
3426 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003427 */
3428int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3429{
3430 int ret = 0;
3431
3432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3433
3434 ret = mbedtls_ssl_flight_transmit( ssl );
3435
3436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3437
3438 return( ret );
3439}
3440
3441/*
3442 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003443 *
3444 * Need to remember the current message in case flush_output returns
3445 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003446 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003447 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003448int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003449{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003450 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003454 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003456
3457 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003458 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459 ssl_swap_epochs( ssl );
3460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003462 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003463
3464 while( ssl->handshake->cur_msg != NULL )
3465 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003466 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003467 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003468
Hanno Beckere1dcb032018-08-17 16:47:58 +01003469 int const is_finished =
3470 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3471 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3472
Hanno Becker04da1892018-08-14 13:22:10 +01003473 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3474 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3475
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003476 /* Swap epochs before sending Finished: we can't do it after
3477 * sending ChangeCipherSpec, in case write returns WANT_READ.
3478 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003479 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003480 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003482 ssl_swap_epochs( ssl );
3483 }
3484
Hanno Becker67bc7c32018-08-06 11:33:50 +01003485 ret = ssl_get_remaining_payload_in_datagram( ssl );
3486 if( ret < 0 )
3487 return( ret );
3488 max_frag_len = (size_t) ret;
3489
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003490 /* CCS is copied as is, while HS messages may need fragmentation */
3491 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3492 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003493 if( max_frag_len == 0 )
3494 {
3495 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3496 return( ret );
3497
3498 continue;
3499 }
3500
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003501 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003502 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003503 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003504
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003505 /* Update position inside current message */
3506 ssl->handshake->cur_msg_p += cur->len;
3507 }
3508 else
3509 {
3510 const unsigned char * const p = ssl->handshake->cur_msg_p;
3511 const size_t hs_len = cur->len - 12;
3512 const size_t frag_off = p - ( cur->p + 12 );
3513 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003514 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003515
Hanno Beckere1dcb032018-08-17 16:47:58 +01003516 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003517 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003518 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003519 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003520
Hanno Becker67bc7c32018-08-06 11:33:50 +01003521 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3522 return( ret );
3523
3524 continue;
3525 }
3526 max_hs_frag_len = max_frag_len - 12;
3527
3528 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3529 max_hs_frag_len : rem_len;
3530
3531 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003532 {
3533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003534 (unsigned) cur_hs_frag_len,
3535 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003536 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003537
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003538 /* Messages are stored with handshake headers as if not fragmented,
3539 * copy beginning of headers then fill fragmentation fields.
3540 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3541 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003542
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003543 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3544 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3545 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3546
Hanno Becker67bc7c32018-08-06 11:33:50 +01003547 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3548 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3549 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003550
3551 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3552
Hanno Becker3f7b9732018-08-28 09:53:25 +01003553 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003554 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3555 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003556 ssl->out_msgtype = cur->type;
3557
3558 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003559 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003560 }
3561
3562 /* If done with the current message move to the next one if any */
3563 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3564 {
3565 if( cur->next != NULL )
3566 {
3567 ssl->handshake->cur_msg = cur->next;
3568 ssl->handshake->cur_msg_p = cur->next->p + 12;
3569 }
3570 else
3571 {
3572 ssl->handshake->cur_msg = NULL;
3573 ssl->handshake->cur_msg_p = NULL;
3574 }
3575 }
3576
3577 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003578 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581 return( ret );
3582 }
3583 }
3584
Hanno Becker67bc7c32018-08-06 11:33:50 +01003585 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3586 return( ret );
3587
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003588 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3590 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003591 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003594 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3595 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003596
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598
3599 return( 0 );
3600}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003601
3602/*
3603 * To be called when the last message of an incoming flight is received.
3604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003606{
3607 /* We won't need to resend that one any more */
3608 ssl_flight_free( ssl->handshake->flight );
3609 ssl->handshake->flight = NULL;
3610 ssl->handshake->cur_msg = NULL;
3611
3612 /* The next incoming flight will start with this msg_seq */
3613 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3614
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003615 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003616 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003617
Hanno Becker0271f962018-08-16 13:23:47 +01003618 /* Clear future message buffering structure. */
3619 ssl_buffering_free( ssl );
3620
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003621 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003622 ssl_set_timer( ssl, 0 );
3623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3625 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003628 }
3629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003631}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003632
3633/*
3634 * To be called when the last message of an outgoing flight is send.
3635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003637{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003638 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003639 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3642 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003645 }
3646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003650
Paul Bakker5121ce52009-01-03 21:22:43 +00003651/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003652 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003654
3655/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003656 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657 *
3658 * - fill in handshake headers
3659 * - update handshake checksum
3660 * - DTLS: save message for resending
3661 * - then pass to the record layer
3662 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003663 * DTLS: except for HelloRequest, messages are only queued, and will only be
3664 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003665 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003666 * Inputs:
3667 * - ssl->out_msglen: 4 + actual handshake message len
3668 * (4 is the size of handshake headers for TLS)
3669 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3670 * - ssl->out_msg + 4: the handshake message body
3671 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003672 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003673 * - ssl->out_msglen: the length of the record contents
3674 * (including handshake headers but excluding record headers)
3675 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003676 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003677int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003678{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003679 int ret;
3680 const size_t hs_len = ssl->out_msglen - 4;
3681 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3684
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003685 /*
3686 * Sanity checks
3687 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003688 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003689 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3690 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003691 /* In SSLv3, the client might send a NoCertificate alert. */
3692#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3693 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3694 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3695 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3696#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3697 {
3698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3699 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3700 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003702
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003703 /* Whenever we send anything different from a
3704 * HelloRequest we should be in a handshake - double check. */
3705 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3706 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003707 ssl->handshake == NULL )
3708 {
3709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3710 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003714 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003715 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003717 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003721#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003722
Hanno Beckerb50a2532018-08-06 11:52:54 +01003723 /* Double-check that we did not exceed the bounds
3724 * of the outgoing record buffer.
3725 * This should never fail as the various message
3726 * writing functions must obey the bounds of the
3727 * outgoing record buffer, but better be safe.
3728 *
3729 * Note: We deliberately do not check for the MTU or MFL here.
3730 */
3731 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3732 {
3733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3734 "size %u, maximum %u",
3735 (unsigned) ssl->out_msglen,
3736 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3738 }
3739
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003740 /*
3741 * Fill handshake headers
3742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003744 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003745 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3746 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3747 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003748
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003749 /*
3750 * DTLS has additional fields in the Handshake layer,
3751 * between the length field and the actual payload:
3752 * uint16 message_seq;
3753 * uint24 fragment_offset;
3754 * uint24 fragment_length;
3755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003757 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003758 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003759 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003760 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003761 {
3762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3763 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003764 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003765 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3767 }
3768
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003770 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003771
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003772 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003773 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003774 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003775 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3776 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3777 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003778 }
3779 else
3780 {
3781 ssl->out_msg[4] = 0;
3782 ssl->out_msg[5] = 0;
3783 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003784
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003785 /* Handshake hashes are computed without fragmentation,
3786 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003787 memset( ssl->out_msg + 6, 0x00, 3 );
3788 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003789 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003791
Hanno Becker0207e532018-08-28 10:28:28 +01003792 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003793 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3794 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 }
3796
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003797 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003799 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003800 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3801 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003802 {
3803 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003806 return( ret );
3807 }
3808 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003809 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003810#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003811 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003812 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003813 {
3814 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3815 return( ret );
3816 }
3817 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003818
3819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3820
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003821 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003822}
3823
3824/*
3825 * Record layer functions
3826 */
3827
3828/*
3829 * Write current record.
3830 *
3831 * Uses:
3832 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3833 * - ssl->out_msglen: length of the record content (excl headers)
3834 * - ssl->out_msg: record content
3835 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003836int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003837{
3838 int ret, done = 0;
3839 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003840 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003841
3842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003845 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003847 {
3848 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003851 return( ret );
3852 }
3853
3854 len = ssl->out_msglen;
3855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3859 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 ret = mbedtls_ssl_hw_record_write( ssl );
3864 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3867 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003868 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003869
3870 if( ret == 0 )
3871 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003872 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003874 if( !done )
3875 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003876 unsigned i;
3877 size_t protected_record_size;
3878
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003879 /* Skip writing the record content type to after the encryption,
3880 * as it may change when using the CID extension. */
3881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003883 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003884
Hanno Becker19859472018-08-06 09:40:20 +01003885 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003886 ssl->out_len[0] = (unsigned char)( len >> 8 );
3887 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003888
Paul Bakker48916f92012-09-16 19:57:18 +00003889 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003890 {
Hanno Becker3307b532017-12-27 21:37:21 +00003891 mbedtls_record rec;
3892
3893 rec.buf = ssl->out_iv;
3894 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3895 ( ssl->out_iv - ssl->out_buf );
3896 rec.data_len = ssl->out_msglen;
3897 rec.data_offset = ssl->out_msg - rec.buf;
3898
3899 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3900 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3901 ssl->conf->transport, rec.ver );
3902 rec.type = ssl->out_msgtype;
3903
Hanno Beckera5a2b082019-05-15 14:03:01 +01003904#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003905 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003906 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003907#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003908
Hanno Becker611a83b2018-01-03 14:27:32 +00003909 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003910 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003913 return( ret );
3914 }
3915
Hanno Becker3307b532017-12-27 21:37:21 +00003916 if( rec.data_offset != 0 )
3917 {
3918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3920 }
3921
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003922 /* Update the record content type and CID. */
3923 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003924#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003925 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003926#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003927 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003928 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3929 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003930 }
3931
Hanno Becker43395762019-05-03 14:46:38 +01003932 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003933
3934#if defined(MBEDTLS_SSL_PROTO_DTLS)
3935 /* In case of DTLS, double-check that we don't exceed
3936 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003937 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003938 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003939 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003940 if( ret < 0 )
3941 return( ret );
3942
3943 if( protected_record_size > (size_t) ret )
3944 {
3945 /* Should never happen */
3946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3947 }
3948 }
3949#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003950
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003951 /* Now write the potentially updated record content type. */
3952 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003955 "version = [%d:%d], msglen = %d",
3956 ssl->out_hdr[0], ssl->out_hdr[1],
3957 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003960 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003961
3962 ssl->out_left += protected_record_size;
3963 ssl->out_hdr += protected_record_size;
3964 ssl_update_out_pointers( ssl, ssl->transform_out );
3965
Hanno Becker04484622018-08-06 09:49:38 +01003966 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3967 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3968 break;
3969
3970 /* The loop goes to its end iff the counter is wrapping */
3971 if( i == ssl_ep_len( ssl ) )
3972 {
3973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3974 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3975 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003976 }
3977
Hanno Becker67bc7c32018-08-06 11:33:50 +01003978#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003979 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003980 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003981 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003982 size_t remaining;
3983 ret = ssl_get_remaining_payload_in_datagram( ssl );
3984 if( ret < 0 )
3985 {
3986 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3987 ret );
3988 return( ret );
3989 }
3990
3991 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003992 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003993 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003994 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003995 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003996 else
3997 {
Hanno Becker513815a2018-08-20 11:56:09 +01003998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003999 }
4000 }
4001#endif /* MBEDTLS_SSL_PROTO_DTLS */
4002
4003 if( ( flush == SSL_FORCE_FLUSH ) &&
4004 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 return( ret );
4008 }
4009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004011
4012 return( 0 );
4013}
4014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004016
4017static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4018{
4019 if( ssl->in_msglen < ssl->in_hslen ||
4020 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4021 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4022 {
4023 return( 1 );
4024 }
4025 return( 0 );
4026}
Hanno Becker44650b72018-08-16 12:51:11 +01004027
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004028static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004029{
4030 return( ( ssl->in_msg[9] << 16 ) |
4031 ( ssl->in_msg[10] << 8 ) |
4032 ssl->in_msg[11] );
4033}
4034
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004035static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004036{
4037 return( ( ssl->in_msg[6] << 16 ) |
4038 ( ssl->in_msg[7] << 8 ) |
4039 ssl->in_msg[8] );
4040}
4041
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004042static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004043{
4044 uint32_t msg_len, frag_off, frag_len;
4045
4046 msg_len = ssl_get_hs_total_len( ssl );
4047 frag_off = ssl_get_hs_frag_off( ssl );
4048 frag_len = ssl_get_hs_frag_len( ssl );
4049
4050 if( frag_off > msg_len )
4051 return( -1 );
4052
4053 if( frag_len > msg_len - frag_off )
4054 return( -1 );
4055
4056 if( frag_len + 12 > ssl->in_msglen )
4057 return( -1 );
4058
4059 return( 0 );
4060}
4061
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004062/*
4063 * Mark bits in bitmask (used for DTLS HS reassembly)
4064 */
4065static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4066{
4067 unsigned int start_bits, end_bits;
4068
4069 start_bits = 8 - ( offset % 8 );
4070 if( start_bits != 8 )
4071 {
4072 size_t first_byte_idx = offset / 8;
4073
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004074 /* Special case */
4075 if( len <= start_bits )
4076 {
4077 for( ; len != 0; len-- )
4078 mask[first_byte_idx] |= 1 << ( start_bits - len );
4079
4080 /* Avoid potential issues with offset or len becoming invalid */
4081 return;
4082 }
4083
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004084 offset += start_bits; /* Now offset % 8 == 0 */
4085 len -= start_bits;
4086
4087 for( ; start_bits != 0; start_bits-- )
4088 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4089 }
4090
4091 end_bits = len % 8;
4092 if( end_bits != 0 )
4093 {
4094 size_t last_byte_idx = ( offset + len ) / 8;
4095
4096 len -= end_bits; /* Now len % 8 == 0 */
4097
4098 for( ; end_bits != 0; end_bits-- )
4099 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4100 }
4101
4102 memset( mask + offset / 8, 0xFF, len / 8 );
4103}
4104
4105/*
4106 * Check that bitmask is full
4107 */
4108static int ssl_bitmask_check( unsigned char *mask, size_t len )
4109{
4110 size_t i;
4111
4112 for( i = 0; i < len / 8; i++ )
4113 if( mask[i] != 0xFF )
4114 return( -1 );
4115
4116 for( i = 0; i < len % 8; i++ )
4117 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4118 return( -1 );
4119
4120 return( 0 );
4121}
4122
Hanno Becker56e205e2018-08-16 09:06:12 +01004123/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004124static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004125 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004126{
Hanno Becker56e205e2018-08-16 09:06:12 +01004127 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004128
Hanno Becker56e205e2018-08-16 09:06:12 +01004129 alloc_len = 12; /* Handshake header */
4130 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004131
Hanno Beckerd07df862018-08-16 09:14:58 +01004132 if( add_bitmap )
4133 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004134
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004135 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004136}
Hanno Becker56e205e2018-08-16 09:06:12 +01004137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004139
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004140static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004141{
4142 return( ( ssl->in_msg[1] << 16 ) |
4143 ( ssl->in_msg[2] << 8 ) |
4144 ssl->in_msg[3] );
4145}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004146
Simon Butcher99000142016-10-13 17:21:01 +01004147int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004148{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004152 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004154 }
4155
Hanno Becker12555c62018-08-16 12:47:53 +01004156 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004159 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004160 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004163 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004164 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004165 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004166 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004167
Hanno Becker44650b72018-08-16 12:51:11 +01004168 if( ssl_check_hs_header( ssl ) != 0 )
4169 {
4170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4172 }
4173
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004174 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004175 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4176 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4177 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4178 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004179 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004180 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4181 {
4182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4183 recv_msg_seq,
4184 ssl->handshake->in_msg_seq ) );
4185 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4186 }
4187
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004188 /* Retransmit only on last message from previous flight, to avoid
4189 * too many retransmissions.
4190 * Besides, No sane server ever retransmits HelloVerifyRequest */
4191 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004195 "message_seq = %d, start_of_flight = %d",
4196 recv_msg_seq,
4197 ssl->handshake->in_flight_start_seq ) );
4198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004202 return( ret );
4203 }
4204 }
4205 else
4206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004208 "message_seq = %d, expected = %d",
4209 recv_msg_seq,
4210 ssl->handshake->in_msg_seq ) );
4211 }
4212
Hanno Becker90333da2017-10-10 11:27:13 +01004213 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004214 }
4215 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004216
Hanno Becker6d97ef52018-08-16 13:09:04 +01004217 /* Message reassembly is handled alongside buffering of future
4218 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004219 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004220 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004221 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004224 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004225 }
4226 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004227 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004229#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004230 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004231 /* With TLS we don't handle fragmentation (for now) */
4232 if( ssl->in_msglen < ssl->in_hslen )
4233 {
4234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4235 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4236 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004237 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004238#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004239
Simon Butcher99000142016-10-13 17:21:01 +01004240 return( 0 );
4241}
4242
4243void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4244{
Hanno Becker0271f962018-08-16 13:23:47 +01004245 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004246
Hanno Becker0271f962018-08-16 13:23:47 +01004247 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004248 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004249 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004250 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004251
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004252 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004254 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004255 ssl->handshake != NULL )
4256 {
Hanno Becker0271f962018-08-16 13:23:47 +01004257 unsigned offset;
4258 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004259
Hanno Becker0271f962018-08-16 13:23:47 +01004260 /* Increment handshake sequence number */
4261 hs->in_msg_seq++;
4262
4263 /*
4264 * Clear up handshake buffering and reassembly structure.
4265 */
4266
4267 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004268 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004269
4270 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004271 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4272 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004273 offset++, hs_buf++ )
4274 {
4275 *hs_buf = *(hs_buf + 1);
4276 }
4277
4278 /* Create a fresh last entry */
4279 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004280 }
4281#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004282}
4283
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004284/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004285 * DTLS anti-replay: RFC 6347 4.1.2.6
4286 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004287 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4288 * Bit n is set iff record number in_window_top - n has been seen.
4289 *
4290 * Usually, in_window_top is the last record number seen and the lsb of
4291 * in_window is set. The only exception is the initial state (record number 0
4292 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4295static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004296{
4297 ssl->in_window_top = 0;
4298 ssl->in_window = 0;
4299}
4300
4301static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4302{
4303 return( ( (uint64_t) buf[0] << 40 ) |
4304 ( (uint64_t) buf[1] << 32 ) |
4305 ( (uint64_t) buf[2] << 24 ) |
4306 ( (uint64_t) buf[3] << 16 ) |
4307 ( (uint64_t) buf[4] << 8 ) |
4308 ( (uint64_t) buf[5] ) );
4309}
4310
4311/*
4312 * Return 0 if sequence number is acceptable, -1 otherwise
4313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004315{
4316 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4317 uint64_t bit;
4318
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004319 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004320 return( 0 );
4321
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004322 if( rec_seqnum > ssl->in_window_top )
4323 return( 0 );
4324
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004325 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004326
4327 if( bit >= 64 )
4328 return( -1 );
4329
4330 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4331 return( -1 );
4332
4333 return( 0 );
4334}
4335
4336/*
4337 * Update replay window on new validated record
4338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004340{
4341 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4342
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004343 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004344 return;
4345
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004346 if( rec_seqnum > ssl->in_window_top )
4347 {
4348 /* Update window_top and the contents of the window */
4349 uint64_t shift = rec_seqnum - ssl->in_window_top;
4350
4351 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004352 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004353 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004354 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004355 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004356 ssl->in_window |= 1;
4357 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004358
4359 ssl->in_window_top = rec_seqnum;
4360 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004361 else
4362 {
4363 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004364 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004365
4366 if( bit < 64 ) /* Always true, but be extra sure */
4367 ssl->in_window |= (uint64_t) 1 << bit;
4368 }
4369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004371
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004372#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004373/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004374static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4375
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004376/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004377 * Without any SSL context, check if a datagram looks like a ClientHello with
4378 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004379 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004380 *
4381 * - if cookie is valid, return 0
4382 * - if ClientHello looks superficially valid but cookie is not,
4383 * fill obuf and set olen, then
4384 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4385 * - otherwise return a specific error code
4386 */
4387static int ssl_check_dtls_clihlo_cookie(
4388 mbedtls_ssl_cookie_write_t *f_cookie_write,
4389 mbedtls_ssl_cookie_check_t *f_cookie_check,
4390 void *p_cookie,
4391 const unsigned char *cli_id, size_t cli_id_len,
4392 const unsigned char *in, size_t in_len,
4393 unsigned char *obuf, size_t buf_len, size_t *olen )
4394{
4395 size_t sid_len, cookie_len;
4396 unsigned char *p;
4397
4398 if( f_cookie_write == NULL || f_cookie_check == NULL )
4399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4400
4401 /*
4402 * Structure of ClientHello with record and handshake headers,
4403 * and expected values. We don't need to check a lot, more checks will be
4404 * done when actually parsing the ClientHello - skipping those checks
4405 * avoids code duplication and does not make cookie forging any easier.
4406 *
4407 * 0-0 ContentType type; copied, must be handshake
4408 * 1-2 ProtocolVersion version; copied
4409 * 3-4 uint16 epoch; copied, must be 0
4410 * 5-10 uint48 sequence_number; copied
4411 * 11-12 uint16 length; (ignored)
4412 *
4413 * 13-13 HandshakeType msg_type; (ignored)
4414 * 14-16 uint24 length; (ignored)
4415 * 17-18 uint16 message_seq; copied
4416 * 19-21 uint24 fragment_offset; copied, must be 0
4417 * 22-24 uint24 fragment_length; (ignored)
4418 *
4419 * 25-26 ProtocolVersion client_version; (ignored)
4420 * 27-58 Random random; (ignored)
4421 * 59-xx SessionID session_id; 1 byte len + sid_len content
4422 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4423 * ...
4424 *
4425 * Minimum length is 61 bytes.
4426 */
4427 if( in_len < 61 ||
4428 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4429 in[3] != 0 || in[4] != 0 ||
4430 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4431 {
4432 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4433 }
4434
4435 sid_len = in[59];
4436 if( sid_len > in_len - 61 )
4437 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4438
4439 cookie_len = in[60 + sid_len];
4440 if( cookie_len > in_len - 60 )
4441 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4442
4443 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4444 cli_id, cli_id_len ) == 0 )
4445 {
4446 /* Valid cookie */
4447 return( 0 );
4448 }
4449
4450 /*
4451 * If we get here, we've got an invalid cookie, let's prepare HVR.
4452 *
4453 * 0-0 ContentType type; copied
4454 * 1-2 ProtocolVersion version; copied
4455 * 3-4 uint16 epoch; copied
4456 * 5-10 uint48 sequence_number; copied
4457 * 11-12 uint16 length; olen - 13
4458 *
4459 * 13-13 HandshakeType msg_type; hello_verify_request
4460 * 14-16 uint24 length; olen - 25
4461 * 17-18 uint16 message_seq; copied
4462 * 19-21 uint24 fragment_offset; copied
4463 * 22-24 uint24 fragment_length; olen - 25
4464 *
4465 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4466 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4467 *
4468 * Minimum length is 28.
4469 */
4470 if( buf_len < 28 )
4471 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4472
4473 /* Copy most fields and adapt others */
4474 memcpy( obuf, in, 25 );
4475 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4476 obuf[25] = 0xfe;
4477 obuf[26] = 0xff;
4478
4479 /* Generate and write actual cookie */
4480 p = obuf + 28;
4481 if( f_cookie_write( p_cookie,
4482 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4483 {
4484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4485 }
4486
4487 *olen = p - obuf;
4488
4489 /* Go back and fill length fields */
4490 obuf[27] = (unsigned char)( *olen - 28 );
4491
4492 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4493 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4494 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4495
4496 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4497 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4498
4499 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4500}
4501
4502/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004503 * Handle possible client reconnect with the same UDP quadruplet
4504 * (RFC 6347 Section 4.2.8).
4505 *
4506 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4507 * that looks like a ClientHello.
4508 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509 * - if the input looks like a ClientHello without cookies,
4510 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004511 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004512 * - if the input looks like a ClientHello with a valid cookie,
4513 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004514 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004515 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004516 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004517 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004518 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4519 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004520 */
4521static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4522{
4523 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004524 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004525
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004526 ret = ssl_check_dtls_clihlo_cookie(
4527 ssl->conf->f_cookie_write,
4528 ssl->conf->f_cookie_check,
4529 ssl->conf->p_cookie,
4530 ssl->cli_id, ssl->cli_id_len,
4531 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004532 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004533
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004534 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4535
4536 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004537 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004538 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004539 * If the error is permanent we'll catch it later,
4540 * if it's not, then hopefully it'll work next time. */
4541 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4542
4543 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004544 }
4545
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004546 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004547 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004548 /* Got a valid cookie, partially reset context */
4549 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4550 {
4551 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4552 return( ret );
4553 }
4554
4555 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004556 }
4557
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004558 return( ret );
4559}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004560#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004561
Hanno Becker46483f12019-05-03 13:25:54 +01004562static int ssl_check_record_type( uint8_t record_type )
4563{
4564 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4565 record_type != MBEDTLS_SSL_MSG_ALERT &&
4566 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4567 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4568 {
4569 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4570 }
4571
4572 return( 0 );
4573}
4574
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004575/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004576 * ContentType type;
4577 * ProtocolVersion version;
4578 * uint16 epoch; // DTLS only
4579 * uint48 sequence_number; // DTLS only
4580 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004581 *
4582 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004583 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004584 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4585 *
4586 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004587 * 1. proceed with the record if this function returns 0
4588 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4589 * 3. return CLIENT_RECONNECT if this function return that value
4590 * 4. drop the whole datagram if this function returns anything else.
4591 * Point 2 is needed when the peer is resending, and we have already received
4592 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004595{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004596 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004597 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004598
Hanno Becker8b09b732019-05-08 12:03:28 +01004599 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004600
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004602 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004603
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004604 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004605#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004606 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004607 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4608 ssl->conf->cid_len != 0 )
4609 {
4610 /* Shift pointers to account for record header including CID
4611 * struct {
4612 * ContentType special_type = tls12_cid;
4613 * ProtocolVersion version;
4614 * uint16 epoch;
4615 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004616 * opaque cid[cid_length]; // Additional field compared to
4617 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004618 * uint16 length;
4619 * opaque enc_content[DTLSCiphertext.length];
4620 * } DTLSCiphertext;
4621 */
4622
4623 /* So far, we only support static CID lengths
4624 * fixed in the configuration. */
4625 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4626 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4627 }
4628 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004629#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004630 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004633
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004634#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004635 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004636 * Section 4.1.2.7, that is, send alert only with TLS */
4637 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4638 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004639 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4640 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004641 }
4642#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004645 }
4646
4647 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004648 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4651 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 }
4653
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004654 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4657 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 }
4659
Hanno Becker8b09b732019-05-08 12:03:28 +01004660 /* Now that the total length of the record header is known, ensure
4661 * that the current datagram is large enough to hold it.
4662 * This would fail, for example, if we received a datagram of
4663 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4664 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4665 if( ret != 0 )
4666 {
4667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4668 return( ret );
4669 }
4670 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4671
4672 /* Parse and validate record length
4673 * This must happen after the CID parsing because
4674 * its position in the record header depends on
4675 * the presence of a CID. */
4676
4677 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004678 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004679 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4682 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004683 }
4684
Hanno Becker8b09b732019-05-08 12:03:28 +01004685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004686 "version = [%d:%d], msglen = %d",
4687 ssl->in_msgtype,
4688 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004689
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004690 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004691 * DTLS-related tests.
4692 * Check epoch before checking length constraint because
4693 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4694 * message gets duplicated before the corresponding Finished message,
4695 * the second ChangeCipherSpec should be discarded because it belongs
4696 * to an old epoch, but not because its length is shorter than
4697 * the minimum record length for packets using the new record transform.
4698 * Note that these two kinds of failures are handled differently,
4699 * as an unexpected record is silently skipped but an invalid
4700 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004701 */
4702#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004703 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004704 {
4705 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4706
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004707 /* Check epoch (and sequence number) with DTLS */
4708 if( rec_epoch != ssl->in_epoch )
4709 {
4710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4711 "expected %d, received %d",
4712 ssl->in_epoch, rec_epoch ) );
4713
4714#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4715 /*
4716 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4717 * access the first byte of record content (handshake type), as we
4718 * have an active transform (possibly iv_len != 0), so use the
4719 * fact that the record header len is 13 instead.
4720 */
4721 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4722 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4723 rec_epoch == 0 &&
4724 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4725 ssl->in_left > 13 &&
4726 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4727 {
4728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4729 "from the same port" ) );
4730 return( ssl_handle_possible_reconnect( ssl ) );
4731 }
4732 else
4733#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004734 {
4735 /* Consider buffering the record. */
4736 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4737 {
4738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4739 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4740 }
4741
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004742 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004743 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004744 }
4745
4746#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4747 /* Replay detection only works for the current epoch */
4748 if( rec_epoch == ssl->in_epoch &&
4749 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4750 {
4751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4752 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4753 }
4754#endif
4755 }
4756#endif /* MBEDTLS_SSL_PROTO_DTLS */
4757
Hanno Becker52c6dc62017-05-26 16:07:36 +01004758
4759 /* Check length against bounds of the current transform and version */
4760 if( ssl->transform_in == NULL )
4761 {
4762 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004763 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004764 {
4765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4766 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4767 }
4768 }
4769 else
4770 {
4771 if( ssl->in_msglen < ssl->transform_in->minlen )
4772 {
4773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4774 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4775 }
4776
4777#if defined(MBEDTLS_SSL_PROTO_SSL3)
4778 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004779 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004780 {
4781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4782 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4783 }
4784#endif
4785#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4786 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4787 /*
4788 * TLS encrypted messages can have up to 256 bytes of padding
4789 */
4790 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4791 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004792 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004793 {
4794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4795 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4796 }
4797#endif
4798 }
4799
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004800 return( 0 );
4801}
Paul Bakker5121ce52009-01-03 21:22:43 +00004802
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004803/*
4804 * If applicable, decrypt (and decompress) record content
4805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004807{
4808 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004811 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4814 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 ret = mbedtls_ssl_hw_record_read( ssl );
4819 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4822 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004823 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004824
4825 if( ret == 0 )
4826 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004827 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004829 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004831 mbedtls_record rec;
4832
4833 rec.buf = ssl->in_iv;
4834 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4835 - ( ssl->in_iv - ssl->in_buf );
4836 rec.data_len = ssl->in_msglen;
4837 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004838#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004839 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004840 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004841#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004842
4843 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4844 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4845 ssl->conf->transport, rec.ver );
4846 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004847 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4848 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004851
Hanno Beckera5a2b082019-05-15 14:03:01 +01004852#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004853 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4854 ssl->conf->ignore_unexpected_cid
4855 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4856 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004858 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004859 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004860#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004861
Paul Bakker5121ce52009-01-03 21:22:43 +00004862 return( ret );
4863 }
4864
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004865 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004866 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004867 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4868 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004869 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004870
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004871 /* The record content type may change during decryption,
4872 * so re-read it. */
4873 ssl->in_msgtype = rec.type;
4874 /* Also update the input buffer, because unfortunately
4875 * the server-side ssl_parse_client_hello() reparses the
4876 * record header when receiving a ClientHello initiating
4877 * a renegotiation. */
4878 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004879 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004880 ssl->in_msglen = rec.data_len;
4881 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4882 ssl->in_len[1] = (unsigned char)( rec.data_len );
4883
Paul Bakker5121ce52009-01-03 21:22:43 +00004884 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4885 ssl->in_msg, ssl->in_msglen );
4886
Hanno Beckera5a2b082019-05-15 14:03:01 +01004887#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004888 /* We have already checked the record content type
4889 * in ssl_parse_record_header(), failing or silently
4890 * dropping the record in the case of an unknown type.
4891 *
4892 * Since with the use of CIDs, the record content type
4893 * might change during decryption, re-check the record
4894 * content type, but treat a failure as fatal this time. */
4895 if( ssl_check_record_type( ssl->in_msgtype ) )
4896 {
4897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4898 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4899 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004900#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004901
Angus Grattond8213d02016-05-25 20:56:48 +10004902 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4905 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004906 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004907 else if( ssl->in_msglen == 0 )
4908 {
4909#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4910 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4911 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4912 {
4913 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4915 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4916 }
4917#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4918
4919 ssl->nb_zero++;
4920
4921 /*
4922 * Three or more empty messages may be a DoS attack
4923 * (excessive CPU consumption).
4924 */
4925 if( ssl->nb_zero > 3 )
4926 {
4927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004928 "messages, possible DoS attack" ) );
4929 /* Treat the records as if they were not properly authenticated,
4930 * thereby failing the connection if we see more than allowed
4931 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004932 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4933 }
4934 }
4935 else
4936 ssl->nb_zero = 0;
4937
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004938 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4939#if defined(MBEDTLS_SSL_PROTO_TLS)
4940 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004941 {
4942 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004943 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004944 if( ++ssl->in_ctr[i - 1] != 0 )
4945 break;
4946
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004947 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004948 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004949 {
4950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4951 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4952 }
4953 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004954#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004955
Paul Bakker5121ce52009-01-03 21:22:43 +00004956 }
4957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004959 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004960 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004961 {
4962 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004965 return( ret );
4966 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004967 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004971 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004974 }
4975#endif
4976
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004977 return( 0 );
4978}
4979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004981
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004982/*
4983 * Read a record.
4984 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004985 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4986 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4987 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004988 */
Hanno Becker1097b342018-08-15 14:09:41 +01004989
4990/* Helper functions for mbedtls_ssl_read_record(). */
4991static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004992static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4993static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004994
Hanno Becker327c93b2018-08-15 13:56:18 +01004995int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004996 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004997{
4998 int ret;
4999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005001
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005002 if( ssl->keep_current_message == 0 )
5003 {
5004 do {
Simon Butcher99000142016-10-13 17:21:01 +01005005
Hanno Becker26994592018-08-15 14:14:59 +01005006 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005007 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005008 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005009
Hanno Beckere74d5562018-08-15 14:26:08 +01005010 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005011 {
Hanno Becker40f50842018-08-15 14:48:01 +01005012#if defined(MBEDTLS_SSL_PROTO_DTLS)
5013 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005014
Hanno Becker40f50842018-08-15 14:48:01 +01005015 /* We only check for buffered messages if the
5016 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005017 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005018 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005019 {
Hanno Becker40f50842018-08-15 14:48:01 +01005020 if( ssl_load_buffered_message( ssl ) == 0 )
5021 have_buffered = 1;
5022 }
5023
5024 if( have_buffered == 0 )
5025#endif /* MBEDTLS_SSL_PROTO_DTLS */
5026 {
5027 ret = ssl_get_next_record( ssl );
5028 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5029 continue;
5030
5031 if( ret != 0 )
5032 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005033 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005034 return( ret );
5035 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005036 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005037 }
5038
5039 ret = mbedtls_ssl_handle_message_type( ssl );
5040
Hanno Becker40f50842018-08-15 14:48:01 +01005041#if defined(MBEDTLS_SSL_PROTO_DTLS)
5042 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5043 {
5044 /* Buffer future message */
5045 ret = ssl_buffer_message( ssl );
5046 if( ret != 0 )
5047 return( ret );
5048
5049 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5050 }
5051#endif /* MBEDTLS_SSL_PROTO_DTLS */
5052
Hanno Becker90333da2017-10-10 11:27:13 +01005053 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5054 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005055
5056 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005057 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005058 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005059 return( ret );
5060 }
5061
Hanno Becker327c93b2018-08-15 13:56:18 +01005062 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005063 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005064 {
5065 mbedtls_ssl_update_handshake_status( ssl );
5066 }
Simon Butcher99000142016-10-13 17:21:01 +01005067 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005068 else
Simon Butcher99000142016-10-13 17:21:01 +01005069 {
Hanno Becker02f59072018-08-15 14:00:24 +01005070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005071 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005072 }
5073
5074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5075
5076 return( 0 );
5077}
5078
Hanno Becker40f50842018-08-15 14:48:01 +01005079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005080static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005081{
Hanno Becker40f50842018-08-15 14:48:01 +01005082 if( ssl->in_left > ssl->next_record_offset )
5083 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005084
Hanno Becker40f50842018-08-15 14:48:01 +01005085 return( 0 );
5086}
5087
5088static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5089{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005090 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005091 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005092 int ret = 0;
5093
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005094 if( hs == NULL )
5095 return( -1 );
5096
Hanno Beckere00ae372018-08-20 09:39:42 +01005097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5098
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005099 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5100 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5101 {
5102 /* Check if we have seen a ChangeCipherSpec before.
5103 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005104 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005105 {
5106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5107 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005108 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005109 }
5110
Hanno Becker39b8bc92018-08-28 17:17:13 +01005111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005112 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5113 ssl->in_msglen = 1;
5114 ssl->in_msg[0] = 1;
5115
5116 /* As long as they are equal, the exact value doesn't matter. */
5117 ssl->in_left = 0;
5118 ssl->next_record_offset = 0;
5119
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005120 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005121 goto exit;
5122 }
Hanno Becker37f95322018-08-16 13:55:32 +01005123
Hanno Beckerb8f50142018-08-28 10:01:34 +01005124#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005125 /* Debug only */
5126 {
5127 unsigned offset;
5128 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5129 {
5130 hs_buf = &hs->buffering.hs[offset];
5131 if( hs_buf->is_valid == 1 )
5132 {
5133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5134 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005135 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005136 }
5137 }
5138 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005139#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005140
5141 /* Check if we have buffered and/or fully reassembled the
5142 * next handshake message. */
5143 hs_buf = &hs->buffering.hs[0];
5144 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5145 {
5146 /* Synthesize a record containing the buffered HS message. */
5147 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5148 ( hs_buf->data[2] << 8 ) |
5149 hs_buf->data[3];
5150
5151 /* Double-check that we haven't accidentally buffered
5152 * a message that doesn't fit into the input buffer. */
5153 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5154 {
5155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5157 }
5158
5159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5160 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5161 hs_buf->data, msg_len + 12 );
5162
5163 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5164 ssl->in_hslen = msg_len + 12;
5165 ssl->in_msglen = msg_len + 12;
5166 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5167
5168 ret = 0;
5169 goto exit;
5170 }
5171 else
5172 {
5173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5174 hs->in_msg_seq ) );
5175 }
5176
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005177 ret = -1;
5178
5179exit:
5180
5181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5182 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005183}
5184
Hanno Beckera02b0b42018-08-21 17:20:27 +01005185static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5186 size_t desired )
5187{
5188 int offset;
5189 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5191 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005192
Hanno Becker01315ea2018-08-21 17:22:17 +01005193 /* Get rid of future records epoch first, if such exist. */
5194 ssl_free_buffered_record( ssl );
5195
5196 /* Check if we have enough space available now. */
5197 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5198 hs->buffering.total_bytes_buffered ) )
5199 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005201 return( 0 );
5202 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005203
Hanno Becker4f432ad2018-08-28 10:02:32 +01005204 /* We don't have enough space to buffer the next expected handshake
5205 * message. Remove buffers used for future messages to gain space,
5206 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005207 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5208 offset >= 0; offset-- )
5209 {
5210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5211 offset ) );
5212
Hanno Beckerb309b922018-08-23 13:18:05 +01005213 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005214
5215 /* Check if we have enough space available now. */
5216 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5217 hs->buffering.total_bytes_buffered ) )
5218 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005220 return( 0 );
5221 }
5222 }
5223
5224 return( -1 );
5225}
5226
Hanno Becker40f50842018-08-15 14:48:01 +01005227static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5228{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005229 int ret = 0;
5230 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5231
5232 if( hs == NULL )
5233 return( 0 );
5234
5235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5236
5237 switch( ssl->in_msgtype )
5238 {
5239 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005241
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005242 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005243 break;
5244
5245 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005246 {
5247 unsigned recv_msg_seq_offset;
5248 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5249 mbedtls_ssl_hs_buffer *hs_buf;
5250 size_t msg_len = ssl->in_hslen - 12;
5251
5252 /* We should never receive an old handshake
5253 * message - double-check nonetheless. */
5254 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5255 {
5256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5258 }
5259
5260 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5261 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5262 {
5263 /* Silently ignore -- message too far in the future */
5264 MBEDTLS_SSL_DEBUG_MSG( 2,
5265 ( "Ignore future HS message with sequence number %u, "
5266 "buffering window %u - %u",
5267 recv_msg_seq, ssl->handshake->in_msg_seq,
5268 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5269
5270 goto exit;
5271 }
5272
5273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5274 recv_msg_seq, recv_msg_seq_offset ) );
5275
5276 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5277
5278 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005279 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005280 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005281 size_t reassembly_buf_sz;
5282
Hanno Becker37f95322018-08-16 13:55:32 +01005283 hs_buf->is_fragmented =
5284 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5285
5286 /* We copy the message back into the input buffer
5287 * after reassembly, so check that it's not too large.
5288 * This is an implementation-specific limitation
5289 * and not one from the standard, hence it is not
5290 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005291 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005292 {
5293 /* Ignore message */
5294 goto exit;
5295 }
5296
Hanno Beckere0b150f2018-08-21 15:51:03 +01005297 /* Check if we have enough space to buffer the message. */
5298 if( hs->buffering.total_bytes_buffered >
5299 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5300 {
5301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5303 }
5304
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005305 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5306 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005307
5308 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5309 hs->buffering.total_bytes_buffered ) )
5310 {
5311 if( recv_msg_seq_offset > 0 )
5312 {
5313 /* If we can't buffer a future message because
5314 * of space limitations -- ignore. */
5315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5316 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5317 (unsigned) hs->buffering.total_bytes_buffered ) );
5318 goto exit;
5319 }
Hanno Beckere1801392018-08-21 16:51:05 +01005320 else
5321 {
5322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5323 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5324 (unsigned) hs->buffering.total_bytes_buffered ) );
5325 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005326
Hanno Beckera02b0b42018-08-21 17:20:27 +01005327 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005328 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5330 (unsigned) msg_len,
5331 (unsigned) reassembly_buf_sz,
5332 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005333 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005334 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5335 goto exit;
5336 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005337 }
5338
5339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5340 msg_len ) );
5341
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005342 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5343 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005344 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005345 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005346 goto exit;
5347 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005348 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005349
5350 /* Prepare final header: copy msg_type, length and message_seq,
5351 * then add standardised fragment_offset and fragment_length */
5352 memcpy( hs_buf->data, ssl->in_msg, 6 );
5353 memset( hs_buf->data + 6, 0, 3 );
5354 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5355
5356 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005357
5358 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005359 }
5360 else
5361 {
5362 /* Make sure msg_type and length are consistent */
5363 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5364 {
5365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5366 /* Ignore */
5367 goto exit;
5368 }
5369 }
5370
Hanno Becker4422bbb2018-08-20 09:40:19 +01005371 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005372 {
5373 size_t frag_len, frag_off;
5374 unsigned char * const msg = hs_buf->data + 12;
5375
5376 /*
5377 * Check and copy current fragment
5378 */
5379
5380 /* Validation of header fields already done in
5381 * mbedtls_ssl_prepare_handshake_record(). */
5382 frag_off = ssl_get_hs_frag_off( ssl );
5383 frag_len = ssl_get_hs_frag_len( ssl );
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5386 frag_off, frag_len ) );
5387 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5388
5389 if( hs_buf->is_fragmented )
5390 {
5391 unsigned char * const bitmask = msg + msg_len;
5392 ssl_bitmask_set( bitmask, frag_off, frag_len );
5393 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5394 msg_len ) == 0 );
5395 }
5396 else
5397 {
5398 hs_buf->is_complete = 1;
5399 }
5400
5401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5402 hs_buf->is_complete ? "" : "not yet " ) );
5403 }
5404
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005405 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005406 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005407
5408 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005409 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005410 break;
5411 }
5412
5413exit:
5414
5415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5416 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005417}
5418#endif /* MBEDTLS_SSL_PROTO_DTLS */
5419
Hanno Becker1097b342018-08-15 14:09:41 +01005420static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005421{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005422 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005423 * Consume last content-layer message and potentially
5424 * update in_msglen which keeps track of the contents'
5425 * consumption state.
5426 *
5427 * (1) Handshake messages:
5428 * Remove last handshake message, move content
5429 * and adapt in_msglen.
5430 *
5431 * (2) Alert messages:
5432 * Consume whole record content, in_msglen = 0.
5433 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005434 * (3) Change cipher spec:
5435 * Consume whole record content, in_msglen = 0.
5436 *
5437 * (4) Application data:
5438 * Don't do anything - the record layer provides
5439 * the application data as a stream transport
5440 * and consumes through mbedtls_ssl_read only.
5441 *
5442 */
5443
5444 /* Case (1): Handshake messages */
5445 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005446 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005447 /* Hard assertion to be sure that no application data
5448 * is in flight, as corrupting ssl->in_msglen during
5449 * ssl->in_offt != NULL is fatal. */
5450 if( ssl->in_offt != NULL )
5451 {
5452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5454 }
5455
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005456 /*
5457 * Get next Handshake message in the current record
5458 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459
Hanno Becker4a810fb2017-05-24 16:27:30 +01005460 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005461 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005462 * current handshake content: If DTLS handshake
5463 * fragmentation is used, that's the fragment
5464 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005465 * size here is faulty and should be changed at
5466 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005467 * (2) While it doesn't seem to cause problems, one
5468 * has to be very careful not to assume that in_hslen
5469 * is always <= in_msglen in a sensible communication.
5470 * Again, it's wrong for DTLS handshake fragmentation.
5471 * The following check is therefore mandatory, and
5472 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005473 * Additionally, ssl->in_hslen might be arbitrarily out of
5474 * bounds after handling a DTLS message with an unexpected
5475 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005476 */
5477 if( ssl->in_hslen < ssl->in_msglen )
5478 {
5479 ssl->in_msglen -= ssl->in_hslen;
5480 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5481 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005482
Hanno Becker4a810fb2017-05-24 16:27:30 +01005483 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5484 ssl->in_msg, ssl->in_msglen );
5485 }
5486 else
5487 {
5488 ssl->in_msglen = 0;
5489 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005490
Hanno Becker4a810fb2017-05-24 16:27:30 +01005491 ssl->in_hslen = 0;
5492 }
5493 /* Case (4): Application data */
5494 else if( ssl->in_offt != NULL )
5495 {
5496 return( 0 );
5497 }
5498 /* Everything else (CCS & Alerts) */
5499 else
5500 {
5501 ssl->in_msglen = 0;
5502 }
5503
Hanno Becker1097b342018-08-15 14:09:41 +01005504 return( 0 );
5505}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005506
Hanno Beckere74d5562018-08-15 14:26:08 +01005507static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5508{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005509 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005510 return( 1 );
5511
5512 return( 0 );
5513}
5514
Hanno Becker5f066e72018-08-16 14:56:31 +01005515#if defined(MBEDTLS_SSL_PROTO_DTLS)
5516
5517static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5518{
5519 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5520 if( hs == NULL )
5521 return;
5522
Hanno Becker01315ea2018-08-21 17:22:17 +01005523 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005524 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005525 hs->buffering.total_bytes_buffered -=
5526 hs->buffering.future_record.len;
5527
5528 mbedtls_free( hs->buffering.future_record.data );
5529 hs->buffering.future_record.data = NULL;
5530 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005531}
5532
5533static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5534{
5535 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5536 unsigned char * rec;
5537 size_t rec_len;
5538 unsigned rec_epoch;
5539
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005540 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005541 return( 0 );
5542
5543 if( hs == NULL )
5544 return( 0 );
5545
Hanno Becker5f066e72018-08-16 14:56:31 +01005546 rec = hs->buffering.future_record.data;
5547 rec_len = hs->buffering.future_record.len;
5548 rec_epoch = hs->buffering.future_record.epoch;
5549
5550 if( rec == NULL )
5551 return( 0 );
5552
Hanno Becker4cb782d2018-08-20 11:19:05 +01005553 /* Only consider loading future records if the
5554 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005555 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005556 return( 0 );
5557
Hanno Becker5f066e72018-08-16 14:56:31 +01005558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5559
5560 if( rec_epoch != ssl->in_epoch )
5561 {
5562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5563 goto exit;
5564 }
5565
5566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5567
5568 /* Double-check that the record is not too large */
5569 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5570 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5571 {
5572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5574 }
5575
5576 memcpy( ssl->in_hdr, rec, rec_len );
5577 ssl->in_left = rec_len;
5578 ssl->next_record_offset = 0;
5579
5580 ssl_free_buffered_record( ssl );
5581
5582exit:
5583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5584 return( 0 );
5585}
5586
5587static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5588{
5589 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5590 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005591 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005592
5593 /* Don't buffer future records outside handshakes. */
5594 if( hs == NULL )
5595 return( 0 );
5596
5597 /* Only buffer handshake records (we are only interested
5598 * in Finished messages). */
5599 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5600 return( 0 );
5601
5602 /* Don't buffer more than one future epoch record. */
5603 if( hs->buffering.future_record.data != NULL )
5604 return( 0 );
5605
Hanno Becker01315ea2018-08-21 17:22:17 +01005606 /* Don't buffer record if there's not enough buffering space remaining. */
5607 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5608 hs->buffering.total_bytes_buffered ) )
5609 {
5610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5611 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5612 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005613 return( 0 );
5614 }
5615
Hanno Becker5f066e72018-08-16 14:56:31 +01005616 /* Buffer record */
5617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5618 ssl->in_epoch + 1 ) );
5619 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5620 rec_hdr_len + ssl->in_msglen );
5621
5622 /* ssl_parse_record_header() only considers records
5623 * of the next epoch as candidates for buffering. */
5624 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005625 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005626
5627 hs->buffering.future_record.data =
5628 mbedtls_calloc( 1, hs->buffering.future_record.len );
5629 if( hs->buffering.future_record.data == NULL )
5630 {
5631 /* If we run out of RAM trying to buffer a
5632 * record from the next epoch, just ignore. */
5633 return( 0 );
5634 }
5635
Hanno Becker01315ea2018-08-21 17:22:17 +01005636 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005637
Hanno Becker01315ea2018-08-21 17:22:17 +01005638 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005639 return( 0 );
5640}
5641
5642#endif /* MBEDTLS_SSL_PROTO_DTLS */
5643
Hanno Beckere74d5562018-08-15 14:26:08 +01005644static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005645{
5646 int ret;
5647
Hanno Becker5f066e72018-08-16 14:56:31 +01005648#if defined(MBEDTLS_SSL_PROTO_DTLS)
5649 /* We might have buffered a future record; if so,
5650 * and if the epoch matches now, load it.
5651 * On success, this call will set ssl->in_left to
5652 * the length of the buffered record, so that
5653 * the calls to ssl_fetch_input() below will
5654 * essentially be no-ops. */
5655 ret = ssl_load_buffered_record( ssl );
5656 if( ret != 0 )
5657 return( ret );
5658#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005659
Hanno Becker8b09b732019-05-08 12:03:28 +01005660 /* Reset in pointers to default state for TLS/DTLS records,
5661 * assuming no CID and no offset between record content and
5662 * record plaintext. */
5663 ssl_update_in_pointers( ssl );
5664
5665 /* Ensure that we have enough space available for the default form
5666 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5667 * with no space for CIDs counted in). */
5668 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5669 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005672 return( ret );
5673 }
5674
5675 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005678 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005679 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005680 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005681 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5682 {
5683 ret = ssl_buffer_future_record( ssl );
5684 if( ret != 0 )
5685 return( ret );
5686
5687 /* Fall through to handling of unexpected records */
5688 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5689 }
5690
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005691 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5692 {
5693 /* Skip unexpected record (but not whole datagram) */
5694 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005695 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005696
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5698 "(header)" ) );
5699 }
5700 else
5701 {
5702 /* Skip invalid record and the rest of the datagram */
5703 ssl->next_record_offset = 0;
5704 ssl->in_left = 0;
5705
5706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5707 "(header)" ) );
5708 }
5709
5710 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005711 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005712 }
5713#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005714 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005715 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005716
5717 /*
5718 * Read and optionally decrypt the message contents
5719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005721 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005724 return( ret );
5725 }
5726
5727 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005729 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005730 {
Hanno Becker43395762019-05-03 14:46:38 +01005731 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005732 if( ssl->next_record_offset < ssl->in_left )
5733 {
5734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5735 }
5736 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005737 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005738#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005739#if defined(MBEDTLS_SSL_PROTO_TLS)
5740 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005741 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005742 }
5743#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005744
5745 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005748 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005749 {
5750 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005751 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005752 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005753 /* Except when waiting for Finished as a bad mac here
5754 * probably means something went wrong in the handshake
5755 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5756 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5757 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5758 {
5759#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5760 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5761 {
5762 mbedtls_ssl_send_alert_message( ssl,
5763 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5764 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5765 }
5766#endif
5767 return( ret );
5768 }
5769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005771 if( ssl->conf->badmac_limit != 0 &&
5772 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5775 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005776 }
5777#endif
5778
Hanno Becker4a810fb2017-05-24 16:27:30 +01005779 /* As above, invalid records cause
5780 * dismissal of the whole datagram. */
5781
5782 ssl->next_record_offset = 0;
5783 ssl->in_left = 0;
5784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005786 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005787 }
5788
5789 return( ret );
5790 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005791 MBEDTLS_SSL_TRANSPORT_ELSE
5792#endif /* MBEDTLS_SSL_PROTO_DTLS */
5793#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005794 {
5795 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5797 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 mbedtls_ssl_send_alert_message( ssl,
5800 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5801 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005802 }
5803#endif
5804 return( ret );
5805 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005806#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005807 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005808
Simon Butcher99000142016-10-13 17:21:01 +01005809 return( 0 );
5810}
5811
5812int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5813{
5814 int ret;
5815
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005816 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005817 * Handle particular types of records
5818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005820 {
Simon Butcher99000142016-10-13 17:21:01 +01005821 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5822 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005823 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005824 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 }
5826
Hanno Beckere678eaa2018-08-21 14:57:46 +01005827 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005828 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005829 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005830 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5832 ssl->in_msglen ) );
5833 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005834 }
5835
Hanno Beckere678eaa2018-08-21 14:57:46 +01005836 if( ssl->in_msg[0] != 1 )
5837 {
5838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5839 ssl->in_msg[0] ) );
5840 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5841 }
5842
5843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005844 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005845 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5846 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5847 {
5848 if( ssl->handshake == NULL )
5849 {
5850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5851 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5852 }
5853
5854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5855 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5856 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005857#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005858 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005862 if( ssl->in_msglen != 2 )
5863 {
5864 /* Note: Standard allows for more than one 2 byte alert
5865 to be packed in a single message, but Mbed TLS doesn't
5866 currently support this. */
5867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5868 ssl->in_msglen ) );
5869 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5870 }
5871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005873 ssl->in_msg[0], ssl->in_msg[1] ) );
5874
5875 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005876 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005881 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005883 }
5884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5886 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5889 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005891
5892#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5893 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5894 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5895 {
Hanno Becker90333da2017-10-10 11:27:13 +01005896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005897 /* Will be handled when trying to parse ServerHello */
5898 return( 0 );
5899 }
5900#endif
5901
5902#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5903 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5904 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5905 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5906 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5907 {
5908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5909 /* Will be handled in mbedtls_ssl_parse_certificate() */
5910 return( 0 );
5911 }
5912#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5913
5914 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005915 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005916 }
5917
Hanno Beckerc76c6192017-06-06 10:03:17 +01005918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005919 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005920 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005921 /* Drop unexpected ApplicationData records,
5922 * except at the beginning of renegotiations */
5923 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5924 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5925#if defined(MBEDTLS_SSL_RENEGOTIATION)
5926 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5927 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005928#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005929 )
5930 {
5931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5932 return( MBEDTLS_ERR_SSL_NON_FATAL );
5933 }
5934
5935 if( ssl->handshake != NULL &&
5936 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5937 {
5938 ssl_handshake_wrapup_free_hs_transform( ssl );
5939 }
5940 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005941#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005942
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 return( 0 );
5944}
5945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005947{
5948 int ret;
5949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5951 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5952 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005953 {
5954 return( ret );
5955 }
5956
5957 return( 0 );
5958}
5959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005961 unsigned char level,
5962 unsigned char message )
5963{
5964 int ret;
5965
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005966 if( ssl == NULL || ssl->conf == NULL )
5967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005970 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005973 ssl->out_msglen = 2;
5974 ssl->out_msg[0] = level;
5975 ssl->out_msg[1] = message;
5976
Hanno Becker67bc7c32018-08-06 11:33:50 +01005977 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005980 return( ret );
5981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005983
5984 return( 0 );
5985}
5986
Hanno Becker17572472019-02-08 07:19:04 +00005987#if defined(MBEDTLS_X509_CRT_PARSE_C)
5988static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5989{
5990#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5991 if( session->peer_cert != NULL )
5992 {
5993 mbedtls_x509_crt_free( session->peer_cert );
5994 mbedtls_free( session->peer_cert );
5995 session->peer_cert = NULL;
5996 }
Hanno Becker5882dd02019-06-06 16:25:57 +01005997#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00005998 if( session->peer_cert_digest != NULL )
5999 {
6000 /* Zeroization is not necessary. */
6001 mbedtls_free( session->peer_cert_digest );
6002 session->peer_cert_digest = NULL;
6003 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6004 session->peer_cert_digest_len = 0;
6005 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006006#else
6007 ((void) session);
6008#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006009}
6010#endif /* MBEDTLS_X509_CRT_PARSE_C */
6011
Paul Bakker5121ce52009-01-03 21:22:43 +00006012/*
6013 * Handshake functions
6014 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006015#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006016/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006018{
Hanno Becker8759e162017-12-27 21:34:08 +00006019 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006022
Hanno Becker5097cba2019-02-05 13:36:46 +00006023 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006026 ssl->state++;
6027 return( 0 );
6028 }
6029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006032}
6033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006035{
Hanno Becker8759e162017-12-27 21:34:08 +00006036 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006039
Hanno Becker5097cba2019-02-05 13:36:46 +00006040 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006043 ssl->state++;
6044 return( 0 );
6045 }
6046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006049}
Gilles Peskinef9828522017-05-03 12:28:43 +02006050
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006051#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006052/* Some certificate support -> implement write and parse */
6053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006057 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006059 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006062
Hanno Becker5097cba2019-02-05 13:36:46 +00006063 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006066 ssl->state++;
6067 return( 0 );
6068 }
6069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006071 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006072 {
6073 if( ssl->client_auth == 0 )
6074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 ssl->state++;
6077 return( 0 );
6078 }
6079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006081 /*
6082 * If using SSLv3 and got no cert, send an Alert message
6083 * (otherwise an empty Certificate message will be sent).
6084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6086 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006087 {
6088 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6090 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6091 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006094 goto write_msg;
6095 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006097 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098#endif /* MBEDTLS_SSL_CLI_C */
6099#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006100 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6105 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 }
6107 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006108#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006111
6112 /*
6113 * 0 . 0 handshake type
6114 * 1 . 3 handshake length
6115 * 4 . 6 length of all certs
6116 * 7 . 9 length of cert. 1
6117 * 10 . n-1 peer certificate
6118 * n . n+2 length of cert. 2
6119 * n+3 . ... upper level cert, etc.
6120 */
6121 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006123
Paul Bakker29087132010-03-21 21:03:34 +00006124 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006125 {
6126 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006127 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006130 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006132 }
6133
6134 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6135 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6136 ssl->out_msg[i + 2] = (unsigned char)( n );
6137
6138 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6139 i += n; crt = crt->next;
6140 }
6141
6142 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6143 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6144 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6145
6146 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6148 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006149
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006150#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006151write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006153
6154 ssl->state++;
6155
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006156 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006157 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006159 return( ret );
6160 }
6161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006163
Paul Bakkered27a042013-04-18 22:46:23 +02006164 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006165}
6166
Hanno Becker285ff0c2019-01-31 07:44:03 +00006167#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006168
6169#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006170static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6171 unsigned char *crt_buf,
6172 size_t crt_buf_len )
6173{
6174 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6175
6176 if( peer_crt == NULL )
6177 return( -1 );
6178
6179 if( peer_crt->raw.len != crt_buf_len )
6180 return( -1 );
6181
Hanno Becker68b856d2019-02-08 14:00:04 +00006182 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006183}
Hanno Becker5882dd02019-06-06 16:25:57 +01006184#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006185static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6186 unsigned char *crt_buf,
6187 size_t crt_buf_len )
6188{
6189 int ret;
6190 unsigned char const * const peer_cert_digest =
6191 ssl->session->peer_cert_digest;
6192 mbedtls_md_type_t const peer_cert_digest_type =
6193 ssl->session->peer_cert_digest_type;
6194 mbedtls_md_info_t const * const digest_info =
6195 mbedtls_md_info_from_type( peer_cert_digest_type );
6196 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6197 size_t digest_len;
6198
6199 if( peer_cert_digest == NULL || digest_info == NULL )
6200 return( -1 );
6201
6202 digest_len = mbedtls_md_get_size( digest_info );
6203 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6204 return( -1 );
6205
6206 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6207 if( ret != 0 )
6208 return( -1 );
6209
6210 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6211}
Hanno Becker5882dd02019-06-06 16:25:57 +01006212#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006213#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006214
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006215/*
6216 * Once the certificate message is read, parse it into a cert chain and
6217 * perform basic checks, but leave actual verification to the caller
6218 */
Hanno Becker35e41772019-02-05 15:37:23 +00006219static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6220 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006221{
Hanno Becker35e41772019-02-05 15:37:23 +00006222 int ret;
6223#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6224 int crt_cnt=0;
6225#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006226 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006227 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006232 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6233 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006235 }
6236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6238 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006241 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6242 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 }
6245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006247
Paul Bakker5121ce52009-01-03 21:22:43 +00006248 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006251 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006252
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006253 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006257 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6258 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 }
6261
Hanno Becker33c3dc82019-01-30 14:46:46 +00006262 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6263 i += 3;
6264
Hanno Becker33c3dc82019-01-30 14:46:46 +00006265 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006266 while( i < ssl->in_hslen )
6267 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006268 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006269 if ( i + 3 > ssl->in_hslen ) {
6270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006271 mbedtls_ssl_send_alert_message( ssl,
6272 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6273 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006274 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6275 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006276 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6277 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006278 if( ssl->in_msg[i] != 0 )
6279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006281 mbedtls_ssl_send_alert_message( ssl,
6282 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6283 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006285 }
6286
Hanno Becker33c3dc82019-01-30 14:46:46 +00006287 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006288 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6289 | (unsigned int) ssl->in_msg[i + 2];
6290 i += 3;
6291
6292 if( n < 128 || i + n > ssl->in_hslen )
6293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006295 mbedtls_ssl_send_alert_message( ssl,
6296 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6297 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006299 }
6300
Hanno Becker33c3dc82019-01-30 14:46:46 +00006301 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006302#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6303 if( crt_cnt++ == 0 &&
6304 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6305 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006306 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006307 /* During client-side renegotiation, check that the server's
6308 * end-CRTs hasn't changed compared to the initial handshake,
6309 * mitigating the triple handshake attack. On success, reuse
6310 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6312 if( ssl_check_peer_crt_unchanged( ssl,
6313 &ssl->in_msg[i],
6314 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006315 {
Hanno Becker35e41772019-02-05 15:37:23 +00006316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6317 mbedtls_ssl_send_alert_message( ssl,
6318 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6319 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6320 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006321 }
Hanno Becker35e41772019-02-05 15:37:23 +00006322
6323 /* Now we can safely free the original chain. */
6324 ssl_clear_peer_cert( ssl->session );
6325 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006326#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6327
Hanno Becker33c3dc82019-01-30 14:46:46 +00006328 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006329#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006330 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006331#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006332 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006333 * it in-place from the input buffer instead of making a copy. */
6334 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6335#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006336 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006337 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006338 case 0: /*ok*/
6339 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6340 /* Ignore certificate with an unknown algorithm: maybe a
6341 prior certificate was already trusted. */
6342 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006343
Hanno Becker33c3dc82019-01-30 14:46:46 +00006344 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6345 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6346 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006347
Hanno Becker33c3dc82019-01-30 14:46:46 +00006348 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6349 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6350 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006351
Hanno Becker33c3dc82019-01-30 14:46:46 +00006352 default:
6353 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6354 crt_parse_der_failed:
6355 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6356 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6357 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006358 }
6359
6360 i += n;
6361 }
6362
Hanno Becker35e41772019-02-05 15:37:23 +00006363 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006364 return( 0 );
6365}
6366
Hanno Beckerb8a08572019-02-05 12:49:06 +00006367#if defined(MBEDTLS_SSL_SRV_C)
6368static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6369{
6370 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6371 return( -1 );
6372
6373#if defined(MBEDTLS_SSL_PROTO_SSL3)
6374 /*
6375 * Check if the client sent an empty certificate
6376 */
6377 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6378 {
6379 if( ssl->in_msglen == 2 &&
6380 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6381 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6382 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6383 {
6384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6385 return( 0 );
6386 }
6387
6388 return( -1 );
6389 }
6390#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6391
6392#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6393 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6394 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6395 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6396 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6397 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6398 {
6399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6400 return( 0 );
6401 }
6402
Hanno Beckerb8a08572019-02-05 12:49:06 +00006403#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6404 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01006405
6406 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00006407}
6408#endif /* MBEDTLS_SSL_SRV_C */
6409
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006410/* Check if a certificate message is expected.
6411 * Return either
6412 * - SSL_CERTIFICATE_EXPECTED, or
6413 * - SSL_CERTIFICATE_SKIP
6414 * indicating whether a Certificate message is expected or not.
6415 */
6416#define SSL_CERTIFICATE_EXPECTED 0
6417#define SSL_CERTIFICATE_SKIP 1
6418static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6419 int authmode )
6420{
6421 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6422 ssl->handshake->ciphersuite_info;
6423
6424 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6425 return( SSL_CERTIFICATE_SKIP );
6426
6427#if defined(MBEDTLS_SSL_SRV_C)
6428 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6429 {
6430 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6431 return( SSL_CERTIFICATE_SKIP );
6432
6433 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6434 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006435 ssl->session_negotiate->verify_result =
6436 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6437 return( SSL_CERTIFICATE_SKIP );
6438 }
6439 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006440#else
6441 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006442#endif /* MBEDTLS_SSL_SRV_C */
6443
6444 return( SSL_CERTIFICATE_EXPECTED );
6445}
6446
Hanno Becker3cf50612019-02-05 14:36:34 +00006447static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6448 int authmode,
6449 mbedtls_x509_crt *chain,
6450 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006451{
Hanno Becker613d4902019-02-05 13:11:17 +00006452 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006453 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6454 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006455 mbedtls_x509_crt *ca_chain;
6456 mbedtls_x509_crl *ca_crl;
6457
6458 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6459 return( 0 );
6460
6461#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6462 if( ssl->handshake->sni_ca_chain != NULL )
6463 {
6464 ca_chain = ssl->handshake->sni_ca_chain;
6465 ca_crl = ssl->handshake->sni_ca_crl;
6466 }
6467 else
6468#endif
6469 {
6470 ca_chain = ssl->conf->ca_chain;
6471 ca_crl = ssl->conf->ca_crl;
6472 }
6473
6474 /*
6475 * Main check: verify certificate
6476 */
6477 ret = mbedtls_x509_crt_verify_restartable(
6478 chain,
6479 ca_chain, ca_crl,
6480 ssl->conf->cert_profile,
6481 ssl->hostname,
6482 &ssl->session_negotiate->verify_result,
6483 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6484
6485 if( ret != 0 )
6486 {
6487 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6488 }
6489
6490#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6491 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6492 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6493#endif
6494
6495 /*
6496 * Secondary checks: always done, but change 'ret' only if it was 0
6497 */
6498
6499#if defined(MBEDTLS_ECP_C)
6500 {
6501 const mbedtls_pk_context *pk = &chain->pk;
6502
6503 /* If certificate uses an EC key, make sure the curve is OK */
6504 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6505 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6506 {
6507 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6508
6509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6510 if( ret == 0 )
6511 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6512 }
6513 }
6514#endif /* MBEDTLS_ECP_C */
6515
6516 if( mbedtls_ssl_check_cert_usage( chain,
6517 ciphersuite_info,
6518 ! ssl->conf->endpoint,
6519 &ssl->session_negotiate->verify_result ) != 0 )
6520 {
6521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6522 if( ret == 0 )
6523 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6524 }
6525
6526 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6527 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6528 * with details encoded in the verification flags. All other kinds
6529 * of error codes, including those from the user provided f_vrfy
6530 * functions, are treated as fatal and lead to a failure of
6531 * ssl_parse_certificate even if verification was optional. */
6532 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6533 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6534 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6535 {
6536 ret = 0;
6537 }
6538
6539 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6540 {
6541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6542 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6543 }
6544
6545 if( ret != 0 )
6546 {
6547 uint8_t alert;
6548
6549 /* The certificate may have been rejected for several reasons.
6550 Pick one and send the corresponding alert. Which alert to send
6551 may be a subject of debate in some cases. */
6552 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6553 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6554 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6555 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6556 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6557 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6558 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6559 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6560 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6561 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6562 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6563 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6564 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6565 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6566 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6567 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6568 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6569 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6570 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6571 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6572 else
6573 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6574 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6575 alert );
6576 }
6577
6578#if defined(MBEDTLS_DEBUG_C)
6579 if( ssl->session_negotiate->verify_result != 0 )
6580 {
6581 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6582 ssl->session_negotiate->verify_result ) );
6583 }
6584 else
6585 {
6586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6587 }
6588#endif /* MBEDTLS_DEBUG_C */
6589
6590 return( ret );
6591}
6592
Hanno Becker34106f62019-02-08 14:59:05 +00006593#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01006594
6595#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006596static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6597 unsigned char *start, size_t len )
6598{
6599 int ret;
6600 /* Remember digest of the peer's end-CRT. */
6601 ssl->session_negotiate->peer_cert_digest =
6602 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6603 if( ssl->session_negotiate->peer_cert_digest == NULL )
6604 {
6605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6606 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6607 mbedtls_ssl_send_alert_message( ssl,
6608 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6609 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6610
6611 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6612 }
6613
6614 ret = mbedtls_md( mbedtls_md_info_from_type(
6615 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6616 start, len,
6617 ssl->session_negotiate->peer_cert_digest );
6618
6619 ssl->session_negotiate->peer_cert_digest_type =
6620 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6621 ssl->session_negotiate->peer_cert_digest_len =
6622 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6623
6624 return( ret );
6625}
Hanno Becker5882dd02019-06-06 16:25:57 +01006626#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006627
6628static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6629 unsigned char *start, size_t len )
6630{
6631 unsigned char *end = start + len;
6632 int ret;
6633
6634 /* Make a copy of the peer's raw public key. */
6635 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6636 ret = mbedtls_pk_parse_subpubkey( &start, end,
6637 &ssl->handshake->peer_pubkey );
6638 if( ret != 0 )
6639 {
6640 /* We should have parsed the public key before. */
6641 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6642 }
6643
6644 return( 0 );
6645}
6646#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6647
Hanno Becker3cf50612019-02-05 14:36:34 +00006648int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6649{
6650 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006651 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006652#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6653 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6654 ? ssl->handshake->sni_authmode
6655 : ssl->conf->authmode;
6656#else
6657 const int authmode = ssl->conf->authmode;
6658#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006659 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006660 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006661
6662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6663
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006664 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6665 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006666 {
6667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006668 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006669 }
6670
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006671#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6672 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006673 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006674 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006675 chain = ssl->handshake->ecrs_peer_cert;
6676 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006677 goto crt_verify;
6678 }
6679#endif
6680
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006681 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006682 {
6683 /* mbedtls_ssl_read_record may have sent an alert already. We
6684 let it decide whether to alert. */
6685 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006686 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006687 }
6688
Hanno Beckerb8a08572019-02-05 12:49:06 +00006689#if defined(MBEDTLS_SSL_SRV_C)
6690 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6691 {
6692 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006693
6694 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006695 ret = 0;
6696 else
6697 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006698
Hanno Becker613d4902019-02-05 13:11:17 +00006699 goto exit;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006700 }
6701#endif /* MBEDTLS_SSL_SRV_C */
6702
Hanno Becker35e41772019-02-05 15:37:23 +00006703 /* Clear existing peer CRT structure in case we tried to
6704 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006705 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006706
6707 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6708 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006709 {
6710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6711 sizeof( mbedtls_x509_crt ) ) );
6712 mbedtls_ssl_send_alert_message( ssl,
6713 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6714 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006715
Hanno Beckere4aeb762019-02-05 17:19:52 +00006716 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6717 goto exit;
6718 }
6719 mbedtls_x509_crt_init( chain );
6720
6721 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006722 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006723 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006724
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006725#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6726 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006727 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006728
6729crt_verify:
6730 if( ssl->handshake->ecrs_enabled)
6731 rs_ctx = &ssl->handshake->ecrs_ctx;
6732#endif
6733
Hanno Becker3cf50612019-02-05 14:36:34 +00006734 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006735 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006736 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006737 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006738
Hanno Becker3008d282019-02-05 17:02:28 +00006739#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker3008d282019-02-05 17:02:28 +00006740 {
Hanno Becker5882dd02019-06-06 16:25:57 +01006741 size_t pk_len;
6742 unsigned char *pk_start;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006743
Hanno Becker34106f62019-02-08 14:59:05 +00006744 /* We parse the CRT chain without copying, so
6745 * these pointers point into the input buffer,
6746 * and are hence still valid after freeing the
6747 * CRT chain. */
Hanno Becker3008d282019-02-05 17:02:28 +00006748
Hanno Becker5882dd02019-06-06 16:25:57 +01006749#if defined(MBEDTLS_SSL_RENEGOTIATION)
6750 unsigned char *crt_start;
6751 size_t crt_len;
6752
Hanno Becker34106f62019-02-08 14:59:05 +00006753 crt_start = chain->raw.p;
6754 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01006755#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker3008d282019-02-05 17:02:28 +00006756
Hanno Becker34106f62019-02-08 14:59:05 +00006757 pk_start = chain->pk_raw.p;
6758 pk_len = chain->pk_raw.len;
6759
6760 /* Free the CRT structures before computing
6761 * digest and copying the peer's public key. */
6762 mbedtls_x509_crt_free( chain );
6763 mbedtls_free( chain );
6764 chain = NULL;
6765
Hanno Becker5882dd02019-06-06 16:25:57 +01006766#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006767 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckercf291d62019-02-06 16:19:04 +00006768 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006769 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01006770#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006771
6772 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6773 if( ret != 0 )
6774 goto exit;
Hanno Beckercf291d62019-02-06 16:19:04 +00006775 }
Hanno Becker34106f62019-02-08 14:59:05 +00006776#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6777 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006778 ssl->session_negotiate->peer_cert = chain;
6779 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006780#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006783
Hanno Becker613d4902019-02-05 13:11:17 +00006784exit:
6785
Hanno Beckere4aeb762019-02-05 17:19:52 +00006786 if( ret == 0 )
6787 ssl->state++;
6788
6789#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6790 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6791 {
6792 ssl->handshake->ecrs_peer_cert = chain;
6793 chain = NULL;
6794 }
6795#endif
6796
6797 if( chain != NULL )
6798 {
6799 mbedtls_x509_crt_free( chain );
6800 mbedtls_free( chain );
6801 }
6802
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 return( ret );
6804}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006805#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006808{
6809 int ret;
6810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006814 ssl->out_msglen = 1;
6815 ssl->out_msg[0] = 1;
6816
Paul Bakker5121ce52009-01-03 21:22:43 +00006817 ssl->state++;
6818
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006819 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006820 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 return( ret );
6823 }
6824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006826
6827 return( 0 );
6828}
6829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006831{
6832 int ret;
6833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006835
Hanno Becker327c93b2018-08-15 13:56:18 +01006836 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006839 return( ret );
6840 }
6841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006845 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6846 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006848 }
6849
Hanno Beckere678eaa2018-08-21 14:57:46 +01006850 /* CCS records are only accepted if they have length 1 and content '1',
6851 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006852
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006853 /*
6854 * Switch to our negotiated transform and session parameters for inbound
6855 * data.
6856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006858 ssl->transform_in = ssl->transform_negotiate;
6859 ssl->session_in = ssl->session_negotiate;
6860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006862 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006865 ssl_dtls_replay_reset( ssl );
6866#endif
6867
6868 /* Increment epoch */
6869 if( ++ssl->in_epoch == 0 )
6870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006872 /* This is highly unlikely to happen for legitimate reasons, so
6873 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006875 }
6876 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006877 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006879#if defined(MBEDTLS_SSL_PROTO_TLS)
6880 {
6881 memset( ssl->in_ctr, 0, 8 );
6882 }
6883#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006884
Hanno Beckerf5970a02019-05-08 09:38:41 +01006885 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6888 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6894 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006896 }
6897 }
6898#endif
6899
Paul Bakker5121ce52009-01-03 21:22:43 +00006900 ssl->state++;
6901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006903
6904 return( 0 );
6905}
6906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6908 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006909{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006910 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6913 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6914 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006915 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006916 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006917#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6919#if defined(MBEDTLS_SHA512_C)
6920 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006921 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6922 else
6923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#if defined(MBEDTLS_SHA256_C)
6925 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006926 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006927 else
6928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006932 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006933 }
Paul Bakker380da532012-04-18 16:10:25 +00006934}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6939 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006940 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6941 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006942#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6944#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006945 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006948 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006951}
6952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006954 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6957 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006958 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6959 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6962#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006963 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006964#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006966 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006967#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006969}
6970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6972 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6973static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006974 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006975{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006976 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6977 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006978}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006979#endif
Paul Bakker380da532012-04-18 16:10:25 +00006980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6982#if defined(MBEDTLS_SHA256_C)
6983static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006984 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006985{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006986 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006987}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006988#endif
Paul Bakker380da532012-04-18 16:10:25 +00006989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990#if defined(MBEDTLS_SHA512_C)
6991static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006992 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006993{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006994 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006995}
Paul Bakker769075d2012-11-24 11:26:46 +01006996#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007000static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007002{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007003 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007004 mbedtls_md5_context md5;
7005 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007006
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 unsigned char padbuf[48];
7008 unsigned char md5sum[16];
7009 unsigned char sha1sum[20];
7010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007012 if( !session )
7013 session = ssl->session;
7014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007016
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007017 mbedtls_md5_init( &md5 );
7018 mbedtls_sha1_init( &sha1 );
7019
7020 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7021 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007022
7023 /*
7024 * SSLv3:
7025 * hash =
7026 * MD5( master + pad2 +
7027 * MD5( handshake + sender + master + pad1 ) )
7028 * + SHA1( master + pad2 +
7029 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007030 */
7031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007033 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7034 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007035#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007038 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7039 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007043 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
Paul Bakker1ef83d62012-04-11 12:09:53 +00007045 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007046
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007047 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7048 mbedtls_md5_update_ret( &md5, session->master, 48 );
7049 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7050 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007051
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007052 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7053 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7054 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7055 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007056
Paul Bakker1ef83d62012-04-11 12:09:53 +00007057 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007058
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007059 mbedtls_md5_starts_ret( &md5 );
7060 mbedtls_md5_update_ret( &md5, session->master, 48 );
7061 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7062 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7063 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007064
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007065 mbedtls_sha1_starts_ret( &sha1 );
7066 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7067 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7068 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7069 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007073 mbedtls_md5_free( &md5 );
7074 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007075
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007076 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7077 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7078 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007085static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007087{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007088 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007089 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007090 mbedtls_md5_context md5;
7091 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007092 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007095 if( !session )
7096 session = ssl->session;
7097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007099
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007100 mbedtls_md5_init( &md5 );
7101 mbedtls_sha1_init( &sha1 );
7102
7103 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7104 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
Paul Bakker1ef83d62012-04-11 12:09:53 +00007106 /*
7107 * TLSv1:
7108 * hash = PRF( master, finished_label,
7109 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7110 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007113 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7114 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007115#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007118 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7119 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007120#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007123 ? "client finished"
7124 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007125
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007126 mbedtls_md5_finish_ret( &md5, padbuf );
7127 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007128
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007129 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007130 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007133
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007134 mbedtls_md5_free( &md5 );
7135 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007136
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007137 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7144#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007145static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007147{
7148 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007149 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007150 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007151 unsigned char padbuf[32];
7152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007154 if( !session )
7155 session = ssl->session;
7156
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007157 mbedtls_sha256_init( &sha256 );
7158
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007160
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007161 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007162
7163 /*
7164 * TLSv1.2:
7165 * hash = PRF( master, finished_label,
7166 * Hash( handshake ) )[0.11]
7167 */
7168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169#if !defined(MBEDTLS_SHA256_ALT)
7170 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007171 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007172#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007175 ? "client finished"
7176 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007177
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007178 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007179
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007180 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007181 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007184
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007185 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007186
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007187 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007194static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007196{
7197 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007198 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007199 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007200 unsigned char padbuf[48];
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007203 if( !session )
7204 session = ssl->session;
7205
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007206 mbedtls_sha512_init( &sha512 );
7207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007209
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007210 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007211
7212 /*
7213 * TLSv1.2:
7214 * hash = PRF( master, finished_label,
7215 * Hash( handshake ) )[0.11]
7216 */
7217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007219 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7220 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007221#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007224 ? "client finished"
7225 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007226
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007227 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007228
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007229 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007230 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007233
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007234 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007235
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007236 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240#endif /* MBEDTLS_SHA512_C */
7241#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007246
7247 /*
7248 * Free our handshake params
7249 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007250 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007252 ssl->handshake = NULL;
7253
7254 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007255 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007256 */
7257 if( ssl->transform )
7258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 mbedtls_ssl_transform_free( ssl->transform );
7260 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007261 }
7262 ssl->transform = ssl->transform_negotiate;
7263 ssl->transform_negotiate = NULL;
7264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007266}
7267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007269{
7270 int resume = ssl->handshake->resume;
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if defined(MBEDTLS_SSL_RENEGOTIATION)
7275 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007278 ssl->renego_records_seen = 0;
7279 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007280#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007281
7282 /*
7283 * Free the previous session and switch in the current one
7284 */
Paul Bakker0a597072012-09-25 21:55:46 +00007285 if( ssl->session )
7286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007288 /* RFC 7366 3.1: keep the EtM state */
7289 ssl->session_negotiate->encrypt_then_mac =
7290 ssl->session->encrypt_then_mac;
7291#endif
7292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 mbedtls_ssl_session_free( ssl->session );
7294 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007295 }
7296 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007297 ssl->session_negotiate = NULL;
7298
Paul Bakker0a597072012-09-25 21:55:46 +00007299 /*
7300 * Add cache entry
7301 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007302 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007303 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007304 resume == 0 )
7305 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007306 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007308 }
Paul Bakker0a597072012-09-25 21:55:46 +00007309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007311 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007312 ssl->handshake->flight != NULL )
7313 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007314 /* Cancel handshake timer */
7315 ssl_set_timer( ssl, 0 );
7316
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007317 /* Keep last flight around in case we need to resend it:
7318 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007320 }
7321 else
7322#endif
7323 ssl_handshake_wrapup_free_hs_transform( ssl );
7324
Paul Bakker48916f92012-09-16 19:57:18 +00007325 ssl->state++;
7326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007328}
7329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007331{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007332 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007335
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007336 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007337
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007338 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007339
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007340 /*
7341 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7342 * may define some other value. Currently (early 2016), no defined
7343 * ciphersuite does this (and this is unlikely to change as activity has
7344 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007349 ssl->verify_data_len = hash_len;
7350 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007351#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007352
Paul Bakker5121ce52009-01-03 21:22:43 +00007353 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7355 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007356
7357 /*
7358 * In case of session resuming, invert the client and server
7359 * ChangeCipherSpec messages order.
7360 */
Paul Bakker0a597072012-09-25 21:55:46 +00007361 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007364 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007368 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007370#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007371 }
7372 else
7373 ssl->state++;
7374
Paul Bakker48916f92012-09-16 19:57:18 +00007375 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007376 * Switch to our negotiated transform and session parameters for outbound
7377 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007382 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007383 {
7384 unsigned char i;
7385
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007386 /* Remember current epoch settings for resending */
7387 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007388 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007389
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007390 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007391 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007392
7393 /* Increment epoch */
7394 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007395 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007396 break;
7397
7398 /* The loop goes to its end iff the counter is wrapping */
7399 if( i == 0 )
7400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7402 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007403 }
7404 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007405 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007407#if defined(MBEDTLS_SSL_PROTO_TLS)
7408 {
7409 memset( ssl->cur_out_ctr, 0, 8 );
7410 }
7411#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007412
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007413 ssl->transform_out = ssl->transform_negotiate;
7414 ssl->session_out = ssl->session_negotiate;
7415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7417 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7422 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007423 }
7424 }
7425#endif
7426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007428 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007430#endif
7431
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007432 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007433 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007435 return( ret );
7436 }
7437
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007438#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007439 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007440 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7441 {
7442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7443 return( ret );
7444 }
7445#endif
7446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007448
7449 return( 0 );
7450}
7451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007453#define SSL_MAX_HASH_LEN 36
7454#else
7455#define SSL_MAX_HASH_LEN 12
7456#endif
7457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007459{
Paul Bakker23986e52011-04-24 08:57:21 +00007460 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007461 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007462 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007465
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007466 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007467
Hanno Becker327c93b2018-08-15 13:56:18 +01007468 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007471 return( ret );
7472 }
7473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007477 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7478 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007480 }
7481
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007482 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483#if defined(MBEDTLS_SSL_PROTO_SSL3)
7484 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007485 hash_len = 36;
7486 else
7487#endif
7488 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7491 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007494 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7495 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497 }
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007500 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007503 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7504 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007506 }
7507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007509 ssl->verify_data_len = hash_len;
7510 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007511#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007512
Paul Bakker0a597072012-09-25 21:55:46 +00007513 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007516 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007520 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007522#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007523 }
7524 else
7525 ssl->state++;
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007528 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007530#endif
7531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007533
7534 return( 0 );
7535}
7536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7542 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7543 mbedtls_md5_init( &handshake->fin_md5 );
7544 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007545 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7546 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7549#if defined(MBEDTLS_SHA256_C)
7550 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007551 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007552#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#if defined(MBEDTLS_SHA512_C)
7554 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007555 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007556#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007558
7559 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007560
7561#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7562 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7563 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7564#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566#if defined(MBEDTLS_DHM_C)
7567 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#if defined(MBEDTLS_ECDH_C)
7570 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007571#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007572#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007573 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007574#if defined(MBEDTLS_SSL_CLI_C)
7575 handshake->ecjpake_cache = NULL;
7576 handshake->ecjpake_cache_len = 0;
7577#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007578#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007579
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007580#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007581 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007582#endif
7583
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007584#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7585 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7586#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007587
7588#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7589 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7590 mbedtls_pk_init( &handshake->peer_pubkey );
7591#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007592}
7593
Hanno Becker611a83b2018-01-03 14:27:32 +00007594void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7599 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007600
Hanno Becker92231322018-01-03 15:32:51 +00007601#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 mbedtls_md_init( &transform->md_ctx_enc );
7603 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007604#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007605}
7606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007610}
7611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007613{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007614 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007615 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007617 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007619 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007620 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007621
7622 /*
7623 * Either the pointers are now NULL or cleared properly and can be freed.
7624 * Now allocate missing structures.
7625 */
7626 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007627 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007628 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007629 }
Paul Bakker48916f92012-09-16 19:57:18 +00007630
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007631 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007632 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007633 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007634 }
Paul Bakker48916f92012-09-16 19:57:18 +00007635
Paul Bakker82788fb2014-10-20 13:59:19 +02007636 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007637 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007638 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007639 }
Paul Bakker48916f92012-09-16 19:57:18 +00007640
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007641 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007642 if( ssl->handshake == NULL ||
7643 ssl->transform_negotiate == NULL ||
7644 ssl->session_negotiate == NULL )
7645 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 mbedtls_free( ssl->handshake );
7649 mbedtls_free( ssl->transform_negotiate );
7650 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007651
7652 ssl->handshake = NULL;
7653 ssl->transform_negotiate = NULL;
7654 ssl->session_negotiate = NULL;
7655
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007656 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007657 }
7658
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007659 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007661 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007662 ssl_handshake_params_init( ssl->handshake );
7663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007665 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007666 {
7667 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007668
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007669 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7670 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7671 else
7672 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007673
7674 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007675 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007676#endif
7677
Paul Bakker48916f92012-09-16 19:57:18 +00007678 return( 0 );
7679}
7680
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007681#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007682/* Dummy cookie callbacks for defaults */
7683static int ssl_cookie_write_dummy( void *ctx,
7684 unsigned char **p, unsigned char *end,
7685 const unsigned char *cli_id, size_t cli_id_len )
7686{
7687 ((void) ctx);
7688 ((void) p);
7689 ((void) end);
7690 ((void) cli_id);
7691 ((void) cli_id_len);
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007694}
7695
7696static int ssl_cookie_check_dummy( void *ctx,
7697 const unsigned char *cookie, size_t cookie_len,
7698 const unsigned char *cli_id, size_t cli_id_len )
7699{
7700 ((void) ctx);
7701 ((void) cookie);
7702 ((void) cookie_len);
7703 ((void) cli_id);
7704 ((void) cli_id_len);
7705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007707}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007708#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007709
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007710/* Once ssl->out_hdr as the address of the beginning of the
7711 * next outgoing record is set, deduce the other pointers.
7712 *
7713 * Note: For TLS, we save the implicit record sequence number
7714 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7715 * and the caller has to make sure there's space for this.
7716 */
7717
7718static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7719 mbedtls_ssl_transform *transform )
7720{
7721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007722 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007723 {
7724 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007725#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007726 ssl->out_cid = ssl->out_ctr + 8;
7727 ssl->out_len = ssl->out_cid;
7728 if( transform != NULL )
7729 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007730#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007731 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007732#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007733 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007734 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007735 MBEDTLS_SSL_TRANSPORT_ELSE
7736#endif /* MBEDTLS_SSL_PROTO_DTLS */
7737#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007738 {
7739 ssl->out_ctr = ssl->out_hdr - 8;
7740 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007741#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007742 ssl->out_cid = ssl->out_len;
7743#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007744 ssl->out_iv = ssl->out_hdr + 5;
7745 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007746#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007747
7748 /* Adjust out_msg to make space for explicit IV, if used. */
7749 if( transform != NULL &&
7750 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7751 {
7752 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7753 }
7754 else
7755 ssl->out_msg = ssl->out_iv;
7756}
7757
7758/* Once ssl->in_hdr as the address of the beginning of the
7759 * next incoming record is set, deduce the other pointers.
7760 *
7761 * Note: For TLS, we save the implicit record sequence number
7762 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7763 * and the caller has to make sure there's space for this.
7764 */
7765
Hanno Beckerf5970a02019-05-08 09:38:41 +01007766static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007767{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007768 /* This function sets the pointers to match the case
7769 * of unprotected TLS/DTLS records, with both ssl->in_iv
7770 * and ssl->in_msg pointing to the beginning of the record
7771 * content.
7772 *
7773 * When decrypting a protected record, ssl->in_msg
7774 * will be shifted to point to the beginning of the
7775 * record plaintext.
7776 */
7777
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007778#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007779 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007780 {
Hanno Becker70e79282019-05-03 14:34:53 +01007781 /* This sets the header pointers to match records
7782 * without CID. When we receive a record containing
7783 * a CID, the fields are shifted accordingly in
7784 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007785 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007786#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007787 ssl->in_cid = ssl->in_ctr + 8;
7788 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007789#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007790 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007791#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007792 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007793 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007794 MBEDTLS_SSL_TRANSPORT_ELSE
7795#endif /* MBEDTLS_SSL_PROTO_DTLS */
7796#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007797 {
7798 ssl->in_ctr = ssl->in_hdr - 8;
7799 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007800#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007801 ssl->in_cid = ssl->in_len;
7802#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007803 ssl->in_iv = ssl->in_hdr + 5;
7804 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007805#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007806
Hanno Beckerf5970a02019-05-08 09:38:41 +01007807 /* This will be adjusted at record decryption time. */
7808 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007809}
7810
Paul Bakker5121ce52009-01-03 21:22:43 +00007811/*
7812 * Initialize an SSL context
7813 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007814void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7815{
7816 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7817}
7818
7819/*
7820 * Setup an SSL context
7821 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007822
7823static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7824{
7825 /* Set the incoming and outgoing record pointers. */
7826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007827 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007828 {
7829 ssl->out_hdr = ssl->out_buf;
7830 ssl->in_hdr = ssl->in_buf;
7831 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007832 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007833#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007834#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007835 {
7836 ssl->out_hdr = ssl->out_buf + 8;
7837 ssl->in_hdr = ssl->in_buf + 8;
7838 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007839#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007840
7841 /* Derive other internal pointers. */
7842 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007843 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007844}
7845
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007846int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007847 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007848{
Paul Bakker48916f92012-09-16 19:57:18 +00007849 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007850
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007851 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007852
7853 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007854 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007855 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007856
7857 /* Set to NULL in case of an error condition */
7858 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007859
Angus Grattond8213d02016-05-25 20:56:48 +10007860 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7861 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007862 {
Angus Grattond8213d02016-05-25 20:56:48 +10007863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007864 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007865 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007866 }
7867
7868 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7869 if( ssl->out_buf == NULL )
7870 {
7871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007872 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007873 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007874 }
7875
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007876 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007877
Paul Bakker48916f92012-09-16 19:57:18 +00007878 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007879 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007880
7881 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007882
7883error:
7884 mbedtls_free( ssl->in_buf );
7885 mbedtls_free( ssl->out_buf );
7886
7887 ssl->conf = NULL;
7888
7889 ssl->in_buf = NULL;
7890 ssl->out_buf = NULL;
7891
7892 ssl->in_hdr = NULL;
7893 ssl->in_ctr = NULL;
7894 ssl->in_len = NULL;
7895 ssl->in_iv = NULL;
7896 ssl->in_msg = NULL;
7897
7898 ssl->out_hdr = NULL;
7899 ssl->out_ctr = NULL;
7900 ssl->out_len = NULL;
7901 ssl->out_iv = NULL;
7902 ssl->out_msg = NULL;
7903
k-stachowiak9f7798e2018-07-31 16:52:32 +02007904 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007905}
7906
7907/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007908 * Reset an initialized and used SSL context for re-use while retaining
7909 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007910 *
7911 * If partial is non-zero, keep data in the input buffer and client ID.
7912 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007913 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007914static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007915{
Paul Bakker48916f92012-09-16 19:57:18 +00007916 int ret;
7917
Hanno Becker7e772132018-08-10 12:38:21 +01007918#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7919 !defined(MBEDTLS_SSL_SRV_C)
7920 ((void) partial);
7921#endif
7922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007924
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007925 /* Cancel any possibly running timer */
7926 ssl_set_timer( ssl, 0 );
7927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928#if defined(MBEDTLS_SSL_RENEGOTIATION)
7929 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007930 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007931
7932 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7934 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007937
Paul Bakker7eb013f2011-10-06 12:37:39 +00007938 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007939 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007940
7941 ssl->in_msgtype = 0;
7942 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007944 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007945 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007948 ssl_dtls_replay_reset( ssl );
7949#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007950
7951 ssl->in_hslen = 0;
7952 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007953
7954 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007955
7956 ssl->out_msgtype = 0;
7957 ssl->out_msglen = 0;
7958 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7960 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007961 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007962#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007963
Hanno Becker19859472018-08-06 09:40:20 +01007964 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7965
Paul Bakker48916f92012-09-16 19:57:18 +00007966 ssl->transform_in = NULL;
7967 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007968
Hanno Becker78640902018-08-13 16:35:15 +01007969 ssl->session_in = NULL;
7970 ssl->session_out = NULL;
7971
Angus Grattond8213d02016-05-25 20:56:48 +10007972 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007973
7974#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007975 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007976#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7977 {
7978 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007979 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007980 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7983 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7986 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7989 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007990 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007991 }
7992#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007993
Paul Bakker48916f92012-09-16 19:57:18 +00007994 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 mbedtls_ssl_transform_free( ssl->transform );
7997 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007998 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007999 }
Paul Bakker48916f92012-09-16 19:57:18 +00008000
Paul Bakkerc0463502013-02-14 11:19:38 +01008001 if( ssl->session )
8002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 mbedtls_ssl_session_free( ssl->session );
8004 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008005 ssl->session = NULL;
8006 }
8007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008009 ssl->alpn_chosen = NULL;
8010#endif
8011
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008012#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008013#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008014 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008015#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008016 {
8017 mbedtls_free( ssl->cli_id );
8018 ssl->cli_id = NULL;
8019 ssl->cli_id_len = 0;
8020 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008021#endif
8022
Paul Bakker48916f92012-09-16 19:57:18 +00008023 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8024 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008025
8026 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008027}
8028
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008029/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008030 * Reset an initialized and used SSL context for re-use while retaining
8031 * all application-set variables, function pointers and data.
8032 */
8033int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8034{
8035 return( ssl_session_reset_int( ssl, 0 ) );
8036}
8037
8038/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008039 * SSL set accessors
8040 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008041void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008042{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008043 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008044}
8045
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008046void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008048 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008049}
8050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008052void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008054 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008055}
8056#endif
8057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008059void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008060{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008061 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008062}
8063#endif
8064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008066
Hanno Becker1841b0a2018-08-24 11:13:57 +01008067void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8068 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008069{
8070 ssl->disable_datagram_packing = !allow_packing;
8071}
8072
8073void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8074 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008075{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008076 conf->hs_timeout_min = min;
8077 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008078}
8079#endif
8080
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008081void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008082{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008083 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008084}
8085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008087void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008088 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008089 void *p_vrfy )
8090{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008091 conf->f_vrfy = f_vrfy;
8092 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008095
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008096void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008097 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008098 void *p_rng )
8099{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008100 conf->f_rng = f_rng;
8101 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008102}
8103
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008104void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008105 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008106 void *p_dbg )
8107{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008108 conf->f_dbg = f_dbg;
8109 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008110}
8111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008113 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008114 mbedtls_ssl_send_t *f_send,
8115 mbedtls_ssl_recv_t *f_recv,
8116 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008117{
8118 ssl->p_bio = p_bio;
8119 ssl->f_send = f_send;
8120 ssl->f_recv = f_recv;
8121 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008122}
8123
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008124#if defined(MBEDTLS_SSL_PROTO_DTLS)
8125void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8126{
8127 ssl->mtu = mtu;
8128}
8129#endif
8130
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008131void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008132{
8133 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008134}
8135
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008136void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8137 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008138 mbedtls_ssl_set_timer_t *f_set_timer,
8139 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008140{
8141 ssl->p_timer = p_timer;
8142 ssl->f_set_timer = f_set_timer;
8143 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008144
8145 /* Make sure we start with no timer running */
8146 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008147}
8148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008150void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008151 void *p_cache,
8152 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8153 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008154{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008155 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008156 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008157 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#if defined(MBEDTLS_SSL_CLI_C)
8162int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008163{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008164 int ret;
8165
8166 if( ssl == NULL ||
8167 session == NULL ||
8168 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008169 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008172 }
8173
Hanno Becker58fccf22019-02-06 14:30:46 +00008174 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8175 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008176 return( ret );
8177
Paul Bakker0a597072012-09-25 21:55:46 +00008178 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008179
8180 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008181}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008182#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008183
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008184void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008185 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008186{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008187 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8188 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8189 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8190 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008191}
8192
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008193void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008194 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008195 int major, int minor )
8196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008198 return;
8199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008201 return;
8202
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008203 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008204}
8205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008207void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008208 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008209{
8210 conf->cert_profile = profile;
8211}
8212
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008213/* Append a new keycert entry to a (possibly empty) list */
8214static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8215 mbedtls_x509_crt *cert,
8216 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008217{
niisato8ee24222018-06-25 19:05:48 +09008218 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008219
niisato8ee24222018-06-25 19:05:48 +09008220 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8221 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008222 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008223
niisato8ee24222018-06-25 19:05:48 +09008224 new_cert->cert = cert;
8225 new_cert->key = key;
8226 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008227
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008228 /* Update head is the list was null, else add to the end */
8229 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008230 {
niisato8ee24222018-06-25 19:05:48 +09008231 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008232 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008233 else
8234 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008235 mbedtls_ssl_key_cert *cur = *head;
8236 while( cur->next != NULL )
8237 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008238 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008239 }
8240
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008241 return( 0 );
8242}
8243
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008244int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008245 mbedtls_x509_crt *own_cert,
8246 mbedtls_pk_context *pk_key )
8247{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008248 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008249}
8250
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008251void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008252 mbedtls_x509_crt *ca_chain,
8253 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008254{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008255 conf->ca_chain = ca_chain;
8256 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008259
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008260#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8261int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8262 mbedtls_x509_crt *own_cert,
8263 mbedtls_pk_context *pk_key )
8264{
8265 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8266 own_cert, pk_key ) );
8267}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008268
8269void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8270 mbedtls_x509_crt *ca_chain,
8271 mbedtls_x509_crl *ca_crl )
8272{
8273 ssl->handshake->sni_ca_chain = ca_chain;
8274 ssl->handshake->sni_ca_crl = ca_crl;
8275}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008276
8277void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8278 int authmode )
8279{
8280 ssl->handshake->sni_authmode = authmode;
8281}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008282#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8283
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008284#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008285/*
8286 * Set EC J-PAKE password for current handshake
8287 */
8288int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8289 const unsigned char *pw,
8290 size_t pw_len )
8291{
8292 mbedtls_ecjpake_role role;
8293
Janos Follath8eb64132016-06-03 15:40:57 +01008294 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8296
8297 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8298 role = MBEDTLS_ECJPAKE_SERVER;
8299 else
8300 role = MBEDTLS_ECJPAKE_CLIENT;
8301
8302 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8303 role,
8304 MBEDTLS_MD_SHA256,
8305 MBEDTLS_ECP_DP_SECP256R1,
8306 pw, pw_len ) );
8307}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008308#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008311int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008312 const unsigned char *psk, size_t psk_len,
8313 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008314{
Paul Bakker6db455e2013-09-18 17:29:31 +02008315 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008320
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008321 /* Identity len will be encoded on two bytes */
8322 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008323 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008324 {
8325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8326 }
8327
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008328 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008329 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008330 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008331
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008332 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008333 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008334 conf->psk_len = 0;
8335 }
8336 if( conf->psk_identity != NULL )
8337 {
8338 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008339 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008340 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008341 }
8342
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008343 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8344 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008345 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008346 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008347 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008348 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008349 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008350 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008351 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008352
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008353 conf->psk_len = psk_len;
8354 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008355
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008356 memcpy( conf->psk, psk, conf->psk_len );
8357 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008358
8359 return( 0 );
8360}
8361
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008362int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8363 const unsigned char *psk, size_t psk_len )
8364{
8365 if( psk == NULL || ssl->handshake == NULL )
8366 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8367
8368 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8370
8371 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008372 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008373 mbedtls_platform_zeroize( ssl->handshake->psk,
8374 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008375 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008376 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008377 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008378
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008379 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008380 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008381
8382 ssl->handshake->psk_len = psk_len;
8383 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8384
8385 return( 0 );
8386}
8387
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008388void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008390 size_t),
8391 void *p_psk )
8392{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008393 conf->f_psk = f_psk;
8394 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008397
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008398#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008399
8400#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008401int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008402{
8403 int ret;
8404
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008405 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8406 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8407 {
8408 mbedtls_mpi_free( &conf->dhm_P );
8409 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008410 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008412
8413 return( 0 );
8414}
Hanno Becker470a8c42017-10-04 15:28:46 +01008415#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008416
Hanno Beckera90658f2017-10-04 15:29:08 +01008417int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8418 const unsigned char *dhm_P, size_t P_len,
8419 const unsigned char *dhm_G, size_t G_len )
8420{
8421 int ret;
8422
8423 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8424 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8425 {
8426 mbedtls_mpi_free( &conf->dhm_P );
8427 mbedtls_mpi_free( &conf->dhm_G );
8428 return( ret );
8429 }
8430
8431 return( 0 );
8432}
Paul Bakker5121ce52009-01-03 21:22:43 +00008433
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008434int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008435{
8436 int ret;
8437
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008438 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8439 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8440 {
8441 mbedtls_mpi_free( &conf->dhm_P );
8442 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008443 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008444 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008445
8446 return( 0 );
8447}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008448#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008449
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008450#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8451/*
8452 * Set the minimum length for Diffie-Hellman parameters
8453 */
8454void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8455 unsigned int bitlen )
8456{
8457 conf->dhm_min_bitlen = bitlen;
8458}
8459#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8460
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008461#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008462/*
8463 * Set allowed/preferred hashes for handshake signatures
8464 */
8465void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8466 const int *hashes )
8467{
8468 conf->sig_hashes = hashes;
8469}
Hanno Becker947194e2017-04-07 13:25:49 +01008470#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008471
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008472#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008473/*
8474 * Set the allowed elliptic curves
8475 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008476void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008477 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008478{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008479 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008480}
Hanno Becker947194e2017-04-07 13:25:49 +01008481#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008482
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008483#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008485{
Hanno Becker947194e2017-04-07 13:25:49 +01008486 /* Initialize to suppress unnecessary compiler warning */
8487 size_t hostname_len = 0;
8488
8489 /* Check if new hostname is valid before
8490 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008491 if( hostname != NULL )
8492 {
8493 hostname_len = strlen( hostname );
8494
8495 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8497 }
8498
8499 /* Now it's clear that we will overwrite the old hostname,
8500 * so we can free it safely */
8501
8502 if( ssl->hostname != NULL )
8503 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008504 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008505 mbedtls_free( ssl->hostname );
8506 }
8507
8508 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008509
Paul Bakker5121ce52009-01-03 21:22:43 +00008510 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008511 {
8512 ssl->hostname = NULL;
8513 }
8514 else
8515 {
8516 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008517 if( ssl->hostname == NULL )
8518 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008519
Hanno Becker947194e2017-04-07 13:25:49 +01008520 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008521
Hanno Becker947194e2017-04-07 13:25:49 +01008522 ssl->hostname[hostname_len] = '\0';
8523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008524
8525 return( 0 );
8526}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008527#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008528
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008529#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008530void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008532 const unsigned char *, size_t),
8533 void *p_sni )
8534{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008535 conf->f_sni = f_sni;
8536 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008541int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008542{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008543 size_t cur_len, tot_len;
8544 const char **p;
8545
8546 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008547 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8548 * MUST NOT be truncated."
8549 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008550 */
8551 tot_len = 0;
8552 for( p = protos; *p != NULL; p++ )
8553 {
8554 cur_len = strlen( *p );
8555 tot_len += cur_len;
8556
8557 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008559 }
8560
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008561 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008562
8563 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008564}
8565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008567{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008568 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008571
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008572void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008573{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008574 conf->max_major_ver = major;
8575 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008576}
8577
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008578void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008579{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008580 conf->min_major_ver = major;
8581 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008582}
8583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008585void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008586{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008587 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008588}
8589#endif
8590
Janos Follath088ce432017-04-10 12:42:31 +01008591#if defined(MBEDTLS_SSL_SRV_C)
8592void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8593 char cert_req_ca_list )
8594{
8595 conf->cert_req_ca_list = cert_req_ca_list;
8596}
8597#endif
8598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008600void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008602 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008603}
8604#endif
8605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008607void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008609 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008610}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008611
8612void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008613 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008614{
8615 conf->enforce_extended_master_secret = ems_enf;
8616}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008617#endif
8618
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008619#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008622 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008623}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008624#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008627int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008628{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008630 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008633 }
8634
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008635 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008636
8637 return( 0 );
8638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008642void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008643{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008644 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008645}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008649void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008650{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008651 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008652}
8653#endif
8654
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008655void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008657 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008658}
8659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008661void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008662{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008663 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008664}
8665
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008666void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008667{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008668 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008669}
8670
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008671void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008672 const unsigned char period[8] )
8673{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008674 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008679#if defined(MBEDTLS_SSL_CLI_C)
8680void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008681{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008682 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008683}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008684#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008685
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008686#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008687void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8688 mbedtls_ssl_ticket_write_t *f_ticket_write,
8689 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8690 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008691{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008692 conf->f_ticket_write = f_ticket_write;
8693 conf->f_ticket_parse = f_ticket_parse;
8694 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008695}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008698
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008699#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8700void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8701 mbedtls_ssl_export_keys_t *f_export_keys,
8702 void *p_export_keys )
8703{
8704 conf->f_export_keys = f_export_keys;
8705 conf->p_export_keys = p_export_keys;
8706}
8707#endif
8708
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008709#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008710void mbedtls_ssl_conf_async_private_cb(
8711 mbedtls_ssl_config *conf,
8712 mbedtls_ssl_async_sign_t *f_async_sign,
8713 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8714 mbedtls_ssl_async_resume_t *f_async_resume,
8715 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008716 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008717{
8718 conf->f_async_sign_start = f_async_sign;
8719 conf->f_async_decrypt_start = f_async_decrypt;
8720 conf->f_async_resume = f_async_resume;
8721 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008722 conf->p_async_config_data = async_config_data;
8723}
8724
Gilles Peskine8f97af72018-04-26 11:46:10 +02008725void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8726{
8727 return( conf->p_async_config_data );
8728}
8729
Gilles Peskine1febfef2018-04-30 11:54:39 +02008730void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008731{
8732 if( ssl->handshake == NULL )
8733 return( NULL );
8734 else
8735 return( ssl->handshake->user_async_ctx );
8736}
8737
Gilles Peskine1febfef2018-04-30 11:54:39 +02008738void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008739 void *ctx )
8740{
8741 if( ssl->handshake != NULL )
8742 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008743}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008744#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008745
Paul Bakker5121ce52009-01-03 21:22:43 +00008746/*
8747 * SSL get accessors
8748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008750{
8751 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8752}
8753
Hanno Becker8b170a02017-10-10 11:51:19 +01008754int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8755{
8756 /*
8757 * Case A: We're currently holding back
8758 * a message for further processing.
8759 */
8760
8761 if( ssl->keep_current_message == 1 )
8762 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008763 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008764 return( 1 );
8765 }
8766
8767 /*
8768 * Case B: Further records are pending in the current datagram.
8769 */
8770
8771#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008772 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008773 ssl->in_left > ssl->next_record_offset )
8774 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008775 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008776 return( 1 );
8777 }
8778#endif /* MBEDTLS_SSL_PROTO_DTLS */
8779
8780 /*
8781 * Case C: A handshake message is being processed.
8782 */
8783
Hanno Becker8b170a02017-10-10 11:51:19 +01008784 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8785 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008787 return( 1 );
8788 }
8789
8790 /*
8791 * Case D: An application data message is being processed
8792 */
8793 if( ssl->in_offt != NULL )
8794 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008795 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008796 return( 1 );
8797 }
8798
8799 /*
8800 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008801 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008802 * we implement support for multiple alerts in single records.
8803 */
8804
8805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8806 return( 0 );
8807}
8808
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008809uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008810{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008811 if( ssl->session != NULL )
8812 return( ssl->session->verify_result );
8813
8814 if( ssl->session_negotiate != NULL )
8815 return( ssl->session_negotiate->verify_result );
8816
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008817 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008818}
8819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008821{
Paul Bakker926c8e42013-03-06 10:23:34 +01008822 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008823 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008826}
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008831 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008832 {
8833 switch( ssl->minor_ver )
8834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008836 return( "DTLSv1.0" );
8837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008839 return( "DTLSv1.2" );
8840
8841 default:
8842 return( "unknown (DTLS)" );
8843 }
8844 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008845 MBEDTLS_SSL_TRANSPORT_ELSE
8846#endif /* MBEDTLS_SSL_PROTO_DTLS */
8847#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008848 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008849 switch( ssl->minor_ver )
8850 {
8851 case MBEDTLS_SSL_MINOR_VERSION_0:
8852 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008853
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008854 case MBEDTLS_SSL_MINOR_VERSION_1:
8855 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008856
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008857 case MBEDTLS_SSL_MINOR_VERSION_2:
8858 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008859
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008860 case MBEDTLS_SSL_MINOR_VERSION_3:
8861 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008862
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008863 default:
8864 return( "unknown" );
8865 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008866 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008867#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008868}
8869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008871{
Hanno Becker3136ede2018-08-17 15:28:19 +01008872 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008874 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008875
Hanno Becker43395762019-05-03 14:46:38 +01008876 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8877
Hanno Becker78640902018-08-13 16:35:15 +01008878 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008879 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881#if defined(MBEDTLS_ZLIB_SUPPORT)
8882 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8883 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008884#endif
8885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008886 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888 case MBEDTLS_MODE_GCM:
8889 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008890 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008892 transform_expansion = transform->minlen;
8893 break;
8894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008896
8897 block_size = mbedtls_cipher_get_block_size(
8898 &transform->cipher_ctx_enc );
8899
Hanno Becker3136ede2018-08-17 15:28:19 +01008900 /* Expansion due to the addition of the MAC. */
8901 transform_expansion += transform->maclen;
8902
8903 /* Expansion due to the addition of CBC padding;
8904 * Theoretically up to 256 bytes, but we never use
8905 * more than the block size of the underlying cipher. */
8906 transform_expansion += block_size;
8907
8908 /* For TLS 1.1 or higher, an explicit IV is added
8909 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008910#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8911 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008912 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008913#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008914
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008915 break;
8916
8917 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008920 }
8921
Hanno Beckera5a2b082019-05-15 14:03:01 +01008922#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008923 if( transform->out_cid_len != 0 )
8924 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008925#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008926
Hanno Becker43395762019-05-03 14:46:38 +01008927 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008928}
8929
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008930#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8931size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8932{
8933 size_t max_len;
8934
8935 /*
8936 * Assume mfl_code is correct since it was checked when set
8937 */
Angus Grattond8213d02016-05-25 20:56:48 +10008938 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008939
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008940 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008941 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008942 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008943 {
Angus Grattond8213d02016-05-25 20:56:48 +10008944 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008945 }
8946
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008947 /* During a handshake, use the value being negotiated */
8948 if( ssl->session_negotiate != NULL &&
8949 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8950 {
8951 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8952 }
8953
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008954 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008955}
8956#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8957
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008958#if defined(MBEDTLS_SSL_PROTO_DTLS)
8959static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8960{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008961 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8962 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8963 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8964 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8965 return ( 0 );
8966
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008967 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8968 return( ssl->mtu );
8969
8970 if( ssl->mtu == 0 )
8971 return( ssl->handshake->mtu );
8972
8973 return( ssl->mtu < ssl->handshake->mtu ?
8974 ssl->mtu : ssl->handshake->mtu );
8975}
8976#endif /* MBEDTLS_SSL_PROTO_DTLS */
8977
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008978int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8979{
8980 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8981
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008982#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8983 !defined(MBEDTLS_SSL_PROTO_DTLS)
8984 (void) ssl;
8985#endif
8986
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008987#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8988 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8989
8990 if( max_len > mfl )
8991 max_len = mfl;
8992#endif
8993
8994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008995 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008996 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008997 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008998 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8999 const size_t overhead = (size_t) ret;
9000
9001 if( ret < 0 )
9002 return( ret );
9003
9004 if( mtu <= overhead )
9005 {
9006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9007 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9008 }
9009
9010 if( max_len > mtu - overhead )
9011 max_len = mtu - overhead;
9012 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009013#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009014
Hanno Becker0defedb2018-08-10 12:35:02 +01009015#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9016 !defined(MBEDTLS_SSL_PROTO_DTLS)
9017 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009018#endif
9019
9020 return( (int) max_len );
9021}
9022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023#if defined(MBEDTLS_X509_CRT_PARSE_C)
9024const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009025{
9026 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009027 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009028
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009029#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009030 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009031#else
9032 return( NULL );
9033#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009038int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9039 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009040{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009041 if( ssl == NULL ||
9042 dst == NULL ||
9043 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009044 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009047 }
9048
Hanno Becker58fccf22019-02-06 14:30:46 +00009049 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009050}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009052
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009053const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9054{
9055 if( ssl == NULL )
9056 return( NULL );
9057
9058 return( ssl->session );
9059}
9060
Paul Bakker5121ce52009-01-03 21:22:43 +00009061/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009062 * Define ticket header determining Mbed TLS version
9063 * and structure of the ticket.
9064 */
9065
Hanno Becker41527622019-05-16 12:50:45 +01009066/*
Hanno Becker26829e92019-05-28 14:30:45 +01009067 * Define bitflag determining compile-time settings influencing
9068 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009069 */
9070
Hanno Becker26829e92019-05-28 14:30:45 +01009071#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009072#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009073#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009074#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009075#endif /* MBEDTLS_HAVE_TIME */
9076
9077#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009078#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009079#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009080#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009081#endif /* MBEDTLS_X509_CRT_PARSE_C */
9082
9083#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009084#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009085#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009086#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009087#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9088
9089#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009090#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009091#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009092#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009093#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9094
9095#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009096#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009097#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009098#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009099#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9100
9101#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009102#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009103#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009104#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009105#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9106
Hanno Becker41527622019-05-16 12:50:45 +01009107#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9108#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9109#else
9110#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9111#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9112
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009113#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9114#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9115#else
9116#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9117#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9118
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009119#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9120#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9121#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9122#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9123#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9124#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9125#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009126#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009127
Hanno Becker26829e92019-05-28 14:30:45 +01009128#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009129 ( (uint16_t) ( \
9130 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9131 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9132 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9133 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9134 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9135 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009136 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9137 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009138
Hanno Becker557fe9f2019-05-16 12:41:07 +01009139static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009140 MBEDTLS_VERSION_MAJOR,
9141 MBEDTLS_VERSION_MINOR,
9142 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009143 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9144 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009145};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009146
9147/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009148 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009149 * (in the presentation language of TLS, RFC 8446 section 3)
9150 *
Hanno Becker26829e92019-05-28 14:30:45 +01009151 * opaque mbedtls_version[3]; // major, minor, patch
9152 * opaque session_format[2]; // version-specific 16-bit field determining
9153 * // the format of the remaining
9154 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009155 *
9156 * Note: When updating the format, remember to keep
9157 * these version+format bytes.
9158 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009159 * // In this version, `session_format` determines
9160 * // the setting of those compile-time
9161 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009162 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009163 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009164 * uint8 ciphersuite[2]; // defined by the standard
9165 * uint8 compression; // 0 or 1
9166 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009167 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009168 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009169 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009170 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9171 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9172 * case disabled: uint8_t peer_cert_digest_type;
9173 * opaque peer_cert_digest<0..2^8-1>;
9174 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009175 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009176 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009177 * uint8 mfl_code; // up to 255 according to standard
9178 * uint8 trunc_hmac; // 0 or 1
9179 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009180 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009181 * The order is the same as in the definition of the structure, except
9182 * verify_result is put before peer_cert so that all mandatory fields come
9183 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009184 */
9185int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9186 unsigned char *buf,
9187 size_t buf_len,
9188 size_t *olen )
9189{
9190 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009191 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009192#if defined(MBEDTLS_HAVE_TIME)
9193 uint64_t start;
9194#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009195#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009196#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009197 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009198#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009199#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009200
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009201 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009202 * Add version identifier
9203 */
9204
9205 used += sizeof( ssl_serialized_session_header );
9206
9207 if( used <= buf_len )
9208 {
9209 memcpy( p, ssl_serialized_session_header,
9210 sizeof( ssl_serialized_session_header ) );
9211 p += sizeof( ssl_serialized_session_header );
9212 }
9213
9214 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009215 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009216 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009217#if defined(MBEDTLS_HAVE_TIME)
9218 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009219
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009220 if( used <= buf_len )
9221 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009222 start = (uint64_t) session->start;
9223
9224 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9225 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9226 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9227 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9228 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9229 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9230 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9231 *p++ = (unsigned char)( ( start ) & 0xFF );
9232 }
9233#endif /* MBEDTLS_HAVE_TIME */
9234
9235 /*
9236 * Basic mandatory fields
9237 */
9238 used += 2 /* ciphersuite */
9239 + 1 /* compression */
9240 + 1 /* id_len */
9241 + sizeof( session->id )
9242 + sizeof( session->master )
9243 + 4; /* verify_result */
9244
9245 if( used <= buf_len )
9246 {
9247 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9248 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9249
9250 *p++ = (unsigned char)( session->compression & 0xFF );
9251
9252 *p++ = (unsigned char)( session->id_len & 0xFF );
9253 memcpy( p, session->id, 32 );
9254 p += 32;
9255
9256 memcpy( p, session->master, 48 );
9257 p += 48;
9258
9259 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9260 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9261 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9262 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009263 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009264
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009265 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009266 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009267 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009268#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009269#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009270 if( session->peer_cert == NULL )
9271 cert_len = 0;
9272 else
9273 cert_len = session->peer_cert->raw.len;
9274
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009275 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009276
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009277 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009278 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009279 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9280 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9281 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9282
9283 if( session->peer_cert != NULL )
9284 {
9285 memcpy( p, session->peer_cert->raw.p, cert_len );
9286 p += cert_len;
9287 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009288 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009289
Hanno Becker5882dd02019-06-06 16:25:57 +01009290#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009291 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009292 if( session->peer_cert_digest != NULL )
9293 {
9294 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9295 if( used <= buf_len )
9296 {
9297 *p++ = (unsigned char) session->peer_cert_digest_type;
9298 *p++ = (unsigned char) session->peer_cert_digest_len;
9299 memcpy( p, session->peer_cert_digest,
9300 session->peer_cert_digest_len );
9301 p += session->peer_cert_digest_len;
9302 }
9303 }
9304 else
9305 {
9306 used += 2;
9307 if( used <= buf_len )
9308 {
9309 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9310 *p++ = 0;
9311 }
9312 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009313#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009314#endif /* MBEDTLS_X509_CRT_PARSE_C */
9315
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009316 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009317 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009318 */
9319#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009320 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009321
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009322 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009323 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009324 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9325 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9326 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9327
9328 if( session->ticket != NULL )
9329 {
9330 memcpy( p, session->ticket, session->ticket_len );
9331 p += session->ticket_len;
9332 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009333
9334 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9335 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9336 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9337 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009338 }
9339#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9340
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009341 /*
9342 * Misc extension-related info
9343 */
9344#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9345 used += 1;
9346
9347 if( used <= buf_len )
9348 *p++ = session->mfl_code;
9349#endif
9350
9351#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9352 used += 1;
9353
9354 if( used <= buf_len )
9355 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9356#endif
9357
9358#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9359 used += 1;
9360
9361 if( used <= buf_len )
9362 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9363#endif
9364
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009365 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009366 *olen = used;
9367
9368 if( used > buf_len )
9369 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009370
9371 return( 0 );
9372}
9373
9374/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009375 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009376 *
9377 * This internal version is wrapped by a public function that cleans up in
9378 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009379 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009380static int ssl_session_load( mbedtls_ssl_session *session,
9381 const unsigned char *buf,
9382 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009383{
9384 const unsigned char *p = buf;
9385 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009386#if defined(MBEDTLS_HAVE_TIME)
9387 uint64_t start;
9388#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009389#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009390#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009391 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009392#endif
9393#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009394
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009395 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009396 * Check version identifier
9397 */
9398
9399 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009401
9402 if( memcmp( p, ssl_serialized_session_header,
9403 sizeof( ssl_serialized_session_header ) ) != 0 )
9404 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009405 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009406 }
9407 p += sizeof( ssl_serialized_session_header );
9408
9409 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009410 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009411 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009412#if defined(MBEDTLS_HAVE_TIME)
9413 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9415
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009416 start = ( (uint64_t) p[0] << 56 ) |
9417 ( (uint64_t) p[1] << 48 ) |
9418 ( (uint64_t) p[2] << 40 ) |
9419 ( (uint64_t) p[3] << 32 ) |
9420 ( (uint64_t) p[4] << 24 ) |
9421 ( (uint64_t) p[5] << 16 ) |
9422 ( (uint64_t) p[6] << 8 ) |
9423 ( (uint64_t) p[7] );
9424 p += 8;
9425
9426 session->start = (time_t) start;
9427#endif /* MBEDTLS_HAVE_TIME */
9428
9429 /*
9430 * Basic mandatory fields
9431 */
9432 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9434
9435 session->ciphersuite = ( p[0] << 8 ) | p[1];
9436 p += 2;
9437
9438 session->compression = *p++;
9439
9440 session->id_len = *p++;
9441 memcpy( session->id, p, 32 );
9442 p += 32;
9443
9444 memcpy( session->master, p, 48 );
9445 p += 48;
9446
9447 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9448 ( (uint32_t) p[1] << 16 ) |
9449 ( (uint32_t) p[2] << 8 ) |
9450 ( (uint32_t) p[3] );
9451 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009452
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009453 /* Immediately clear invalid pointer values that have been read, in case
9454 * we exit early before we replaced them with valid ones. */
9455#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009456#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009457 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009458#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009459 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009460#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009461#endif
9462#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9463 session->ticket = NULL;
9464#endif
9465
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009466 /*
9467 * Peer certificate
9468 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009469#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009470#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009471 if( 3 > (size_t)( end - p ) )
9472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9473
9474 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9475 p += 3;
9476
9477 if( cert_len == 0 )
9478 {
9479 session->peer_cert = NULL;
9480 }
9481 else
9482 {
9483 int ret;
9484
9485 if( cert_len > (size_t)( end - p ) )
9486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9487
9488 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9489
9490 if( session->peer_cert == NULL )
9491 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9492
9493 mbedtls_x509_crt_init( session->peer_cert );
9494
9495 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9496 p, cert_len ) ) != 0 )
9497 {
9498 mbedtls_x509_crt_free( session->peer_cert );
9499 mbedtls_free( session->peer_cert );
9500 session->peer_cert = NULL;
9501 return( ret );
9502 }
9503
9504 p += cert_len;
9505 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009506#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009507 /* Deserialize CRT digest from the end of the ticket. */
9508 if( 2 > (size_t)( end - p ) )
9509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9510
9511 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9512 session->peer_cert_digest_len = (size_t) *p++;
9513
9514 if( session->peer_cert_digest_len != 0 )
9515 {
Hanno Becker2326d202019-06-06 14:54:55 +01009516 const mbedtls_md_info_t *md_info =
9517 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9518 if( md_info == NULL )
9519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9520 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9522
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009523 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9525
9526 session->peer_cert_digest =
9527 mbedtls_calloc( 1, session->peer_cert_digest_len );
9528 if( session->peer_cert_digest == NULL )
9529 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9530
9531 memcpy( session->peer_cert_digest, p,
9532 session->peer_cert_digest_len );
9533 p += session->peer_cert_digest_len;
9534 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009535#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009536#endif /* MBEDTLS_X509_CRT_PARSE_C */
9537
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009538 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009539 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009540 */
9541#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9542 if( 3 > (size_t)( end - p ) )
9543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9544
9545 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9546 p += 3;
9547
9548 if( session->ticket_len != 0 )
9549 {
9550 if( session->ticket_len > (size_t)( end - p ) )
9551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9552
9553 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9554 if( session->ticket == NULL )
9555 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9556
9557 memcpy( session->ticket, p, session->ticket_len );
9558 p += session->ticket_len;
9559 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009560
9561 if( 4 > (size_t)( end - p ) )
9562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9563
9564 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9565 ( (uint32_t) p[1] << 16 ) |
9566 ( (uint32_t) p[2] << 8 ) |
9567 ( (uint32_t) p[3] );
9568 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009569#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9570
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009571 /*
9572 * Misc extension-related info
9573 */
9574#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9575 if( 1 > (size_t)( end - p ) )
9576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9577
9578 session->mfl_code = *p++;
9579#endif
9580
9581#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9582 if( 1 > (size_t)( end - p ) )
9583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9584
9585 session->trunc_hmac = *p++;
9586#endif
9587
9588#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9589 if( 1 > (size_t)( end - p ) )
9590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9591
9592 session->encrypt_then_mac = *p++;
9593#endif
9594
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009595 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009596 if( p != end )
9597 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9598
9599 return( 0 );
9600}
9601
9602/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009603 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009604 */
9605int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9606 const unsigned char *buf,
9607 size_t len )
9608{
9609 int ret = ssl_session_load( session, buf, len );
9610
9611 if( ret != 0 )
9612 mbedtls_ssl_session_free( session );
9613
9614 return( ret );
9615}
9616
9617/*
Paul Bakker1961b702013-01-25 14:49:24 +01009618 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009621{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009623
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009624 if( ssl == NULL || ssl->conf == NULL )
9625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009628 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009630#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009632 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009634#endif
9635
Paul Bakker1961b702013-01-25 14:49:24 +01009636 return( ret );
9637}
9638
9639/*
9640 * Perform the SSL handshake
9641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009643{
9644 int ret = 0;
9645
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009646 if( ssl == NULL || ssl->conf == NULL )
9647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009654
9655 if( ret != 0 )
9656 break;
9657 }
9658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009660
9661 return( ret );
9662}
9663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664#if defined(MBEDTLS_SSL_RENEGOTIATION)
9665#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009666/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009667 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009670{
9671 int ret;
9672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009674
9675 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9677 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009678
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009679 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009680 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009681 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009682 return( ret );
9683 }
9684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009686
9687 return( 0 );
9688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009690
9691/*
9692 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693 * - any side: calling mbedtls_ssl_renegotiate(),
9694 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9695 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009696 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009697 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009701{
9702 int ret;
9703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009705
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009706 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9707 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009708
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009709 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9710 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009712 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009714 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009715 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009716 ssl->handshake->out_msg_seq = 1;
9717 else
9718 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009719 }
9720#endif
9721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9723 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009728 return( ret );
9729 }
9730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009732
9733 return( 0 );
9734}
9735
9736/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009737 * Renegotiate current connection on client,
9738 * or request renegotiation on server
9739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009741{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009743
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009744 if( ssl == NULL || ssl->conf == NULL )
9745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009748 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009749 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009755
9756 /* Did we already try/start sending HelloRequest? */
9757 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009759
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009760 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009761 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009765 /*
9766 * On client, either start the renegotiation process or,
9767 * if already in progress, continue the handshake
9768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009773
9774 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009777 return( ret );
9778 }
9779 }
9780 else
9781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009785 return( ret );
9786 }
9787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009789
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009790 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009791}
9792
9793/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009794 * Check record counters and renegotiate if they're above the limit.
9795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009797{
Andres AG2196c7f2016-12-15 17:01:16 +00009798 size_t ep_len = ssl_ep_len( ssl );
9799 int in_ctr_cmp;
9800 int out_ctr_cmp;
9801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9803 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009804 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009805 {
9806 return( 0 );
9807 }
9808
Andres AG2196c7f2016-12-15 17:01:16 +00009809 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9810 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009811 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009812 ssl->conf->renego_period + ep_len, 8 - ep_len );
9813
9814 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009815 {
9816 return( 0 );
9817 }
9818
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009823
9824/*
9825 * Receive application data decrypted from the SSL layer
9826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009828{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009829 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009830 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009831
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009832 if( ssl == NULL || ssl->conf == NULL )
9833 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009838 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009841 return( ret );
9842
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009843 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009845 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009846 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009847 return( ret );
9848 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009849 }
9850#endif
9851
Hanno Becker4a810fb2017-05-24 16:27:30 +01009852 /*
9853 * Check if renegotiation is necessary and/or handshake is
9854 * in process. If yes, perform/continue, and fall through
9855 * if an unexpected packet is received while the client
9856 * is waiting for the ServerHello.
9857 *
9858 * (There is no equivalent to the last condition on
9859 * the server-side as it is not treated as within
9860 * a handshake while waiting for the ClientHello
9861 * after a renegotiation request.)
9862 */
9863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009865 ret = ssl_check_ctr_renegotiate( ssl );
9866 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9867 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009870 return( ret );
9871 }
9872#endif
9873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009876 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009877 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9878 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009881 return( ret );
9882 }
9883 }
9884
Hanno Beckere41158b2017-10-23 13:30:32 +01009885 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009886 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009887 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009888 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009889 if( ssl->f_get_timer != NULL &&
9890 ssl->f_get_timer( ssl->p_timer ) == -1 )
9891 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009892 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009893 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009894
Hanno Becker327c93b2018-08-15 13:56:18 +01009895 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009896 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009897 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9898 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009899
Hanno Becker4a810fb2017-05-24 16:27:30 +01009900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9901 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009902 }
9903
9904 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009906 {
9907 /*
9908 * OpenSSL sends empty messages to randomize the IV
9909 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009910 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009913 return( 0 );
9914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009916 return( ret );
9917 }
9918 }
9919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009923
Hanno Becker4a810fb2017-05-24 16:27:30 +01009924 /*
9925 * - For client-side, expect SERVER_HELLO_REQUEST.
9926 * - For server-side, expect CLIENT_HELLO.
9927 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9928 */
9929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009931 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009933 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009936
9937 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009939 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009940 {
9941 continue;
9942 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009943 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009944#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009945#if defined(MBEDTLS_SSL_PROTO_TLS)
9946 {
9947 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9948 }
9949#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009950 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009951#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009952
Hanno Becker4a810fb2017-05-24 16:27:30 +01009953#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009954 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009958
9959 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009961 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009962 {
9963 continue;
9964 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009965 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009966#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009967#if defined(MBEDTLS_SSL_PROTO_TLS)
9968 {
9969 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9970 }
9971#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009972 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009973#endif /* MBEDTLS_SSL_SRV_C */
9974
Hanno Becker21df7f92017-10-17 11:03:26 +01009975#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009976 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009977 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9978 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9979 ssl->conf->allow_legacy_renegotiation ==
9980 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9981 {
9982 /*
9983 * Accept renegotiation request
9984 */
Paul Bakker48916f92012-09-16 19:57:18 +00009985
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009986 /* DTLS clients need to know renego is server-initiated */
9987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009988 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009989 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9990 {
9991 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9992 }
9993#endif
9994 ret = ssl_start_renegotiation( ssl );
9995 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9996 ret != 0 )
9997 {
9998 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9999 return( ret );
10000 }
10001 }
10002 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010003#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010004 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010005 /*
10006 * Refuse renegotiation
10007 */
10008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011#if defined(MBEDTLS_SSL_PROTO_SSL3)
10012 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010013 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010014 /* SSLv3 does not have a "no_renegotiation" warning, so
10015 we send a fatal alert and abort the connection. */
10016 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10017 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10018 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010019 }
10020 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10022#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10023 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10024 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10027 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10028 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010029 {
10030 return( ret );
10031 }
Paul Bakker48916f92012-09-16 19:57:18 +000010032 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010033 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10035 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010039 }
Paul Bakker48916f92012-09-16 19:57:18 +000010040 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010041
Hanno Becker90333da2017-10-10 11:27:13 +010010042 /* At this point, we don't know whether the renegotiation has been
10043 * completed or not. The cases to consider are the following:
10044 * 1) The renegotiation is complete. In this case, no new record
10045 * has been read yet.
10046 * 2) The renegotiation is incomplete because the client received
10047 * an application data record while awaiting the ServerHello.
10048 * 3) The renegotiation is incomplete because the client received
10049 * a non-handshake, non-application data message while awaiting
10050 * the ServerHello.
10051 * In each of these case, looping will be the proper action:
10052 * - For 1), the next iteration will read a new record and check
10053 * if it's application data.
10054 * - For 2), the loop condition isn't satisfied as application data
10055 * is present, hence continue is the same as break
10056 * - For 3), the loop condition is satisfied and read_record
10057 * will re-deliver the message that was held back by the client
10058 * when expecting the ServerHello.
10059 */
10060 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010061 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010062#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010064 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010065 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010066 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010067 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010070 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010072 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010073 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010074 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10078 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010081 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010082 }
10083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010088 }
10089
10090 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010091
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010092 /* We're going to return something now, cancel timer,
10093 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010095 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010096
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010097#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010098 /* If we requested renego but received AppData, resend HelloRequest.
10099 * Do it now, after setting in_offt, to avoid taking this branch
10100 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010102 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010104 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010105 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010108 return( ret );
10109 }
10110 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010112#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010113 }
10114
10115 n = ( len < ssl->in_msglen )
10116 ? len : ssl->in_msglen;
10117
10118 memcpy( buf, ssl->in_offt, n );
10119 ssl->in_msglen -= n;
10120
10121 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010122 {
10123 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010124 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010125 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010126 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010127 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010128 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010129 /* more data available */
10130 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010131 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010134
Paul Bakker23986e52011-04-24 08:57:21 +000010135 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010136}
10137
10138/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010139 * Send application data to be encrypted by the SSL layer, taking care of max
10140 * fragment length and buffer size.
10141 *
10142 * According to RFC 5246 Section 6.2.1:
10143 *
10144 * Zero-length fragments of Application data MAY be sent as they are
10145 * potentially useful as a traffic analysis countermeasure.
10146 *
10147 * Therefore, it is possible that the input message length is 0 and the
10148 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010149 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010150static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010151 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010152{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010153 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10154 const size_t max_len = (size_t) ret;
10155
10156 if( ret < 0 )
10157 {
10158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10159 return( ret );
10160 }
10161
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010162 if( len > max_len )
10163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010164#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010165 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010168 "maximum fragment length: %d > %d",
10169 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010171 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010172 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010173#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010174#if defined(MBEDTLS_SSL_PROTO_TLS)
10175 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010176 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010177 }
10178#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010179 }
Paul Bakker887bd502011-06-08 13:10:54 +000010180
Paul Bakker5121ce52009-01-03 21:22:43 +000010181 if( ssl->out_left != 0 )
10182 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010183 /*
10184 * The user has previously tried to send the data and
10185 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10186 * written. In this case, we expect the high-level write function
10187 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010189 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010192 return( ret );
10193 }
10194 }
Paul Bakker887bd502011-06-08 13:10:54 +000010195 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010196 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010197 /*
10198 * The user is trying to send a message the first time, so we need to
10199 * copy the data into the internal buffers and setup the data structure
10200 * to keep track of partial writes
10201 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010202 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010204 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010205
Hanno Becker67bc7c32018-08-06 11:33:50 +010010206 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010209 return( ret );
10210 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010211 }
10212
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010213 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010214}
10215
10216/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010217 * Write application data, doing 1/n-1 splitting if necessary.
10218 *
10219 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010220 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010221 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010224static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010225 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010226{
10227 int ret;
10228
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010229 if( ssl->conf->cbc_record_splitting ==
10230 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010231 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10233 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10234 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010235 {
10236 return( ssl_write_real( ssl, buf, len ) );
10237 }
10238
10239 if( ssl->split_done == 0 )
10240 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010241 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010242 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010243 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010244 }
10245
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010246 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10247 return( ret );
10248 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010249
10250 return( ret + 1 );
10251}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010253
10254/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010255 * Write application data (public-facing wrapper)
10256 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010257int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010258{
10259 int ret;
10260
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010262
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010263 if( ssl == NULL || ssl->conf == NULL )
10264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10265
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010266#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010267 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10268 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010269 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010270 return( ret );
10271 }
10272#endif
10273
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010274 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010275 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010276 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010277 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010279 return( ret );
10280 }
10281 }
10282
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010283#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010284 ret = ssl_write_split( ssl, buf, len );
10285#else
10286 ret = ssl_write_real( ssl, buf, len );
10287#endif
10288
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010290
10291 return( ret );
10292}
10293
10294/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010295 * Notify the peer that the connection is being closed
10296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010298{
10299 int ret;
10300
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010301 if( ssl == NULL || ssl->conf == NULL )
10302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010305
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010306 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010307 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10312 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10313 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010316 return( ret );
10317 }
10318 }
10319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010321
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010322 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010323}
10324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010326{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010327 if( transform == NULL )
10328 return;
10329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010331 deflateEnd( &transform->ctx_deflate );
10332 inflateEnd( &transform->ctx_inflate );
10333#endif
10334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10336 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010337
Hanno Becker92231322018-01-03 15:32:51 +000010338#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 mbedtls_md_free( &transform->md_ctx_enc );
10340 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010341#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010342
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010343 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010344}
10345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010346#if defined(MBEDTLS_X509_CRT_PARSE_C)
10347static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010350
10351 while( cur != NULL )
10352 {
10353 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010355 cur = next;
10356 }
10357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010359
Hanno Becker0271f962018-08-16 13:23:47 +010010360#if defined(MBEDTLS_SSL_PROTO_DTLS)
10361
10362static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10363{
10364 unsigned offset;
10365 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10366
10367 if( hs == NULL )
10368 return;
10369
Hanno Becker283f5ef2018-08-24 09:34:47 +010010370 ssl_free_buffered_record( ssl );
10371
Hanno Becker0271f962018-08-16 13:23:47 +010010372 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010373 ssl_buffering_free_slot( ssl, offset );
10374}
10375
10376static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10377 uint8_t slot )
10378{
10379 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10380 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010381
10382 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10383 return;
10384
Hanno Beckere605b192018-08-21 15:59:07 +010010385 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010386 {
Hanno Beckere605b192018-08-21 15:59:07 +010010387 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010388 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010389 mbedtls_free( hs_buf->data );
10390 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010391 }
10392}
10393
10394#endif /* MBEDTLS_SSL_PROTO_DTLS */
10395
Gilles Peskine9b562d52018-04-25 20:32:43 +020010396void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010397{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010398 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10399
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010400 if( handshake == NULL )
10401 return;
10402
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010403#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10404 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10405 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010406 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010407 handshake->async_in_progress = 0;
10408 }
10409#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10410
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010411#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10412 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10413 mbedtls_md5_free( &handshake->fin_md5 );
10414 mbedtls_sha1_free( &handshake->fin_sha1 );
10415#endif
10416#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10417#if defined(MBEDTLS_SHA256_C)
10418 mbedtls_sha256_free( &handshake->fin_sha256 );
10419#endif
10420#if defined(MBEDTLS_SHA512_C)
10421 mbedtls_sha512_free( &handshake->fin_sha512 );
10422#endif
10423#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425#if defined(MBEDTLS_DHM_C)
10426 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010428#if defined(MBEDTLS_ECDH_C)
10429 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010430#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010431#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010432 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010433#if defined(MBEDTLS_SSL_CLI_C)
10434 mbedtls_free( handshake->ecjpake_cache );
10435 handshake->ecjpake_cache = NULL;
10436 handshake->ecjpake_cache_len = 0;
10437#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010438#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010439
Janos Follath4ae5c292016-02-10 11:27:43 +000010440#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10441 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010442 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010444#endif
10445
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010446#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10447 if( handshake->psk != NULL )
10448 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010449 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010450 mbedtls_free( handshake->psk );
10451 }
10452#endif
10453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10455 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010456 /*
10457 * Free only the linked list wrapper, not the keys themselves
10458 * since the belong to the SNI callback
10459 */
10460 if( handshake->sni_key_cert != NULL )
10461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010463
10464 while( cur != NULL )
10465 {
10466 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010468 cur = next;
10469 }
10470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010472
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010473#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010474 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010475 if( handshake->ecrs_peer_cert != NULL )
10476 {
10477 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10478 mbedtls_free( handshake->ecrs_peer_cert );
10479 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010480#endif
10481
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010482#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10483 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10484 mbedtls_pk_free( &handshake->peer_pubkey );
10485#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487#if defined(MBEDTLS_SSL_PROTO_DTLS)
10488 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010489 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010490 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010491#endif
10492
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010493 mbedtls_platform_zeroize( handshake,
10494 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010495}
10496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010498{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010499 if( session == NULL )
10500 return;
10501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010502#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010503 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010504#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010505
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010506#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010507 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010508#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010510 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010511}
10512
Paul Bakker5121ce52009-01-03 21:22:43 +000010513/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010514 * Serialize a full SSL context
10515 */
10516int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10517 unsigned char *buf,
10518 size_t buf_len,
10519 size_t *olen )
10520{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010521 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010522 (void) ssl;
10523
10524 if( buf != NULL )
10525 memset( buf, 0, buf_len );
10526
10527 *olen = 0;
10528
10529 return( 0 );
10530}
10531
10532/*
10533 * Deserialize a full SSL context
10534 */
10535int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10536 const unsigned char *buf,
10537 size_t len )
10538{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010539 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010540 (void) ssl;
10541 (void) buf;
10542 (void) len;
10543
10544 return( 0 );
10545}
10546
10547/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010548 * Free an SSL context
10549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010550void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010551{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010552 if( ssl == NULL )
10553 return;
10554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010556
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010557 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010558 {
Angus Grattond8213d02016-05-25 20:56:48 +100010559 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010560 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010561 }
10562
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010563 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010564 {
Angus Grattond8213d02016-05-25 20:56:48 +100010565 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010566 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010567 }
10568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010569#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010570 if( ssl->compress_buf != NULL )
10571 {
Angus Grattond8213d02016-05-25 20:56:48 +100010572 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010573 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010574 }
10575#endif
10576
Paul Bakker48916f92012-09-16 19:57:18 +000010577 if( ssl->transform )
10578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579 mbedtls_ssl_transform_free( ssl->transform );
10580 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010581 }
10582
10583 if( ssl->handshake )
10584 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010585 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010586 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10587 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010589 mbedtls_free( ssl->handshake );
10590 mbedtls_free( ssl->transform_negotiate );
10591 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010592 }
10593
Paul Bakkerc0463502013-02-14 11:19:38 +010010594 if( ssl->session )
10595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596 mbedtls_ssl_session_free( ssl->session );
10597 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010598 }
10599
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010600#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010601 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010602 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010603 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010605 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010606#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10609 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10612 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010613 }
10614#endif
10615
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010616#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010618#endif
10619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010621
Paul Bakker86f04f42013-02-14 11:20:09 +010010622 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010623 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010624}
10625
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010626/*
10627 * Initialze mbedtls_ssl_config
10628 */
10629void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10630{
10631 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010632
10633#if !defined(MBEDTLS_SSL_PROTO_TLS)
10634 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10635#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010636}
10637
Simon Butcherc97b6972015-12-27 23:48:17 +000010638#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010639static int ssl_preset_default_hashes[] = {
10640#if defined(MBEDTLS_SHA512_C)
10641 MBEDTLS_MD_SHA512,
10642 MBEDTLS_MD_SHA384,
10643#endif
10644#if defined(MBEDTLS_SHA256_C)
10645 MBEDTLS_MD_SHA256,
10646 MBEDTLS_MD_SHA224,
10647#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010648#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010649 MBEDTLS_MD_SHA1,
10650#endif
10651 MBEDTLS_MD_NONE
10652};
Simon Butcherc97b6972015-12-27 23:48:17 +000010653#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010654
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010655static int ssl_preset_suiteb_ciphersuites[] = {
10656 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10657 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10658 0
10659};
10660
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010661#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010662static int ssl_preset_suiteb_hashes[] = {
10663 MBEDTLS_MD_SHA256,
10664 MBEDTLS_MD_SHA384,
10665 MBEDTLS_MD_NONE
10666};
10667#endif
10668
10669#if defined(MBEDTLS_ECP_C)
10670static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10671 MBEDTLS_ECP_DP_SECP256R1,
10672 MBEDTLS_ECP_DP_SECP384R1,
10673 MBEDTLS_ECP_DP_NONE
10674};
10675#endif
10676
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010677/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010678 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010679 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010680int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010681 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010682{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010683#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010684 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010685#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010686
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010687 /* Use the functions here so that they are covered in tests,
10688 * but otherwise access member directly for efficiency */
10689 mbedtls_ssl_conf_endpoint( conf, endpoint );
10690 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010691
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010692 /*
10693 * Things that are common to all presets
10694 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010695#if defined(MBEDTLS_SSL_CLI_C)
10696 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10697 {
10698 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10699#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10700 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10701#endif
10702 }
10703#endif
10704
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010705#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010706 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010707#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010708
10709#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10710 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10711#endif
10712
10713#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10714 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010715 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010716 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010717#endif
10718
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010719#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10720 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10721#endif
10722
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010723#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010724 conf->f_cookie_write = ssl_cookie_write_dummy;
10725 conf->f_cookie_check = ssl_cookie_check_dummy;
10726#endif
10727
10728#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10729 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10730#endif
10731
Janos Follath088ce432017-04-10 12:42:31 +010010732#if defined(MBEDTLS_SSL_SRV_C)
10733 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10734#endif
10735
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010736#if defined(MBEDTLS_SSL_PROTO_DTLS)
10737 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10738 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10739#endif
10740
10741#if defined(MBEDTLS_SSL_RENEGOTIATION)
10742 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010743 memset( conf->renego_period, 0x00, 2 );
10744 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010745#endif
10746
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010747#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10748 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10749 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010750 const unsigned char dhm_p[] =
10751 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10752 const unsigned char dhm_g[] =
10753 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10754
Hanno Beckera90658f2017-10-04 15:29:08 +010010755 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10756 dhm_p, sizeof( dhm_p ),
10757 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010758 {
10759 return( ret );
10760 }
10761 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010762#endif
10763
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010764 /*
10765 * Preset-specific defaults
10766 */
10767 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010768 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010769 /*
10770 * NSA Suite B
10771 */
10772 case MBEDTLS_SSL_PRESET_SUITEB:
10773 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10774 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10775 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10776 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10777
10778 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10779 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10780 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10781 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10782 ssl_preset_suiteb_ciphersuites;
10783
10784#if defined(MBEDTLS_X509_CRT_PARSE_C)
10785 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010786#endif
10787
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010788#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010789 conf->sig_hashes = ssl_preset_suiteb_hashes;
10790#endif
10791
10792#if defined(MBEDTLS_ECP_C)
10793 conf->curve_list = ssl_preset_suiteb_curves;
10794#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010795 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010796
10797 /*
10798 * Default
10799 */
10800 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010801 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10802 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10803 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10804 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10805 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10806 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10807 MBEDTLS_SSL_MIN_MINOR_VERSION :
10808 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010809 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10810 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10811
10812#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010813 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010814 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10815#endif
10816
10817 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10818 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10819 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10820 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10821 mbedtls_ssl_list_ciphersuites();
10822
10823#if defined(MBEDTLS_X509_CRT_PARSE_C)
10824 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10825#endif
10826
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010827#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010828 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010829#endif
10830
10831#if defined(MBEDTLS_ECP_C)
10832 conf->curve_list = mbedtls_ecp_grp_id_list();
10833#endif
10834
10835#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10836 conf->dhm_min_bitlen = 1024;
10837#endif
10838 }
10839
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010840 return( 0 );
10841}
10842
10843/*
10844 * Free mbedtls_ssl_config
10845 */
10846void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10847{
10848#if defined(MBEDTLS_DHM_C)
10849 mbedtls_mpi_free( &conf->dhm_P );
10850 mbedtls_mpi_free( &conf->dhm_G );
10851#endif
10852
10853#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10854 if( conf->psk != NULL )
10855 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010856 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010857 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010858 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010859 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010860 }
10861
10862 if( conf->psk_identity != NULL )
10863 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010864 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010865 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010866 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010867 conf->psk_identity_len = 0;
10868 }
10869#endif
10870
10871#if defined(MBEDTLS_X509_CRT_PARSE_C)
10872 ssl_key_cert_free( conf->key_cert );
10873#endif
10874
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010875 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010876}
10877
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010878#if defined(MBEDTLS_PK_C) && \
10879 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010880/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010883unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010885#if defined(MBEDTLS_RSA_C)
10886 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10887 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010889#if defined(MBEDTLS_ECDSA_C)
10890 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10891 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010892#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010893 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010894}
10895
Hanno Becker7e5437a2017-04-28 17:15:26 +010010896unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10897{
10898 switch( type ) {
10899 case MBEDTLS_PK_RSA:
10900 return( MBEDTLS_SSL_SIG_RSA );
10901 case MBEDTLS_PK_ECDSA:
10902 case MBEDTLS_PK_ECKEY:
10903 return( MBEDTLS_SSL_SIG_ECDSA );
10904 default:
10905 return( MBEDTLS_SSL_SIG_ANON );
10906 }
10907}
10908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010909mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010910{
10911 switch( sig )
10912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913#if defined(MBEDTLS_RSA_C)
10914 case MBEDTLS_SSL_SIG_RSA:
10915 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917#if defined(MBEDTLS_ECDSA_C)
10918 case MBEDTLS_SSL_SIG_ECDSA:
10919 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010920#endif
10921 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010923 }
10924}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010925#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010926
Hanno Becker7e5437a2017-04-28 17:15:26 +010010927#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10928 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10929
10930/* Find an entry in a signature-hash set matching a given hash algorithm. */
10931mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10932 mbedtls_pk_type_t sig_alg )
10933{
10934 switch( sig_alg )
10935 {
10936 case MBEDTLS_PK_RSA:
10937 return( set->rsa );
10938 case MBEDTLS_PK_ECDSA:
10939 return( set->ecdsa );
10940 default:
10941 return( MBEDTLS_MD_NONE );
10942 }
10943}
10944
10945/* Add a signature-hash-pair to a signature-hash set */
10946void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10947 mbedtls_pk_type_t sig_alg,
10948 mbedtls_md_type_t md_alg )
10949{
10950 switch( sig_alg )
10951 {
10952 case MBEDTLS_PK_RSA:
10953 if( set->rsa == MBEDTLS_MD_NONE )
10954 set->rsa = md_alg;
10955 break;
10956
10957 case MBEDTLS_PK_ECDSA:
10958 if( set->ecdsa == MBEDTLS_MD_NONE )
10959 set->ecdsa = md_alg;
10960 break;
10961
10962 default:
10963 break;
10964 }
10965}
10966
10967/* Allow exactly one hash algorithm for each signature. */
10968void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10969 mbedtls_md_type_t md_alg )
10970{
10971 set->rsa = md_alg;
10972 set->ecdsa = md_alg;
10973}
10974
10975#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10976 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10977
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010978/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010979 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010981mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010982{
10983 switch( hash )
10984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010985#if defined(MBEDTLS_MD5_C)
10986 case MBEDTLS_SSL_HASH_MD5:
10987 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010988#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010989#if defined(MBEDTLS_SHA1_C)
10990 case MBEDTLS_SSL_HASH_SHA1:
10991 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993#if defined(MBEDTLS_SHA256_C)
10994 case MBEDTLS_SSL_HASH_SHA224:
10995 return( MBEDTLS_MD_SHA224 );
10996 case MBEDTLS_SSL_HASH_SHA256:
10997 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010998#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010999#if defined(MBEDTLS_SHA512_C)
11000 case MBEDTLS_SSL_HASH_SHA384:
11001 return( MBEDTLS_MD_SHA384 );
11002 case MBEDTLS_SSL_HASH_SHA512:
11003 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011004#endif
11005 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011007 }
11008}
11009
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011010/*
11011 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11012 */
11013unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11014{
11015 switch( md )
11016 {
11017#if defined(MBEDTLS_MD5_C)
11018 case MBEDTLS_MD_MD5:
11019 return( MBEDTLS_SSL_HASH_MD5 );
11020#endif
11021#if defined(MBEDTLS_SHA1_C)
11022 case MBEDTLS_MD_SHA1:
11023 return( MBEDTLS_SSL_HASH_SHA1 );
11024#endif
11025#if defined(MBEDTLS_SHA256_C)
11026 case MBEDTLS_MD_SHA224:
11027 return( MBEDTLS_SSL_HASH_SHA224 );
11028 case MBEDTLS_MD_SHA256:
11029 return( MBEDTLS_SSL_HASH_SHA256 );
11030#endif
11031#if defined(MBEDTLS_SHA512_C)
11032 case MBEDTLS_MD_SHA384:
11033 return( MBEDTLS_SSL_HASH_SHA384 );
11034 case MBEDTLS_MD_SHA512:
11035 return( MBEDTLS_SSL_HASH_SHA512 );
11036#endif
11037 default:
11038 return( MBEDTLS_SSL_HASH_NONE );
11039 }
11040}
11041
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011042#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011043/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011044 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011045 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011046 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011047int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011050
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011051 if( ssl->conf->curve_list == NULL )
11052 return( -1 );
11053
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011054 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011055 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011056 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011057
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011058 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011059}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011060#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011061
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011062#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011063/*
11064 * Check if a hash proposed by the peer is in our list.
11065 * Return 0 if we're willing to use it, -1 otherwise.
11066 */
11067int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11068 mbedtls_md_type_t md )
11069{
11070 const int *cur;
11071
11072 if( ssl->conf->sig_hashes == NULL )
11073 return( -1 );
11074
11075 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11076 if( *cur == (int) md )
11077 return( 0 );
11078
11079 return( -1 );
11080}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011081#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083#if defined(MBEDTLS_X509_CRT_PARSE_C)
11084int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11085 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011086 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011087 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011088{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011089 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011090#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011091 int usage = 0;
11092#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011093#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011094 const char *ext_oid;
11095 size_t ext_len;
11096#endif
11097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011098#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11099 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011100 ((void) cert);
11101 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011102 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011103#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011105#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11106 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011107 {
11108 /* Server part of the key exchange */
11109 switch( ciphersuite->key_exchange )
11110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011111 case MBEDTLS_KEY_EXCHANGE_RSA:
11112 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011113 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011114 break;
11115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011116 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11117 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11118 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11119 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011120 break;
11121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011122 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11123 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011124 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011125 break;
11126
11127 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011128 case MBEDTLS_KEY_EXCHANGE_NONE:
11129 case MBEDTLS_KEY_EXCHANGE_PSK:
11130 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11131 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011132 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011133 usage = 0;
11134 }
11135 }
11136 else
11137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011138 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11139 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011140 }
11141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011142 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011143 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011144 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011145 ret = -1;
11146 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011147#else
11148 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011149#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011151#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11152 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011154 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11155 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011156 }
11157 else
11158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011159 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11160 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011161 }
11162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011163 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011164 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011165 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011166 ret = -1;
11167 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011168#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011169
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011170 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011172#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011173
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011174/*
11175 * Convert version numbers to/from wire format
11176 * and, for DTLS, to/from TLS equivalent.
11177 *
11178 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011179 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011180 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11181 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011183void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011184 unsigned char ver[2] )
11185{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011186#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11187 ((void) transport);
11188#endif
11189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011190#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011191 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011193 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011194 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11195
11196 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11197 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11198 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011199 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011200#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011201#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011202 {
11203 ver[0] = (unsigned char) major;
11204 ver[1] = (unsigned char) minor;
11205 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011206#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011207}
11208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011209void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011210 const unsigned char ver[2] )
11211{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011212#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11213 ((void) transport);
11214#endif
11215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011216#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011217 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011218 {
11219 *major = 255 - ver[0] + 2;
11220 *minor = 255 - ver[1] + 1;
11221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011223 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11224 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011225 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011226#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011227#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011228 {
11229 *major = ver[0];
11230 *minor = ver[1];
11231 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011232#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011233}
11234
Simon Butcher99000142016-10-13 17:21:01 +010011235int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11236{
11237#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11238 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11239 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11240
11241 switch( md )
11242 {
11243#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11244#if defined(MBEDTLS_MD5_C)
11245 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011246 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011247#endif
11248#if defined(MBEDTLS_SHA1_C)
11249 case MBEDTLS_SSL_HASH_SHA1:
11250 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11251 break;
11252#endif
11253#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11254#if defined(MBEDTLS_SHA512_C)
11255 case MBEDTLS_SSL_HASH_SHA384:
11256 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11257 break;
11258#endif
11259#if defined(MBEDTLS_SHA256_C)
11260 case MBEDTLS_SSL_HASH_SHA256:
11261 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11262 break;
11263#endif
11264 default:
11265 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11266 }
11267
11268 return 0;
11269#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11270 (void) ssl;
11271 (void) md;
11272
11273 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11274#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11275}
11276
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011277#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11278 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11279int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11280 unsigned char *output,
11281 unsigned char *data, size_t data_len )
11282{
11283 int ret = 0;
11284 mbedtls_md5_context mbedtls_md5;
11285 mbedtls_sha1_context mbedtls_sha1;
11286
11287 mbedtls_md5_init( &mbedtls_md5 );
11288 mbedtls_sha1_init( &mbedtls_sha1 );
11289
11290 /*
11291 * digitally-signed struct {
11292 * opaque md5_hash[16];
11293 * opaque sha_hash[20];
11294 * };
11295 *
11296 * md5_hash
11297 * MD5(ClientHello.random + ServerHello.random
11298 * + ServerParams);
11299 * sha_hash
11300 * SHA(ClientHello.random + ServerHello.random
11301 * + ServerParams);
11302 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011303 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011304 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011306 goto exit;
11307 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011308 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011309 ssl->handshake->randbytes, 64 ) ) != 0 )
11310 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011312 goto exit;
11313 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011314 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011315 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011317 goto exit;
11318 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011319 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011320 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011322 goto exit;
11323 }
11324
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011325 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011326 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011328 goto exit;
11329 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011330 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011331 ssl->handshake->randbytes, 64 ) ) != 0 )
11332 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011334 goto exit;
11335 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011336 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011337 data_len ) ) != 0 )
11338 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011340 goto exit;
11341 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011342 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011343 output + 16 ) ) != 0 )
11344 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011346 goto exit;
11347 }
11348
11349exit:
11350 mbedtls_md5_free( &mbedtls_md5 );
11351 mbedtls_sha1_free( &mbedtls_sha1 );
11352
11353 if( ret != 0 )
11354 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11355 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11356
11357 return( ret );
11358
11359}
11360#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11361 MBEDTLS_SSL_PROTO_TLS1_1 */
11362
11363#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11364 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11365int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011366 unsigned char *hash, size_t *hashlen,
11367 unsigned char *data, size_t data_len,
11368 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011369{
11370 int ret = 0;
11371 mbedtls_md_context_t ctx;
11372 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011373 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011374
11375 mbedtls_md_init( &ctx );
11376
11377 /*
11378 * digitally-signed struct {
11379 * opaque client_random[32];
11380 * opaque server_random[32];
11381 * ServerDHParams params;
11382 * };
11383 */
11384 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11385 {
11386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11387 goto exit;
11388 }
11389 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11390 {
11391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11392 goto exit;
11393 }
11394 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11395 {
11396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11397 goto exit;
11398 }
11399 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11400 {
11401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11402 goto exit;
11403 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011404 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011405 {
11406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11407 goto exit;
11408 }
11409
11410exit:
11411 mbedtls_md_free( &ctx );
11412
11413 if( ret != 0 )
11414 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11415 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11416
11417 return( ret );
11418}
11419#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11420 MBEDTLS_SSL_PROTO_TLS1_2 */
11421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011422#endif /* MBEDTLS_SSL_TLS_C */