blob: f1075cbf1771dab11f6cabe6e1ad530d8ccbb75c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Hanno Becker58fccf22019-02-06 14:30:46 +0000371int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
372 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000378
379#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380 if( src->peer_cert != NULL )
381 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200382 int ret;
383
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200384 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200391 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 dst->peer_cert = NULL;
395 return( ret );
396 }
397 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100398#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000399 if( src->peer_cert_digest != NULL )
400 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000401 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000402 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000409 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000410 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200414
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200415#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200416 if( src->ticket != NULL )
417 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200418 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200419 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200421
422 memcpy( dst->ticket, src->ticket, src->ticket_len );
423 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200424#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200425
426 return( 0 );
427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
430int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200431 const unsigned char *key_enc, const unsigned char *key_dec,
432 size_t keylen,
433 const unsigned char *iv_enc, const unsigned char *iv_dec,
434 size_t ivlen,
435 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200436 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
438int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
441int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
442#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444/*
445 * Key material generation
446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200448static int ssl3_prf( const unsigned char *secret, size_t slen,
449 const char *label,
450 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000451 unsigned char *dstbuf, size_t dlen )
452{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100453 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000454 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200455 mbedtls_md5_context md5;
456 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000457 unsigned char padding[16];
458 unsigned char sha1sum[20];
459 ((void)label);
460
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200461 mbedtls_md5_init( &md5 );
462 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200463
Paul Bakker5f70b252012-09-13 14:23:06 +0000464 /*
465 * SSLv3:
466 * block =
467 * MD5( secret + SHA1( 'A' + secret + random ) ) +
468 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
469 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
470 * ...
471 */
472 for( i = 0; i < dlen / 16; i++ )
473 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200474 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000475
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100478 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100479 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100480 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100482 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100484 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100485 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100489 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100490 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000495 }
496
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200498 mbedtls_md5_free( &md5 );
499 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000500
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500501 mbedtls_platform_zeroize( padding, sizeof( padding ) );
502 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000503
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100504 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200509static int tls1_prf( const unsigned char *secret, size_t slen,
510 const char *label,
511 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000512 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000513{
Paul Bakker23986e52011-04-24 08:57:21 +0000514 size_t nb, hs;
515 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200516 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 unsigned char tmp[128];
518 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_md_info_t *md_info;
520 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100521 int ret;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 hs = ( slen + 1 ) / 2;
529 S1 = secret;
530 S2 = secret + slen - hs;
531
532 nb = strlen( label );
533 memcpy( tmp + 20, label, nb );
534 memcpy( tmp + 20 + nb, random, rlen );
535 nb += rlen;
536
537 /*
538 * First compute P_md5(secret,label+random)[0..dlen]
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544 return( ret );
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
547 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
548 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 for( i = 0; i < dlen; i += 16 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md_hmac_reset ( &md_ctx );
553 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
554 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_reset ( &md_ctx );
557 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
558 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
561
562 for( j = 0; j < k; j++ )
563 dstbuf[i + j] = h_i[j];
564 }
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 /*
569 * XOR out with P_sha1(secret,label+random)[0..dlen]
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100575 return( ret );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
578 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
579 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 for( i = 0; i < dlen; i += 20 )
582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_md_hmac_reset ( &md_ctx );
584 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
585 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_md_hmac_reset ( &md_ctx );
588 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
589 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
592
593 for( j = 0; j < k; j++ )
594 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
595 }
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500599 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
600 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602 return( 0 );
603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
607static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608 const unsigned char *secret, size_t slen,
609 const char *label,
610 const unsigned char *random, size_t rlen,
611 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612{
613 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100614 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
617 const mbedtls_md_info_t *md_info;
618 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 int ret;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627
628 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000630
631 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100632 memcpy( tmp + md_len, label, nb );
633 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634 nb += rlen;
635
636 /*
637 * Compute P_<hash>(secret, label + random)[0..dlen]
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100640 return( ret );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
643 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
644 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100645
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100646 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_md_hmac_reset ( &md_ctx );
649 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
650 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_md_hmac_reset ( &md_ctx );
653 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
654 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000655
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000657
658 for( j = 0; j < k; j++ )
659 dstbuf[i + j] = h_i[j];
660 }
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100663
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500664 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
665 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000666
667 return( 0 );
668}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100671static int tls_prf_sha256( const unsigned char *secret, size_t slen,
672 const char *label,
673 const unsigned char *random, size_t rlen,
674 unsigned char *dstbuf, size_t dlen )
675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100677 label, random, rlen, dstbuf, dlen ) );
678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200682static int tls_prf_sha384( const unsigned char *secret, size_t slen,
683 const char *label,
684 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000685 unsigned char *dstbuf, size_t dlen )
686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100688 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_SHA512_C */
691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
696 defined(MBEDTLS_SSL_PROTO_TLS1_1)
697static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200698#endif
Paul Bakker380da532012-04-18 16:10:25 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200701static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200703#endif
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200706static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200708#endif
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
711#if defined(MBEDTLS_SHA256_C)
712static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200713static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200715#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SHA512_C)
718static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200719static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000723
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200724/* Type for the TLS PRF */
725typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
726 const unsigned char *, size_t,
727 unsigned char *, size_t);
728
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200729/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200730 * Populate a transform structure with session keys and all the other
731 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200732 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200733 * Parameters:
734 * - [in/out]: transform: structure to populate
735 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200736 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200737 * - [in] ciphersuite
738 * - [in] master
739 * - [in] encrypt_then_mac
740 * - [in] trunc_hmac
741 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742 * - [in] tls_prf: pointer to PRF to use for key derivation
743 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200744 * - [in] minor_ver: SSL/TLS minor version
745 * - [in] endpoint: client or server
746 * - [in] ssl: optionally used for:
747 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
748 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
749 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200750 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200752 int ciphersuite,
753 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100754#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
756 int encrypt_then_mac,
757#endif
758#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
759 int trunc_hmac,
760#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100761#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200762#if defined(MBEDTLS_ZLIB_SUPPORT)
763 int compression,
764#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200765 ssl_tls_prf_t tls_prf,
766 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200767 int minor_ver,
768 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200769 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000770{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 unsigned char keyblk[256];
773 unsigned char *key1;
774 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100775 unsigned char *mac_enc;
776 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000777 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200778 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000779 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 const mbedtls_cipher_info_t *cipher_info;
782 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100783
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200784#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
785 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
786 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200787 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200788 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000789#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000790
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200791 /* Copy info about negotiated version and extensions */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100792#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
793 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200794 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000795#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200796 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 /*
799 * Get various info structures
800 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 if( ciphersuite_info == NULL )
803 {
804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200805 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
807 }
808
Hanno Becker8759e162017-12-27 21:34:08 +0000809 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100810 if( cipher_info == NULL )
811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000813 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100815 }
816
Hanno Becker8759e162017-12-27 21:34:08 +0000817 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100818 if( md_info == NULL )
819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000821 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100823 }
824
Hanno Beckera5a2b082019-05-15 14:03:01 +0100825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100826 /* Copy own and peer's CID if the use of the CID
827 * extension has been negotiated. */
828 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
829 {
830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100831
Hanno Becker4932f9f2019-05-03 15:23:51 +0100832 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100833 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100834 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100835 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100836
837 transform->out_cid_len = ssl->handshake->peer_cid_len;
838 memcpy( transform->out_cid, ssl->handshake->peer_cid,
839 ssl->handshake->peer_cid_len );
840 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
841 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100842 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100843#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100844
Paul Bakker5121ce52009-01-03 21:22:43 +0000845 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200846 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000847 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200848 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100849 if( ret != 0 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100852 return( ret );
853 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200856 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200857 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200858 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 /*
862 * Determine the appropriate key, IV and MAC length.
863 */
Paul Bakker68884e32013-01-07 18:20:04 +0100864
Hanno Beckere7f2df02017-12-27 08:17:40 +0000865 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
Hanno Beckerf1229442018-01-03 15:32:31 +0000867#if defined(MBEDTLS_GCM_C) || \
868 defined(MBEDTLS_CCM_C) || \
869 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871 cipher_info->mode == MBEDTLS_MODE_CCM ||
872 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 {
Hanno Becker8759e162017-12-27 21:34:08 +0000874 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200875
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000877 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000878 transform->taglen =
879 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200880
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200881 /* All modes haves 96-bit IVs;
882 * GCM and CCM has 4 implicit and 8 explicit bytes
883 * ChachaPoly has all 12 bytes implicit
884 */
Paul Bakker68884e32013-01-07 18:20:04 +0100885 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200886 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
887 transform->fixed_ivlen = 12;
888 else
889 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200891 /* Minimum length of encrypted record */
892 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000893 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100894 }
895 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000896#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
897#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
898 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
899 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200901 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
903 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100907 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 mac_key_len = mbedtls_md_get_size( md_info );
911 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200914 /*
915 * If HMAC is to be truncated, we shall keep the leftmost bytes,
916 * (rfc 6066 page 13 or rfc 2104 section 4),
917 * so we only need to adjust the length here.
918 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200919 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000922
923#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
924 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000925 * HMAC implementation which also truncates the key
926 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000927 mac_key_len = transform->maclen;
928#endif
929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931
932 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100933 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200935 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937 transform->minlen = transform->maclen;
938 else
Paul Bakker68884e32013-01-07 18:20:04 +0100939 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940 /*
941 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100942 * 1. if EtM is in use: one block plus MAC
943 * otherwise: * first multiple of blocklen greater than maclen
944 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200947 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100948 {
949 transform->minlen = transform->maclen
950 + cipher_info->block_size;
951 }
952 else
953#endif
954 {
955 transform->minlen = transform->maclen
956 + cipher_info->block_size
957 - transform->maclen % cipher_info->block_size;
958 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200961 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
962 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200963 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100964 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200967 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
968 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200969 {
970 transform->minlen += transform->ivlen;
971 }
972 else
973#endif
974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200977 }
Paul Bakker68884e32013-01-07 18:20:04 +0100978 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000980 else
981#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
985 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Hanno Beckere7f2df02017-12-27 08:17:40 +0000987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
988 (unsigned) keylen,
989 (unsigned) transform->minlen,
990 (unsigned) transform->ivlen,
991 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
993 /*
994 * Finally setup the cipher contexts, IVs and MAC secrets.
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200997 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001000 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker68884e32013-01-07 18:20:04 +01001002 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001005 /*
1006 * This is not used in TLS v1.1.
1007 */
Paul Bakker48916f92012-09-16 19:57:18 +00001008 iv_copy_len = ( transform->fixed_ivlen ) ?
1009 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001010 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1011 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 }
1014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_SSL_CLI_C */
1016#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001017 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001019 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker81c7b182017-11-09 18:39:33 +00001022 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001023 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001025 /*
1026 * This is not used in TLS v1.1.
1027 */
Paul Bakker48916f92012-09-16 19:57:18 +00001028 iv_copy_len = ( transform->fixed_ivlen ) ?
1029 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001030 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1031 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001032 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Hanno Becker92231322018-01-03 15:32:51 +00001041#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001043 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001044 {
Hanno Becker92231322018-01-03 15:32:51 +00001045 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001049 }
1050
Hanno Becker81c7b182017-11-09 18:39:33 +00001051 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1052 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001053 }
1054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1056#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1057 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001058 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001059 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001060 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1061 For AEAD-based ciphersuites, there is nothing to do here. */
1062 if( mac_key_len != 0 )
1063 {
1064 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1065 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1066 }
Paul Bakker68884e32013-01-07 18:20:04 +01001067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001068 else
1069#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001073 }
Hanno Becker92231322018-01-03 15:32:51 +00001074#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1077 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001078 {
1079 int ret = 0;
1080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001082
Hanno Beckere7f2df02017-12-27 08:17:40 +00001083 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001084 transform->iv_enc, transform->iv_dec,
1085 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001086 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001087 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001091 }
1092 }
Hanno Becker92231322018-01-03 15:32:51 +00001093#else
1094 ((void) mac_dec);
1095 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001097
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001098#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1099 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001101 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001102 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001103 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001104 iv_copy_len );
1105 }
1106#endif
1107
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001108 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 return( ret );
1113 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001114
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001115 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 cipher_info ) ) != 0 )
1117 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001119 return( ret );
1120 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001123 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127 return( ret );
1128 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001131 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001135 return( ret );
1136 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_CIPHER_MODE_CBC)
1139 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1142 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001145 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001146 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1149 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001152 return( ret );
1153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001157 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001159 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001161 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001164
Paul Bakker48916f92012-09-16 19:57:18 +00001165 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1166 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001168 if( deflateInit( &transform->ctx_deflate,
1169 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001170 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1173 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001174 }
1175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001177
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 return( 0 );
1179}
1180
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001181/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001182 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001183 *
1184 * Inputs:
1185 * - SSL/TLS minor version
1186 * - hash associated with the ciphersuite (only used by TLS 1.2)
1187 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001188 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001189 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1190 */
1191static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1192 int minor_ver,
1193 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001195#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1196 (void) hash;
1197#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001198
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001201 {
1202 handshake->tls_prf = ssl3_prf;
1203 handshake->calc_verify = ssl_calc_verify_ssl;
1204 handshake->calc_finished = ssl_calc_finished_ssl;
1205 }
1206 else
1207#endif
1208#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001209 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001210 {
1211 handshake->tls_prf = tls1_prf;
1212 handshake->calc_verify = ssl_calc_verify_tls;
1213 handshake->calc_finished = ssl_calc_finished_tls;
1214 }
1215 else
1216#endif
1217#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1218#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001219 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1220 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001221 {
1222 handshake->tls_prf = tls_prf_sha384;
1223 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1224 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1225 }
1226 else
1227#endif
1228#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001229 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001230 {
1231 handshake->tls_prf = tls_prf_sha256;
1232 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1233 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1234 }
1235 else
1236#endif
1237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1238 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1240 }
1241
1242 return( 0 );
1243}
1244
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001245/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001246 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001247 *
1248 * Parameters:
1249 * [in/out] handshake
1250 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1251 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 * [out] master
1253 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001255static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001257 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001258{
1259 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001260
1261#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001262 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001263 (void) ssl;
1264#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001265
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001266 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001267 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1269 return( 0 );
1270 }
1271
1272 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1273 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001274
1275#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001276 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001277 {
1278 unsigned char session_hash[48];
1279 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001280
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001281 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001282
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001283 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1284 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001285
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001286 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001287 "extended master secret",
1288 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001289 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001290 }
1291 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001292#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001293 {
1294 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1295 "master secret",
1296 handshake->randbytes, 64,
1297 master, 48 );
1298 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001299 if( ret != 0 )
1300 {
1301 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1302 return( ret );
1303 }
1304
1305 mbedtls_platform_zeroize( handshake->premaster,
1306 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001307
1308 return( 0 );
1309}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001310
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001311int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1312{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001313 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001314 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1315 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001316
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1318
1319 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001320 ret = ssl_set_handshake_prfs( ssl->handshake,
1321 ssl->minor_ver,
1322 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001323 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001324 {
1325 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001326 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001327 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001328
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001329 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001330 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001331 ssl->session_negotiate->master,
1332 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001333 if( ret != 0 )
1334 {
1335 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1336 return( ret );
1337 }
1338
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001339 /* Swap the client and server random values:
1340 * - MS derivation wanted client+server (RFC 5246 8.1)
1341 * - key derivation wants server+client (RFC 5246 6.3) */
1342 {
1343 unsigned char tmp[64];
1344 memcpy( tmp, ssl->handshake->randbytes, 64 );
1345 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1346 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1347 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1348 }
1349
1350 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001351 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001352 ssl->session_negotiate->ciphersuite,
1353 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001354#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001355#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1356 ssl->session_negotiate->encrypt_then_mac,
1357#endif
1358#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1359 ssl->session_negotiate->trunc_hmac,
1360#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001361#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001362#if defined(MBEDTLS_ZLIB_SUPPORT)
1363 ssl->session_negotiate->compression,
1364#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001365 ssl->handshake->tls_prf,
1366 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001367 ssl->minor_ver,
1368 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001369 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001370 if( ret != 0 )
1371 {
1372 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1373 return( ret );
1374 }
1375
1376 /* We no longer need Server/ClientHello.random values */
1377 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1378 sizeof( ssl->handshake->randbytes ) );
1379
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001380 /* Allocate compression buffer */
1381#if defined(MBEDTLS_ZLIB_SUPPORT)
1382 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1383 ssl->compress_buf == NULL )
1384 {
1385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1386 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1387 if( ssl->compress_buf == NULL )
1388 {
1389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001390 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001391 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1392 }
1393 }
1394#endif
1395
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1397
1398 return( 0 );
1399}
1400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001402void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1403 unsigned char hash[36],
1404 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001405{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001406 mbedtls_md5_context md5;
1407 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 unsigned char pad_1[48];
1409 unsigned char pad_2[48];
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001413 mbedtls_md5_init( &md5 );
1414 mbedtls_sha1_init( &sha1 );
1415
1416 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1417 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Paul Bakker380da532012-04-18 16:10:25 +00001419 memset( pad_1, 0x36, 48 );
1420 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001422 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1423 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1424 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001426 mbedtls_md5_starts_ret( &md5 );
1427 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1428 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1429 mbedtls_md5_update_ret( &md5, hash, 16 );
1430 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001432 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1433 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1434 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001435
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001436 mbedtls_sha1_starts_ret( &sha1 );
1437 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1438 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1439 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1440 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001441
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001442 *hlen = 36;
1443
1444 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001447 mbedtls_md5_free( &md5 );
1448 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001449
Paul Bakker380da532012-04-18 16:10:25 +00001450 return;
1451}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001455void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1456 unsigned char hash[36],
1457 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001458{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001459 mbedtls_md5_context md5;
1460 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001463
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001464 mbedtls_md5_init( &md5 );
1465 mbedtls_sha1_init( &sha1 );
1466
1467 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1468 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001469
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001470 mbedtls_md5_finish_ret( &md5, hash );
1471 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001473 *hlen = 36;
1474
1475 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001477
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001478 mbedtls_md5_free( &md5 );
1479 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001480
Paul Bakker380da532012-04-18 16:10:25 +00001481 return;
1482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1486#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001487void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1488 unsigned char hash[32],
1489 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001490{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001491 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001492
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001493 mbedtls_sha256_init( &sha256 );
1494
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001496
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001497 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001498 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001500 *hlen = 32;
1501
1502 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001504
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001505 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001506
Paul Bakker380da532012-04-18 16:10:25 +00001507 return;
1508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001512void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1513 unsigned char hash[48],
1514 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001515{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001516 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001517
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001518 mbedtls_sha512_init( &sha512 );
1519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001521
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001522 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001523 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001525 *hlen = 48;
1526
1527 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001530 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001531
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 return;
1533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#endif /* MBEDTLS_SHA512_C */
1535#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1538int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001539{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001540 unsigned char *p = ssl->handshake->premaster;
1541 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001542 const unsigned char *psk = ssl->conf->psk;
1543 size_t psk_len = ssl->conf->psk_len;
1544
1545 /* If the psk callback was called, use its result */
1546 if( ssl->handshake->psk != NULL )
1547 {
1548 psk = ssl->handshake->psk;
1549 psk_len = ssl->handshake->psk_len;
1550 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001551
1552 /*
1553 * PMS = struct {
1554 * opaque other_secret<0..2^16-1>;
1555 * opaque psk<0..2^16-1>;
1556 * };
1557 * with "other_secret" depending on the particular key exchange
1558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1560 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001561 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001562 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001564
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001565 *(p++) = (unsigned char)( psk_len >> 8 );
1566 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001567
1568 if( end < p || (size_t)( end - p ) < psk_len )
1569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1570
1571 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001572 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001573 }
1574 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1576#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1577 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001578 {
1579 /*
1580 * other_secret already set by the ClientKeyExchange message,
1581 * and is 48 bytes long
1582 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001583 if( end - p < 2 )
1584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1585
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001586 *p++ = 0;
1587 *p++ = 48;
1588 p += 48;
1589 }
1590 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1592#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1593 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001594 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001595 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001596 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001598 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001600 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001601 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604 return( ret );
1605 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001606 *(p++) = (unsigned char)( len >> 8 );
1607 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001608 p += len;
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 }
1612 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1614#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1615 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001617 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 size_t zlen;
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001621 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001622 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001625 return( ret );
1626 }
1627
1628 *(p++) = (unsigned char)( zlen >> 8 );
1629 *(p++) = (unsigned char)( zlen );
1630 p += zlen;
1631
Janos Follath3fbdada2018-08-15 10:26:53 +01001632 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1633 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634 }
1635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1639 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001640 }
1641
1642 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001643 if( end - p < 2 )
1644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001645
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001646 *(p++) = (unsigned char)( psk_len >> 8 );
1647 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001648
1649 if( end < p || (size_t)( end - p ) < psk_len )
1650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1651
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001652 memcpy( p, psk, psk_len );
1653 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001654
1655 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1656
1657 return( 0 );
1658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001662/*
1663 * SSLv3.0 MAC functions
1664 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001665#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001666static void ssl_mac( mbedtls_md_context_t *md_ctx,
1667 const unsigned char *secret,
1668 const unsigned char *buf, size_t len,
1669 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001670 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001671{
1672 unsigned char header[11];
1673 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001674 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1676 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001677
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001678 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001680 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001681 else
Paul Bakker68884e32013-01-07 18:20:04 +01001682 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
1684 memcpy( header, ctr, 8 );
1685 header[ 8] = (unsigned char) type;
1686 header[ 9] = (unsigned char)( len >> 8 );
1687 header[10] = (unsigned char)( len );
1688
Paul Bakker68884e32013-01-07 18:20:04 +01001689 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_md_starts( md_ctx );
1691 mbedtls_md_update( md_ctx, secret, md_size );
1692 mbedtls_md_update( md_ctx, padding, padlen );
1693 mbedtls_md_update( md_ctx, header, 11 );
1694 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001695 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
Paul Bakker68884e32013-01-07 18:20:04 +01001697 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_md_starts( md_ctx );
1699 mbedtls_md_update( md_ctx, secret, md_size );
1700 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001701 mbedtls_md_update( md_ctx, out, md_size );
1702 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001705
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001706/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001707 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001708#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001709 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1710 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1711 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1712/* This function makes sure every byte in the memory region is accessed
1713 * (in ascending addresses order) */
1714static void ssl_read_memory( unsigned char *p, size_t len )
1715{
1716 unsigned char acc = 0;
1717 volatile unsigned char force;
1718
1719 for( ; len != 0; p++, len-- )
1720 acc ^= *p;
1721
1722 force = acc;
1723 (void) force;
1724}
1725#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1726
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001727/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001728 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001729 */
Hanno Becker3307b532017-12-27 21:37:21 +00001730
Hanno Beckera5a2b082019-05-15 14:03:01 +01001731#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001732/* This functions transforms a DTLS plaintext fragment and a record content
1733 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001734 *
1735 * struct {
1736 * opaque content[DTLSPlaintext.length];
1737 * ContentType real_type;
1738 * uint8 zeros[length_of_padding];
1739 * } DTLSInnerPlaintext;
1740 *
1741 * Input:
1742 * - `content`: The beginning of the buffer holding the
1743 * plaintext to be wrapped.
1744 * - `*content_size`: The length of the plaintext in Bytes.
1745 * - `max_len`: The number of Bytes available starting from
1746 * `content`. This must be `>= *content_size`.
1747 * - `rec_type`: The desired record content type.
1748 *
1749 * Output:
1750 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1751 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1752 *
1753 * Returns:
1754 * - `0` on success.
1755 * - A negative error code if `max_len` didn't offer enough space
1756 * for the expansion.
1757 */
1758static int ssl_cid_build_inner_plaintext( unsigned char *content,
1759 size_t *content_size,
1760 size_t remaining,
1761 uint8_t rec_type )
1762{
1763 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001764 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1765 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1766 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001767
1768 /* Write real content type */
1769 if( remaining == 0 )
1770 return( -1 );
1771 content[ len ] = rec_type;
1772 len++;
1773 remaining--;
1774
1775 if( remaining < pad )
1776 return( -1 );
1777 memset( content + len, 0, pad );
1778 len += pad;
1779 remaining -= pad;
1780
1781 *content_size = len;
1782 return( 0 );
1783}
1784
Hanno Becker7dc25772019-05-20 15:08:01 +01001785/* This function parses a DTLSInnerPlaintext structure.
1786 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001787static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1788 size_t *content_size,
1789 uint8_t *rec_type )
1790{
1791 size_t remaining = *content_size;
1792
1793 /* Determine length of padding by skipping zeroes from the back. */
1794 do
1795 {
1796 if( remaining == 0 )
1797 return( -1 );
1798 remaining--;
1799 } while( content[ remaining ] == 0 );
1800
1801 *content_size = remaining;
1802 *rec_type = content[ remaining ];
1803
1804 return( 0 );
1805}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001806#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001807
Hanno Becker99abf512019-05-20 14:50:53 +01001808/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001809 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001810static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001811 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001812 mbedtls_record *rec )
1813{
Hanno Becker99abf512019-05-20 14:50:53 +01001814 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001815 *
1816 * additional_data = seq_num + TLSCompressed.type +
1817 * TLSCompressed.version + TLSCompressed.length;
1818 *
Hanno Becker99abf512019-05-20 14:50:53 +01001819 * For the CID extension, this is extended as follows
1820 * (quoting draft-ietf-tls-dtls-connection-id-05,
1821 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001822 *
1823 * additional_data = seq_num + DTLSPlaintext.type +
1824 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001825 * cid +
1826 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001827 * length_of_DTLSInnerPlaintext;
1828 */
1829
Hanno Becker3307b532017-12-27 21:37:21 +00001830 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1831 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001832 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001833
Hanno Beckera5a2b082019-05-15 14:03:01 +01001834#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001835 if( rec->cid_len != 0 )
1836 {
1837 memcpy( add_data + 11, rec->cid, rec->cid_len );
1838 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1839 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1840 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1841 *add_data_len = 13 + 1 + rec->cid_len;
1842 }
1843 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001844#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001845 {
1846 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1847 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1848 *add_data_len = 13;
1849 }
Hanno Becker3307b532017-12-27 21:37:21 +00001850}
1851
Hanno Becker611a83b2018-01-03 14:27:32 +00001852int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1853 mbedtls_ssl_transform *transform,
1854 mbedtls_record *rec,
1855 int (*f_rng)(void *, unsigned char *, size_t),
1856 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001857{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001859 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001860 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001861 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001862 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001863 size_t post_avail;
1864
1865 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001866#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001867 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001868 ((void) ssl);
1869#endif
1870
1871 /* The PRNG is used for dynamic IV generation that's used
1872 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1873#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1874 ( defined(MBEDTLS_AES_C) || \
1875 defined(MBEDTLS_ARIA_C) || \
1876 defined(MBEDTLS_CAMELLIA_C) ) && \
1877 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1878 ((void) f_rng);
1879 ((void) p_rng);
1880#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
Hanno Becker3307b532017-12-27 21:37:21 +00001884 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001885 {
Hanno Becker3307b532017-12-27 21:37:21 +00001886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1888 }
Hanno Becker505089d2019-05-01 09:45:57 +01001889 if( rec == NULL
1890 || rec->buf == NULL
1891 || rec->buf_len < rec->data_offset
1892 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001893#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001894 || rec->cid_len != 0
1895#endif
1896 )
Hanno Becker3307b532017-12-27 21:37:21 +00001897 {
1898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001900 }
1901
Hanno Becker3307b532017-12-27 21:37:21 +00001902 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001903 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001905 data, rec->data_len );
1906
1907 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1908
1909 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1910 {
1911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1912 (unsigned) rec->data_len,
1913 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1915 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001916
Hanno Beckera5a2b082019-05-15 14:03:01 +01001917#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001918 /*
1919 * Add CID information
1920 */
1921 rec->cid_len = transform->out_cid_len;
1922 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1923 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001924
1925 if( rec->cid_len != 0 )
1926 {
1927 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001928 * Wrap plaintext into DTLSInnerPlaintext structure.
1929 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001930 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001931 * Note that this changes `rec->data_len`, and hence
1932 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001933 */
1934 if( ssl_cid_build_inner_plaintext( data,
1935 &rec->data_len,
1936 post_avail,
1937 rec->type ) != 0 )
1938 {
1939 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1940 }
1941
1942 rec->type = MBEDTLS_SSL_MSG_CID;
1943 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001944#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001945
Hanno Becker92c930f2019-04-29 17:31:37 +01001946 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1947
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001949 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001951#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 if( mode == MBEDTLS_MODE_STREAM ||
1953 ( mode == MBEDTLS_MODE_CBC
1954#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001955 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001956#endif
1957 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 {
Hanno Becker3307b532017-12-27 21:37:21 +00001959 if( post_avail < transform->maclen )
1960 {
1961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1962 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1963 }
1964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001966 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001967 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001968 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001969 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1970 data, rec->data_len, rec->ctr, rec->type, mac );
1971 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001972 }
1973 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1976 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001977 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001978 {
Hanno Becker992b6872017-11-09 18:57:39 +00001979 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1980
Hanno Beckere83efe62019-04-29 13:52:53 +01001981 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001982
Hanno Becker3307b532017-12-27 21:37:21 +00001983 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001984 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001985 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1986 data, rec->data_len );
1987 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1988 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1989
1990 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001991 }
1992 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001993#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1996 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001997 }
1998
Hanno Becker3307b532017-12-27 21:37:21 +00001999 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2000 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002001
Hanno Becker3307b532017-12-27 21:37:21 +00002002 rec->data_len += transform->maclen;
2003 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002004 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002005 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002006#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002008 /*
2009 * Encrypt
2010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2012 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002014 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002015 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002017 "including %d bytes of padding",
2018 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
Hanno Becker3307b532017-12-27 21:37:21 +00002020 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2021 transform->iv_enc, transform->ivlen,
2022 data, rec->data_len,
2023 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002026 return( ret );
2027 }
2028
Hanno Becker3307b532017-12-27 21:37:21 +00002029 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2032 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 }
Paul Bakker68884e32013-01-07 18:20:04 +01002035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002037
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002038#if defined(MBEDTLS_GCM_C) || \
2039 defined(MBEDTLS_CCM_C) || \
2040 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002042 mode == MBEDTLS_MODE_CCM ||
2043 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002044 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002045 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002046 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002047 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002048
Hanno Becker3307b532017-12-27 21:37:21 +00002049 /* Check that there's space for both the authentication tag
2050 * and the explicit IV before and after the record content. */
2051 if( post_avail < transform->taglen ||
2052 rec->data_offset < explicit_iv_len )
2053 {
2054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2055 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2056 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002057
Paul Bakker68884e32013-01-07 18:20:04 +01002058 /*
2059 * Generate IV
2060 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002061 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2062 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002063 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002064 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002065 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2066 explicit_iv_len );
2067 /* Prefix record content with explicit IV. */
2068 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002069 }
2070 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2071 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002072 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002073 unsigned char i;
2074
2075 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2076
2077 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002078 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002079 }
2080 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002081 {
2082 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002085 }
2086
Hanno Beckere83efe62019-04-29 13:52:53 +01002087 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002088
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002089 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2090 iv, transform->ivlen );
2091 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002092 data - explicit_iv_len, explicit_iv_len );
2093 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002094 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002096 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002097 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002098
Paul Bakker68884e32013-01-07 18:20:04 +01002099 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002100 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002101 */
Hanno Becker3307b532017-12-27 21:37:21 +00002102
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002103 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002104 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002105 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002106 data, rec->data_len, /* source */
2107 data, &rec->data_len, /* destination */
2108 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002111 return( ret );
2112 }
2113
Hanno Becker3307b532017-12-27 21:37:21 +00002114 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2115 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002116
Hanno Becker3307b532017-12-27 21:37:21 +00002117 rec->data_len += transform->taglen + explicit_iv_len;
2118 rec->data_offset -= explicit_iv_len;
2119 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002120 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2124#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002125 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002128 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002129 size_t padlen, i;
2130 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131
Hanno Becker3307b532017-12-27 21:37:21 +00002132 /* Currently we're always using minimal padding
2133 * (up to 255 bytes would be allowed). */
2134 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2135 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 padlen = 0;
2137
Hanno Becker3307b532017-12-27 21:37:21 +00002138 /* Check there's enough space in the buffer for the padding. */
2139 if( post_avail < padlen + 1 )
2140 {
2141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2142 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2143 }
2144
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002146 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
Hanno Becker3307b532017-12-27 21:37:21 +00002148 rec->data_len += padlen + 1;
2149 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002152 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002153 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2154 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155 */
Hanno Becker3307b532017-12-27 21:37:21 +00002156 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002157 {
Hanno Becker3307b532017-12-27 21:37:21 +00002158 if( f_rng == NULL )
2159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2162 }
2163
2164 if( rec->data_offset < transform->ivlen )
2165 {
2166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2167 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2168 }
2169
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002170 /*
2171 * Generate IV
2172 */
Hanno Becker3307b532017-12-27 21:37:21 +00002173 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002174 if( ret != 0 )
2175 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002176
Hanno Becker3307b532017-12-27 21:37:21 +00002177 memcpy( data - transform->ivlen, transform->iv_enc,
2178 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002180 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002184 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002185 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002186 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
Hanno Becker3307b532017-12-27 21:37:21 +00002188 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2189 transform->iv_enc,
2190 transform->ivlen,
2191 data, rec->data_len,
2192 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002195 return( ret );
2196 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002197
Hanno Becker3307b532017-12-27 21:37:21 +00002198 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002202 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002205 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002206 {
2207 /*
2208 * Save IV in SSL3 and TLS1
2209 */
Hanno Becker3307b532017-12-27 21:37:21 +00002210 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2211 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 }
Hanno Becker3307b532017-12-27 21:37:21 +00002213 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002214#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002215 {
2216 data -= transform->ivlen;
2217 rec->data_offset -= transform->ivlen;
2218 rec->data_len += transform->ivlen;
2219 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002222 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002224 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2225
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002226 /*
2227 * MAC(MAC_write_key, seq_num +
2228 * TLSCipherText.type +
2229 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002230 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002231 * IV + // except for TLS 1.0
2232 * ENC(content + padding + padding_length));
2233 */
Hanno Becker3307b532017-12-27 21:37:21 +00002234
2235 if( post_avail < transform->maclen)
2236 {
2237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2238 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2239 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002240
Hanno Beckere83efe62019-04-29 13:52:53 +01002241 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002244 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002245 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002246
Hanno Becker3307b532017-12-27 21:37:21 +00002247 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002248 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002249 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2250 data, rec->data_len );
2251 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2252 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002253
Hanno Becker3307b532017-12-27 21:37:21 +00002254 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002255
Hanno Becker3307b532017-12-27 21:37:21 +00002256 rec->data_len += transform->maclen;
2257 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002258 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002259 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002262 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002264 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002270 /* Make extra sure authentication was performed, exactly once */
2271 if( auth_done != 1 )
2272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2274 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002275 }
2276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
2279 return( 0 );
2280}
2281
Hanno Becker611a83b2018-01-03 14:27:32 +00002282int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2283 mbedtls_ssl_transform *transform,
2284 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002285{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002286 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002288 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002289#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002290 size_t padlen = 0, correct = 1;
2291#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002292 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002293 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002294 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002295
Hanno Becker611a83b2018-01-03 14:27:32 +00002296#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002297 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002298 ((void) ssl);
2299#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002302 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002303 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2305 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2306 }
2307 if( rec == NULL ||
2308 rec->buf == NULL ||
2309 rec->buf_len < rec->data_offset ||
2310 rec->buf_len - rec->data_offset < rec->data_len )
2311 {
2312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002314 }
2315
Hanno Becker4c6876b2017-12-27 21:28:58 +00002316 data = rec->buf + rec->data_offset;
2317 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
Hanno Beckera5a2b082019-05-15 14:03:01 +01002319#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002320 /*
2321 * Match record's CID with incoming CID.
2322 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002323 if( rec->cid_len != transform->in_cid_len ||
2324 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2325 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002326 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002327 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002328#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2331 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002332 {
2333 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002334 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2335 transform->iv_dec,
2336 transform->ivlen,
2337 data, rec->data_len,
2338 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002341 return( ret );
2342 }
2343
Hanno Becker4c6876b2017-12-27 21:28:58 +00002344 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2347 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 }
Paul Bakker68884e32013-01-07 18:20:04 +01002350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002352#if defined(MBEDTLS_GCM_C) || \
2353 defined(MBEDTLS_CCM_C) || \
2354 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 mode == MBEDTLS_MODE_CCM ||
2357 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002358 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002359 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002360 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002361
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002362 /*
2363 * Compute and update sizes
2364 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002365 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002368 "+ taglen (%d)", rec->data_len,
2369 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002371 }
Paul Bakker68884e32013-01-07 18:20:04 +01002372
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002373 /*
2374 * Prepare IV
2375 */
2376 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2377 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002378 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002379 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002380 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002381
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002382 }
2383 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2384 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002385 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002386 unsigned char i;
2387
2388 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2389
2390 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002391 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002392 }
2393 else
2394 {
2395 /* Reminder if we ever add an AEAD mode with a different size */
2396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2398 }
2399
Hanno Becker4c6876b2017-12-27 21:28:58 +00002400 data += explicit_iv_len;
2401 rec->data_offset += explicit_iv_len;
2402 rec->data_len -= explicit_iv_len + transform->taglen;
2403
Hanno Beckere83efe62019-04-29 13:52:53 +01002404 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002405 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002406 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002407
2408 memcpy( transform->iv_dec + transform->fixed_ivlen,
2409 data - explicit_iv_len, explicit_iv_len );
2410
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002411 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002412 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002413 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002414
Paul Bakker68884e32013-01-07 18:20:04 +01002415
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002416 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002417 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002418 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002419 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2420 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002421 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002422 data, rec->data_len,
2423 data, &olen,
2424 data + rec->data_len,
2425 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2430 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002431
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002432 return( ret );
2433 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002434 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002435
Hanno Becker4c6876b2017-12-27 21:28:58 +00002436 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2439 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002440 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2444#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002445 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002448 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002449
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 /*
Paul Bakker45829992013-01-03 14:52:21 +01002451 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002454 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2455 {
2456 /* The ciphertext is prefixed with the CBC IV. */
2457 minlen += transform->ivlen;
2458 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002459#endif
Paul Bakker45829992013-01-03 14:52:21 +01002460
Hanno Becker4c6876b2017-12-27 21:28:58 +00002461 /* Size considerations:
2462 *
2463 * - The CBC cipher text must not be empty and hence
2464 * at least of size transform->ivlen.
2465 *
2466 * Together with the potential IV-prefix, this explains
2467 * the first of the two checks below.
2468 *
2469 * - The record must contain a MAC, either in plain or
2470 * encrypted, depending on whether Encrypt-then-MAC
2471 * is used or not.
2472 * - If it is, the message contains the IV-prefix,
2473 * the CBC ciphertext, and the MAC.
2474 * - If it is not, the padded plaintext, and hence
2475 * the CBC ciphertext, has at least length maclen + 1
2476 * because there is at least the padding length byte.
2477 *
2478 * As the CBC ciphertext is not empty, both cases give the
2479 * lower bound minlen + maclen + 1 on the record size, which
2480 * we test for in the second check below.
2481 */
2482 if( rec->data_len < minlen + transform->ivlen ||
2483 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002486 "+ 1 ) ( + expl IV )", rec->data_len,
2487 transform->ivlen,
2488 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002490 }
2491
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002492 /*
2493 * Authenticate before decrypt if enabled
2494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002496 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497 {
Hanno Becker992b6872017-11-09 18:57:39 +00002498 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002501
Hanno Becker4c6876b2017-12-27 21:28:58 +00002502 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2503 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504
Hanno Beckere83efe62019-04-29 13:52:53 +01002505 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002506
Hanno Beckere83efe62019-04-29 13:52:53 +01002507 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2508 add_data_len );
2509 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2510 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002511 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2512 data, rec->data_len );
2513 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2514 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002515
Hanno Becker4c6876b2017-12-27 21:28:58 +00002516 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2517 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002518 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002519 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520
Hanno Becker4c6876b2017-12-27 21:28:58 +00002521 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2522 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002526 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002527 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002530
2531 /*
2532 * Check length sanity
2533 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002534 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002537 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002539 }
2540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002542 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002543 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002544 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002545 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002546 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002547 /* This is safe because data_len >= minlen + maclen + 1 initially,
2548 * and at this point we have at most subtracted maclen (note that
2549 * minlen == transform->ivlen here). */
2550 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002551
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 data += transform->ivlen;
2553 rec->data_offset += transform->ivlen;
2554 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002555 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002557
Hanno Becker4c6876b2017-12-27 21:28:58 +00002558 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2559 transform->iv_dec, transform->ivlen,
2560 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002563 return( ret );
2564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002565
Hanno Becker4c6876b2017-12-27 21:28:58 +00002566 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2569 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002570 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002573 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002574 {
2575 /*
2576 * Save IV in SSL3 and TLS1
2577 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002578 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2579 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002581#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583 /* Safe since data_len >= minlen + maclen + 1, so after having
2584 * subtracted at most minlen and maclen up to this point,
2585 * data_len > 0. */
2586 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002587
Hanno Becker4c6876b2017-12-27 21:28:58 +00002588 if( auth_done == 1 )
2589 {
2590 correct *= ( rec->data_len >= padlen + 1 );
2591 padlen *= ( rec->data_len >= padlen + 1 );
2592 }
2593 else
Paul Bakker45829992013-01-03 14:52:21 +01002594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002596 if( rec->data_len < transform->maclen + padlen + 1 )
2597 {
2598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2599 rec->data_len,
2600 transform->maclen,
2601 padlen + 1 ) );
2602 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002603#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002604
2605 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2606 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Hanno Becker4c6876b2017-12-27 21:28:58 +00002609 padlen++;
2610
2611 /* Regardless of the validity of the padding,
2612 * we have data_len >= padlen here. */
2613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002615 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002617 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#if defined(MBEDTLS_SSL_DEBUG_ALL)
2620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002621 "should be no more than %d",
2622 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002623#endif
Paul Bakker45829992013-01-03 14:52:21 +01002624 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626 }
2627 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2629#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2630 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002631 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002633 /* The padding check involves a series of up to 256
2634 * consecutive memory reads at the end of the record
2635 * plaintext buffer. In order to hide the length and
2636 * validity of the padding, always perform exactly
2637 * `min(256,plaintext_len)` reads (but take into account
2638 * only the last `padlen` bytes for the padding check). */
2639 size_t pad_count = 0;
2640 size_t real_count = 0;
2641 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002642
Hanno Becker4c6876b2017-12-27 21:28:58 +00002643 /* Index of first padding byte; it has been ensured above
2644 * that the subtraction is safe. */
2645 size_t const padding_idx = rec->data_len - padlen;
2646 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2647 size_t const start_idx = rec->data_len - num_checks;
2648 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002649
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002651 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002652 real_count |= ( idx >= padding_idx );
2653 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002654 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002655 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002658 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002660#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002661 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002663 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2665 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2668 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002669 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670
Hanno Becker4c6876b2017-12-27 21:28:58 +00002671 /* If the padding was found to be invalid, padlen == 0
2672 * and the subtraction is safe. If the padding was found valid,
2673 * padlen hasn't been changed and the previous assertion
2674 * data_len >= padlen still holds. */
2675 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002677 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002679 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2682 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002685#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002687 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002688#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
2690 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002691 * Authenticate if not done yet.
2692 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002694#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002695 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002696 {
Hanno Becker992b6872017-11-09 18:57:39 +00002697 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002698
Hanno Becker4c6876b2017-12-27 21:28:58 +00002699 /* If the initial value of padlen was such that
2700 * data_len < maclen + padlen + 1, then padlen
2701 * got reset to 1, and the initial check
2702 * data_len >= minlen + maclen + 1
2703 * guarantees that at this point we still
2704 * have at least data_len >= maclen.
2705 *
2706 * If the initial value of padlen was such that
2707 * data_len >= maclen + padlen + 1, then we have
2708 * subtracted either padlen + 1 (if the padding was correct)
2709 * or 0 (if the padding was incorrect) since then,
2710 * hence data_len >= maclen in any case.
2711 */
2712 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Hanno Beckere83efe62019-04-29 13:52:53 +01002714 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002717 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002718 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002719 ssl_mac( &transform->md_ctx_dec,
2720 transform->mac_dec,
2721 data, rec->data_len,
2722 rec->ctr, rec->type,
2723 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002724 }
2725 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2727#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2728 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002729 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002730 {
2731 /*
2732 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002733 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002734 *
2735 * Known timing attacks:
2736 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2737 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002738 * To compensate for different timings for the MAC calculation
2739 * depending on how much padding was removed (which is determined
2740 * by padlen), process extra_run more blocks through the hash
2741 * function.
2742 *
2743 * The formula in the paper is
2744 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2745 * where L1 is the size of the header plus the decrypted message
2746 * plus CBC padding and L2 is the size of the header plus the
2747 * decrypted message. This is for an underlying hash function
2748 * with 64-byte blocks.
2749 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2750 * correctly. We round down instead of up, so -56 is the correct
2751 * value for our calculations instead of -55.
2752 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002753 * Repeat the formula rather than defining a block_size variable.
2754 * This avoids requiring division by a variable at runtime
2755 * (which would be marginally less efficient and would require
2756 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002757 */
2758 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002759 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002760
2761 /*
2762 * The next two sizes are the minimum and maximum values of
2763 * in_msglen over all padlen values.
2764 *
2765 * They're independent of padlen, since we previously did
2766 * in_msglen -= padlen.
2767 *
2768 * Note that max_len + maclen is never more than the buffer
2769 * length, as we previously did in_msglen -= maclen too.
2770 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002771 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002772 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2773
Hanno Becker4c6876b2017-12-27 21:28:58 +00002774 memset( tmp, 0, sizeof( tmp ) );
2775
2776 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002778#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2779 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002780 case MBEDTLS_MD_MD5:
2781 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002782 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002783 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002784 extra_run =
2785 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2786 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002787 break;
2788#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002789#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002790 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002791 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002792 extra_run =
2793 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2794 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002795 break;
2796#endif
2797 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2800 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002801
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002802 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002803
Hanno Beckere83efe62019-04-29 13:52:53 +01002804 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2805 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002806 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2807 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002808 /* Make sure we access everything even when padlen > 0. This
2809 * makes the synchronisation requirements for just-in-time
2810 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002811 ssl_read_memory( data + rec->data_len, padlen );
2812 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002813
2814 /* Call mbedtls_md_process at least once due to cache attacks
2815 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002816 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002817 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002818
Hanno Becker4c6876b2017-12-27 21:28:58 +00002819 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002820
2821 /* Make sure we access all the memory that could contain the MAC,
2822 * before we check it in the next code block. This makes the
2823 * synchronisation requirements for just-in-time Prime+Probe
2824 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002825 ssl_read_memory( data + min_len,
2826 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002827 }
2828 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2830 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2833 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002834 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002836#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002837 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2838 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002839#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Hanno Becker4c6876b2017-12-27 21:28:58 +00002841 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2842 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#if defined(MBEDTLS_SSL_DEBUG_ALL)
2845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002846#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002847 correct = 0;
2848 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002849 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002850 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002851
2852 /*
2853 * Finally check the correct flag
2854 */
2855 if( correct == 0 )
2856 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002857#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002858
2859 /* Make extra sure authentication was performed, exactly once */
2860 if( auth_done != 1 )
2861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Hanno Beckera5a2b082019-05-15 14:03:01 +01002866#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002867 if( rec->cid_len != 0 )
2868 {
2869 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2870 &rec->type );
2871 if( ret != 0 )
2872 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2873 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002874#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
2878 return( 0 );
2879}
2880
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002881#undef MAC_NONE
2882#undef MAC_PLAINTEXT
2883#undef MAC_CIPHERTEXT
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002886/*
2887 * Compression/decompression functions
2888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890{
2891 int ret;
2892 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002893 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002894 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002895 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002898
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002899 if( len_pre == 0 )
2900 return( 0 );
2901
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902 memcpy( msg_pre, ssl->out_msg, len_pre );
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905 ssl->out_msglen ) );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908 ssl->out_msg, ssl->out_msglen );
2909
Paul Bakker48916f92012-09-16 19:57:18 +00002910 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2911 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2912 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002913 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914
Paul Bakker48916f92012-09-16 19:57:18 +00002915 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002916 if( ret != Z_OK )
2917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2919 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 }
2921
Angus Grattond8213d02016-05-25 20:56:48 +10002922 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002923 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926 ssl->out_msglen ) );
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002929 ssl->out_msg, ssl->out_msglen );
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002932
2933 return( 0 );
2934}
2935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002937{
2938 int ret;
2939 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002940 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002941 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002942 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002945
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002946 if( len_pre == 0 )
2947 return( 0 );
2948
Paul Bakker2770fbd2012-07-03 13:30:23 +00002949 memcpy( msg_pre, ssl->in_msg, len_pre );
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002952 ssl->in_msglen ) );
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002955 ssl->in_msg, ssl->in_msglen );
2956
Paul Bakker48916f92012-09-16 19:57:18 +00002957 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2958 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2959 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002960 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002961 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002962
Paul Bakker48916f92012-09-16 19:57:18 +00002963 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002964 if( ret != Z_OK )
2965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2967 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002968 }
2969
Angus Grattond8213d02016-05-25 20:56:48 +10002970 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002971 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002974 ssl->in_msglen ) );
2975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002977 ssl->in_msg, ssl->in_msglen );
2978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002980
2981 return( 0 );
2982}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2986static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#if defined(MBEDTLS_SSL_PROTO_DTLS)
2989static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002990{
2991 /* If renegotiation is not enforced, retransmit until we would reach max
2992 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002993 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002994 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002995 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002996 unsigned char doublings = 1;
2997
2998 while( ratio != 0 )
2999 {
3000 ++doublings;
3001 ratio >>= 1;
3002 }
3003
3004 if( ++ssl->renego_records_seen > doublings )
3005 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003007 return( 0 );
3008 }
3009 }
3010
3011 return( ssl_write_hello_request( ssl ) );
3012}
3013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003015
Paul Bakker5121ce52009-01-03 21:22:43 +00003016/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003017 * Fill the input message buffer by appending data to it.
3018 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003019 *
3020 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3021 * available (from this read and/or a previous one). Otherwise, an error code
3022 * is returned (possibly EOF or WANT_READ).
3023 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003024 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3025 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3026 * since we always read a whole datagram at once.
3027 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003028 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003029 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003032{
Paul Bakker23986e52011-04-24 08:57:21 +00003033 int ret;
3034 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003038 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003041 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003043 }
3044
Angus Grattond8213d02016-05-25 20:56:48 +10003045 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003049 }
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003052 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003054 uint32_t timeout;
3055
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003056 /* Just to be sure */
3057 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3058 {
3059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3060 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3062 }
3063
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003064 /*
3065 * The point is, we need to always read a full datagram at once, so we
3066 * sometimes read more then requested, and handle the additional data.
3067 * It could be the rest of the current record (while fetching the
3068 * header) and/or some other records in the same datagram.
3069 */
3070
3071 /*
3072 * Move to the next record in the already read datagram if applicable
3073 */
3074 if( ssl->next_record_offset != 0 )
3075 {
3076 if( ssl->in_left < ssl->next_record_offset )
3077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003080 }
3081
3082 ssl->in_left -= ssl->next_record_offset;
3083
3084 if( ssl->in_left != 0 )
3085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003087 ssl->next_record_offset ) );
3088 memmove( ssl->in_hdr,
3089 ssl->in_hdr + ssl->next_record_offset,
3090 ssl->in_left );
3091 }
3092
3093 ssl->next_record_offset = 0;
3094 }
3095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003098
3099 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003100 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003101 */
3102 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003105 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003106 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003107
3108 /*
3109 * A record can't be split accross datagrams. If we need to read but
3110 * are not at the beginning of a new record, the caller did something
3111 * wrong.
3112 */
3113 if( ssl->in_left != 0 )
3114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3116 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003117 }
3118
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003119 /*
3120 * Don't even try to read if time's out already.
3121 * This avoids by-passing the timer when repeatedly receiving messages
3122 * that will end up being dropped.
3123 */
3124 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003125 {
3126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003127 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003128 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003129 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003130 {
Angus Grattond8213d02016-05-25 20:56:48 +10003131 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003134 timeout = ssl->handshake->retransmit_timeout;
3135 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003136 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003139
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003140 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003141 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3142 timeout );
3143 else
3144 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003147
3148 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003150 }
3151
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003152 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003155 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003158 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003159 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003162 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003163 }
3164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003168 return( ret );
3169 }
3170
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003171 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003174 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003176 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003177 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003180 return( ret );
3181 }
3182
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003183 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003184 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003186 }
3187
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 if( ret < 0 )
3189 return( ret );
3190
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003191 ssl->in_left = ret;
3192 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003193 MBEDTLS_SSL_TRANSPORT_ELSE
3194#endif /* MBEDTLS_SSL_PROTO_DTLS */
3195#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003198 ssl->in_left, nb_want ) );
3199
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003200 while( ssl->in_left < nb_want )
3201 {
3202 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003203
3204 if( ssl_check_timer( ssl ) != 0 )
3205 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3206 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003207 {
3208 if( ssl->f_recv_timeout != NULL )
3209 {
3210 ret = ssl->f_recv_timeout( ssl->p_bio,
3211 ssl->in_hdr + ssl->in_left, len,
3212 ssl->conf->read_timeout );
3213 }
3214 else
3215 {
3216 ret = ssl->f_recv( ssl->p_bio,
3217 ssl->in_hdr + ssl->in_left, len );
3218 }
3219 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003222 ssl->in_left, nb_want ) );
3223 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003224
3225 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003227
3228 if( ret < 0 )
3229 return( ret );
3230
mohammad160352aecb92018-03-28 23:41:40 -07003231 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003232 {
Darryl Green11999bb2018-03-13 15:22:58 +00003233 MBEDTLS_SSL_DEBUG_MSG( 1,
3234 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003235 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3237 }
3238
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003239 ssl->in_left += ret;
3240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003241 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003242#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003245
3246 return( 0 );
3247}
3248
3249/*
3250 * Flush any data not yet written
3251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003253{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003254 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003255 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003259 if( ssl->f_send == NULL )
3260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003262 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003264 }
3265
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003266 /* Avoid incrementing counter if data is flushed */
3267 if( ssl->out_left == 0 )
3268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003270 return( 0 );
3271 }
3272
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 while( ssl->out_left > 0 )
3274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003276 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277
Hanno Becker2b1e3542018-08-06 11:19:13 +01003278 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003279 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
3283 if( ret <= 0 )
3284 return( ret );
3285
mohammad160352aecb92018-03-28 23:41:40 -07003286 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003287 {
Darryl Green11999bb2018-03-13 15:22:58 +00003288 MBEDTLS_SSL_DEBUG_MSG( 1,
3289 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003290 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003291 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3292 }
3293
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 ssl->out_left -= ret;
3295 }
3296
Hanno Becker2b1e3542018-08-06 11:19:13 +01003297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003298 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003299 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003300 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003301 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003302 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003303#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003304#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003305 {
3306 ssl->out_hdr = ssl->out_buf + 8;
3307 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003308#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003309 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
3313 return( 0 );
3314}
3315
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003316/*
3317 * Functions to handle the DTLS retransmission state machine
3318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003320/*
3321 * Append current handshake message to current outgoing flight
3322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3327 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3328 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003329
3330 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003331 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003332 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003335 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003336 }
3337
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003338 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003342 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343 }
3344
3345 /* Copy current handshake message with headers */
3346 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3347 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003348 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349 msg->next = NULL;
3350
3351 /* Append to the current flight */
3352 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003353 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003354 else
3355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357 while( cur->next != NULL )
3358 cur = cur->next;
3359 cur->next = msg;
3360 }
3361
Hanno Becker3b235902018-08-06 09:54:53 +01003362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003363 return( 0 );
3364}
3365
3366/*
3367 * Free the current flight of handshake messages
3368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 mbedtls_ssl_flight_item *cur = flight;
3372 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003373
3374 while( cur != NULL )
3375 {
3376 next = cur->next;
3377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 mbedtls_free( cur->p );
3379 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003380
3381 cur = next;
3382 }
3383}
3384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3386static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003387#endif
3388
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003389/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003390 * Swap transform_out and out_ctr with the alternative ones
3391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003395 unsigned char tmp_out_ctr[8];
3396
3397 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003400 return;
3401 }
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003404
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003405 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003406 tmp_transform = ssl->transform_out;
3407 ssl->transform_out = ssl->handshake->alt_transform_out;
3408 ssl->handshake->alt_transform_out = tmp_transform;
3409
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003410 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003411 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3412 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003413 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003414
3415 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003416 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3419 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3424 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003425 }
3426 }
3427#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003428}
3429
3430/*
3431 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003432 */
3433int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3434{
3435 int ret = 0;
3436
3437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3438
3439 ret = mbedtls_ssl_flight_transmit( ssl );
3440
3441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3442
3443 return( ret );
3444}
3445
3446/*
3447 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003448 *
3449 * Need to remember the current message in case flush_output returns
3450 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003451 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003452 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003453int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003454{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003455 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461
3462 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003463 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003464 ssl_swap_epochs( ssl );
3465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003467 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003468
3469 while( ssl->handshake->cur_msg != NULL )
3470 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003471 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003472 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003473
Hanno Beckere1dcb032018-08-17 16:47:58 +01003474 int const is_finished =
3475 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3476 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3477
Hanno Becker04da1892018-08-14 13:22:10 +01003478 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3479 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3480
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003481 /* Swap epochs before sending Finished: we can't do it after
3482 * sending ChangeCipherSpec, in case write returns WANT_READ.
3483 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003484 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003485 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003487 ssl_swap_epochs( ssl );
3488 }
3489
Hanno Becker67bc7c32018-08-06 11:33:50 +01003490 ret = ssl_get_remaining_payload_in_datagram( ssl );
3491 if( ret < 0 )
3492 return( ret );
3493 max_frag_len = (size_t) ret;
3494
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003495 /* CCS is copied as is, while HS messages may need fragmentation */
3496 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3497 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003498 if( max_frag_len == 0 )
3499 {
3500 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3501 return( ret );
3502
3503 continue;
3504 }
3505
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003506 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003507 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003508 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003509
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003510 /* Update position inside current message */
3511 ssl->handshake->cur_msg_p += cur->len;
3512 }
3513 else
3514 {
3515 const unsigned char * const p = ssl->handshake->cur_msg_p;
3516 const size_t hs_len = cur->len - 12;
3517 const size_t frag_off = p - ( cur->p + 12 );
3518 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003519 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003520
Hanno Beckere1dcb032018-08-17 16:47:58 +01003521 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003522 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003523 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003524 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003525
Hanno Becker67bc7c32018-08-06 11:33:50 +01003526 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3527 return( ret );
3528
3529 continue;
3530 }
3531 max_hs_frag_len = max_frag_len - 12;
3532
3533 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3534 max_hs_frag_len : rem_len;
3535
3536 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003537 {
3538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003539 (unsigned) cur_hs_frag_len,
3540 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003541 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003542
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003543 /* Messages are stored with handshake headers as if not fragmented,
3544 * copy beginning of headers then fill fragmentation fields.
3545 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3546 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003547
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003548 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3549 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3550 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3551
Hanno Becker67bc7c32018-08-06 11:33:50 +01003552 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3553 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3554 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003555
3556 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3557
Hanno Becker3f7b9732018-08-28 09:53:25 +01003558 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003559 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3560 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003561 ssl->out_msgtype = cur->type;
3562
3563 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003564 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003565 }
3566
3567 /* If done with the current message move to the next one if any */
3568 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3569 {
3570 if( cur->next != NULL )
3571 {
3572 ssl->handshake->cur_msg = cur->next;
3573 ssl->handshake->cur_msg_p = cur->next->p + 12;
3574 }
3575 else
3576 {
3577 ssl->handshake->cur_msg = NULL;
3578 ssl->handshake->cur_msg_p = NULL;
3579 }
3580 }
3581
3582 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003583 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003586 return( ret );
3587 }
3588 }
3589
Hanno Becker67bc7c32018-08-06 11:33:50 +01003590 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3591 return( ret );
3592
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003593 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3595 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003596 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003599 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3600 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003601
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003603
3604 return( 0 );
3605}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003606
3607/*
3608 * To be called when the last message of an incoming flight is received.
3609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003611{
3612 /* We won't need to resend that one any more */
3613 ssl_flight_free( ssl->handshake->flight );
3614 ssl->handshake->flight = NULL;
3615 ssl->handshake->cur_msg = NULL;
3616
3617 /* The next incoming flight will start with this msg_seq */
3618 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3619
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003620 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003621 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003622
Hanno Becker0271f962018-08-16 13:23:47 +01003623 /* Clear future message buffering structure. */
3624 ssl_buffering_free( ssl );
3625
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003626 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003627 ssl_set_timer( ssl, 0 );
3628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3630 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003633 }
3634 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003636}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003637
3638/*
3639 * To be called when the last message of an outgoing flight is send.
3640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003642{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003643 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003644 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3647 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003650 }
3651 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003655
Paul Bakker5121ce52009-01-03 21:22:43 +00003656/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003659
3660/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003661 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003662 *
3663 * - fill in handshake headers
3664 * - update handshake checksum
3665 * - DTLS: save message for resending
3666 * - then pass to the record layer
3667 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003668 * DTLS: except for HelloRequest, messages are only queued, and will only be
3669 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003670 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003671 * Inputs:
3672 * - ssl->out_msglen: 4 + actual handshake message len
3673 * (4 is the size of handshake headers for TLS)
3674 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3675 * - ssl->out_msg + 4: the handshake message body
3676 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003677 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003678 * - ssl->out_msglen: the length of the record contents
3679 * (including handshake headers but excluding record headers)
3680 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003681 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003683{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003684 int ret;
3685 const size_t hs_len = ssl->out_msglen - 4;
3686 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3689
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003690 /*
3691 * Sanity checks
3692 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003693 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003694 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3695 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003696 /* In SSLv3, the client might send a NoCertificate alert. */
3697#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3698 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3699 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3700 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3701#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3702 {
3703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3705 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003706 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003707
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003708 /* Whenever we send anything different from a
3709 * HelloRequest we should be in a handshake - double check. */
3710 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3711 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003712 ssl->handshake == NULL )
3713 {
3714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3716 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003719 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003722 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003725 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003726#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003727
Hanno Beckerb50a2532018-08-06 11:52:54 +01003728 /* Double-check that we did not exceed the bounds
3729 * of the outgoing record buffer.
3730 * This should never fail as the various message
3731 * writing functions must obey the bounds of the
3732 * outgoing record buffer, but better be safe.
3733 *
3734 * Note: We deliberately do not check for the MTU or MFL here.
3735 */
3736 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3737 {
3738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3739 "size %u, maximum %u",
3740 (unsigned) ssl->out_msglen,
3741 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3742 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3743 }
3744
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003745 /*
3746 * Fill handshake headers
3747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003749 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003750 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3751 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3752 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003753
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003754 /*
3755 * DTLS has additional fields in the Handshake layer,
3756 * between the length field and the actual payload:
3757 * uint16 message_seq;
3758 * uint24 fragment_offset;
3759 * uint24 fragment_length;
3760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003762 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003763 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003764 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003765 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003766 {
3767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3768 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003770 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3772 }
3773
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003774 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003775 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003776
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003777 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003778 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003779 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003780 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3781 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3782 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003783 }
3784 else
3785 {
3786 ssl->out_msg[4] = 0;
3787 ssl->out_msg[5] = 0;
3788 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003789
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003790 /* Handshake hashes are computed without fragmentation,
3791 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003792 memset( ssl->out_msg + 6, 0x00, 3 );
3793 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003796
Hanno Becker0207e532018-08-28 10:28:28 +01003797 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003798 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3799 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 }
3801
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003802 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003804 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003805 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3806 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003807 {
3808 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003811 return( ret );
3812 }
3813 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003814 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003815#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003816 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003817 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003818 {
3819 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3820 return( ret );
3821 }
3822 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003823
3824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3825
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003826 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003827}
3828
3829/*
3830 * Record layer functions
3831 */
3832
3833/*
3834 * Write current record.
3835 *
3836 * Uses:
3837 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3838 * - ssl->out_msglen: length of the record content (excl headers)
3839 * - ssl->out_msg: record content
3840 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003841int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003842{
3843 int ret, done = 0;
3844 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003845 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003846
3847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003850 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003852 {
3853 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003856 return( ret );
3857 }
3858
3859 len = ssl->out_msglen;
3860 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3864 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 ret = mbedtls_ssl_hw_record_write( ssl );
3869 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3872 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003873 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003874
3875 if( ret == 0 )
3876 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003877 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003879 if( !done )
3880 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003881 unsigned i;
3882 size_t protected_record_size;
3883
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003884 /* Skip writing the record content type to after the encryption,
3885 * as it may change when using the CID extension. */
3886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003888 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003889
Hanno Becker19859472018-08-06 09:40:20 +01003890 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003891 ssl->out_len[0] = (unsigned char)( len >> 8 );
3892 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003893
Paul Bakker48916f92012-09-16 19:57:18 +00003894 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003895 {
Hanno Becker3307b532017-12-27 21:37:21 +00003896 mbedtls_record rec;
3897
3898 rec.buf = ssl->out_iv;
3899 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3900 ( ssl->out_iv - ssl->out_buf );
3901 rec.data_len = ssl->out_msglen;
3902 rec.data_offset = ssl->out_msg - rec.buf;
3903
3904 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3905 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3906 ssl->conf->transport, rec.ver );
3907 rec.type = ssl->out_msgtype;
3908
Hanno Beckera5a2b082019-05-15 14:03:01 +01003909#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003910 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003911 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003912#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003913
Hanno Becker611a83b2018-01-03 14:27:32 +00003914 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003915 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003918 return( ret );
3919 }
3920
Hanno Becker3307b532017-12-27 21:37:21 +00003921 if( rec.data_offset != 0 )
3922 {
3923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3924 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3925 }
3926
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003927 /* Update the record content type and CID. */
3928 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003929#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003930 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003931#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003932 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003933 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3934 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003935 }
3936
Hanno Becker43395762019-05-03 14:46:38 +01003937 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003938
3939#if defined(MBEDTLS_SSL_PROTO_DTLS)
3940 /* In case of DTLS, double-check that we don't exceed
3941 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003942 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003943 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003944 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003945 if( ret < 0 )
3946 return( ret );
3947
3948 if( protected_record_size > (size_t) ret )
3949 {
3950 /* Should never happen */
3951 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3952 }
3953 }
3954#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003955
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003956 /* Now write the potentially updated record content type. */
3957 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003960 "version = [%d:%d], msglen = %d",
3961 ssl->out_hdr[0], ssl->out_hdr[1],
3962 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003965 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003966
3967 ssl->out_left += protected_record_size;
3968 ssl->out_hdr += protected_record_size;
3969 ssl_update_out_pointers( ssl, ssl->transform_out );
3970
Hanno Becker04484622018-08-06 09:49:38 +01003971 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3972 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3973 break;
3974
3975 /* The loop goes to its end iff the counter is wrapping */
3976 if( i == ssl_ep_len( ssl ) )
3977 {
3978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3979 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003981 }
3982
Hanno Becker67bc7c32018-08-06 11:33:50 +01003983#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003984 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003985 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003986 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003987 size_t remaining;
3988 ret = ssl_get_remaining_payload_in_datagram( ssl );
3989 if( ret < 0 )
3990 {
3991 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3992 ret );
3993 return( ret );
3994 }
3995
3996 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003997 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003998 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003999 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004000 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004001 else
4002 {
Hanno Becker513815a2018-08-20 11:56:09 +01004003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004004 }
4005 }
4006#endif /* MBEDTLS_SSL_PROTO_DTLS */
4007
4008 if( ( flush == SSL_FORCE_FLUSH ) &&
4009 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004012 return( ret );
4013 }
4014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004016
4017 return( 0 );
4018}
4019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004021
4022static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4023{
4024 if( ssl->in_msglen < ssl->in_hslen ||
4025 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4026 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4027 {
4028 return( 1 );
4029 }
4030 return( 0 );
4031}
Hanno Becker44650b72018-08-16 12:51:11 +01004032
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004033static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004034{
4035 return( ( ssl->in_msg[9] << 16 ) |
4036 ( ssl->in_msg[10] << 8 ) |
4037 ssl->in_msg[11] );
4038}
4039
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004040static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004041{
4042 return( ( ssl->in_msg[6] << 16 ) |
4043 ( ssl->in_msg[7] << 8 ) |
4044 ssl->in_msg[8] );
4045}
4046
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004047static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004048{
4049 uint32_t msg_len, frag_off, frag_len;
4050
4051 msg_len = ssl_get_hs_total_len( ssl );
4052 frag_off = ssl_get_hs_frag_off( ssl );
4053 frag_len = ssl_get_hs_frag_len( ssl );
4054
4055 if( frag_off > msg_len )
4056 return( -1 );
4057
4058 if( frag_len > msg_len - frag_off )
4059 return( -1 );
4060
4061 if( frag_len + 12 > ssl->in_msglen )
4062 return( -1 );
4063
4064 return( 0 );
4065}
4066
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004067/*
4068 * Mark bits in bitmask (used for DTLS HS reassembly)
4069 */
4070static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4071{
4072 unsigned int start_bits, end_bits;
4073
4074 start_bits = 8 - ( offset % 8 );
4075 if( start_bits != 8 )
4076 {
4077 size_t first_byte_idx = offset / 8;
4078
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004079 /* Special case */
4080 if( len <= start_bits )
4081 {
4082 for( ; len != 0; len-- )
4083 mask[first_byte_idx] |= 1 << ( start_bits - len );
4084
4085 /* Avoid potential issues with offset or len becoming invalid */
4086 return;
4087 }
4088
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004089 offset += start_bits; /* Now offset % 8 == 0 */
4090 len -= start_bits;
4091
4092 for( ; start_bits != 0; start_bits-- )
4093 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4094 }
4095
4096 end_bits = len % 8;
4097 if( end_bits != 0 )
4098 {
4099 size_t last_byte_idx = ( offset + len ) / 8;
4100
4101 len -= end_bits; /* Now len % 8 == 0 */
4102
4103 for( ; end_bits != 0; end_bits-- )
4104 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4105 }
4106
4107 memset( mask + offset / 8, 0xFF, len / 8 );
4108}
4109
4110/*
4111 * Check that bitmask is full
4112 */
4113static int ssl_bitmask_check( unsigned char *mask, size_t len )
4114{
4115 size_t i;
4116
4117 for( i = 0; i < len / 8; i++ )
4118 if( mask[i] != 0xFF )
4119 return( -1 );
4120
4121 for( i = 0; i < len % 8; i++ )
4122 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4123 return( -1 );
4124
4125 return( 0 );
4126}
4127
Hanno Becker56e205e2018-08-16 09:06:12 +01004128/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004129static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004130 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004131{
Hanno Becker56e205e2018-08-16 09:06:12 +01004132 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004133
Hanno Becker56e205e2018-08-16 09:06:12 +01004134 alloc_len = 12; /* Handshake header */
4135 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004136
Hanno Beckerd07df862018-08-16 09:14:58 +01004137 if( add_bitmap )
4138 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004139
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004140 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004141}
Hanno Becker56e205e2018-08-16 09:06:12 +01004142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004144
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004145static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004146{
4147 return( ( ssl->in_msg[1] << 16 ) |
4148 ( ssl->in_msg[2] << 8 ) |
4149 ssl->in_msg[3] );
4150}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004151
Simon Butcher99000142016-10-13 17:21:01 +01004152int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004154 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004157 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004159 }
4160
Hanno Becker12555c62018-08-16 12:47:53 +01004161 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004164 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004165 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004168 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004169 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004170 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004171 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004172
Hanno Becker44650b72018-08-16 12:51:11 +01004173 if( ssl_check_hs_header( ssl ) != 0 )
4174 {
4175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4176 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4177 }
4178
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004179 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004180 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4181 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4182 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4183 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004184 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004185 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4186 {
4187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4188 recv_msg_seq,
4189 ssl->handshake->in_msg_seq ) );
4190 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4191 }
4192
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004193 /* Retransmit only on last message from previous flight, to avoid
4194 * too many retransmissions.
4195 * Besides, No sane server ever retransmits HelloVerifyRequest */
4196 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004200 "message_seq = %d, start_of_flight = %d",
4201 recv_msg_seq,
4202 ssl->handshake->in_flight_start_seq ) );
4203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004207 return( ret );
4208 }
4209 }
4210 else
4211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004213 "message_seq = %d, expected = %d",
4214 recv_msg_seq,
4215 ssl->handshake->in_msg_seq ) );
4216 }
4217
Hanno Becker90333da2017-10-10 11:27:13 +01004218 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004219 }
4220 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004221
Hanno Becker6d97ef52018-08-16 13:09:04 +01004222 /* Message reassembly is handled alongside buffering of future
4223 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004224 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004225 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004226 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004229 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004230 }
4231 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004232 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004234#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004235 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004236 /* With TLS we don't handle fragmentation (for now) */
4237 if( ssl->in_msglen < ssl->in_hslen )
4238 {
4239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4240 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4241 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004242 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004243#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004244
Simon Butcher99000142016-10-13 17:21:01 +01004245 return( 0 );
4246}
4247
4248void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4249{
Hanno Becker0271f962018-08-16 13:23:47 +01004250 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004251
Hanno Becker0271f962018-08-16 13:23:47 +01004252 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004253 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004254 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004255 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004256
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004257 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004259 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004260 ssl->handshake != NULL )
4261 {
Hanno Becker0271f962018-08-16 13:23:47 +01004262 unsigned offset;
4263 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004264
Hanno Becker0271f962018-08-16 13:23:47 +01004265 /* Increment handshake sequence number */
4266 hs->in_msg_seq++;
4267
4268 /*
4269 * Clear up handshake buffering and reassembly structure.
4270 */
4271
4272 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004273 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004274
4275 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004276 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4277 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004278 offset++, hs_buf++ )
4279 {
4280 *hs_buf = *(hs_buf + 1);
4281 }
4282
4283 /* Create a fresh last entry */
4284 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004285 }
4286#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004287}
4288
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004289/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004290 * DTLS anti-replay: RFC 6347 4.1.2.6
4291 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004292 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4293 * Bit n is set iff record number in_window_top - n has been seen.
4294 *
4295 * Usually, in_window_top is the last record number seen and the lsb of
4296 * in_window is set. The only exception is the initial state (record number 0
4297 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4300static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004301{
4302 ssl->in_window_top = 0;
4303 ssl->in_window = 0;
4304}
4305
4306static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4307{
4308 return( ( (uint64_t) buf[0] << 40 ) |
4309 ( (uint64_t) buf[1] << 32 ) |
4310 ( (uint64_t) buf[2] << 24 ) |
4311 ( (uint64_t) buf[3] << 16 ) |
4312 ( (uint64_t) buf[4] << 8 ) |
4313 ( (uint64_t) buf[5] ) );
4314}
4315
4316/*
4317 * Return 0 if sequence number is acceptable, -1 otherwise
4318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004320{
4321 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4322 uint64_t bit;
4323
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004324 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004325 return( 0 );
4326
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004327 if( rec_seqnum > ssl->in_window_top )
4328 return( 0 );
4329
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004330 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004331
4332 if( bit >= 64 )
4333 return( -1 );
4334
4335 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4336 return( -1 );
4337
4338 return( 0 );
4339}
4340
4341/*
4342 * Update replay window on new validated record
4343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345{
4346 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4347
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004348 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004349 return;
4350
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004351 if( rec_seqnum > ssl->in_window_top )
4352 {
4353 /* Update window_top and the contents of the window */
4354 uint64_t shift = rec_seqnum - ssl->in_window_top;
4355
4356 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004357 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004358 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004359 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004360 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004361 ssl->in_window |= 1;
4362 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004363
4364 ssl->in_window_top = rec_seqnum;
4365 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004366 else
4367 {
4368 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004369 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004370
4371 if( bit < 64 ) /* Always true, but be extra sure */
4372 ssl->in_window |= (uint64_t) 1 << bit;
4373 }
4374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004376
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004377#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004378/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004379static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4380
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004381/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004382 * Without any SSL context, check if a datagram looks like a ClientHello with
4383 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004384 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004385 *
4386 * - if cookie is valid, return 0
4387 * - if ClientHello looks superficially valid but cookie is not,
4388 * fill obuf and set olen, then
4389 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4390 * - otherwise return a specific error code
4391 */
4392static int ssl_check_dtls_clihlo_cookie(
4393 mbedtls_ssl_cookie_write_t *f_cookie_write,
4394 mbedtls_ssl_cookie_check_t *f_cookie_check,
4395 void *p_cookie,
4396 const unsigned char *cli_id, size_t cli_id_len,
4397 const unsigned char *in, size_t in_len,
4398 unsigned char *obuf, size_t buf_len, size_t *olen )
4399{
4400 size_t sid_len, cookie_len;
4401 unsigned char *p;
4402
4403 if( f_cookie_write == NULL || f_cookie_check == NULL )
4404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4405
4406 /*
4407 * Structure of ClientHello with record and handshake headers,
4408 * and expected values. We don't need to check a lot, more checks will be
4409 * done when actually parsing the ClientHello - skipping those checks
4410 * avoids code duplication and does not make cookie forging any easier.
4411 *
4412 * 0-0 ContentType type; copied, must be handshake
4413 * 1-2 ProtocolVersion version; copied
4414 * 3-4 uint16 epoch; copied, must be 0
4415 * 5-10 uint48 sequence_number; copied
4416 * 11-12 uint16 length; (ignored)
4417 *
4418 * 13-13 HandshakeType msg_type; (ignored)
4419 * 14-16 uint24 length; (ignored)
4420 * 17-18 uint16 message_seq; copied
4421 * 19-21 uint24 fragment_offset; copied, must be 0
4422 * 22-24 uint24 fragment_length; (ignored)
4423 *
4424 * 25-26 ProtocolVersion client_version; (ignored)
4425 * 27-58 Random random; (ignored)
4426 * 59-xx SessionID session_id; 1 byte len + sid_len content
4427 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4428 * ...
4429 *
4430 * Minimum length is 61 bytes.
4431 */
4432 if( in_len < 61 ||
4433 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4434 in[3] != 0 || in[4] != 0 ||
4435 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4436 {
4437 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4438 }
4439
4440 sid_len = in[59];
4441 if( sid_len > in_len - 61 )
4442 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4443
4444 cookie_len = in[60 + sid_len];
4445 if( cookie_len > in_len - 60 )
4446 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4447
4448 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4449 cli_id, cli_id_len ) == 0 )
4450 {
4451 /* Valid cookie */
4452 return( 0 );
4453 }
4454
4455 /*
4456 * If we get here, we've got an invalid cookie, let's prepare HVR.
4457 *
4458 * 0-0 ContentType type; copied
4459 * 1-2 ProtocolVersion version; copied
4460 * 3-4 uint16 epoch; copied
4461 * 5-10 uint48 sequence_number; copied
4462 * 11-12 uint16 length; olen - 13
4463 *
4464 * 13-13 HandshakeType msg_type; hello_verify_request
4465 * 14-16 uint24 length; olen - 25
4466 * 17-18 uint16 message_seq; copied
4467 * 19-21 uint24 fragment_offset; copied
4468 * 22-24 uint24 fragment_length; olen - 25
4469 *
4470 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4471 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4472 *
4473 * Minimum length is 28.
4474 */
4475 if( buf_len < 28 )
4476 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4477
4478 /* Copy most fields and adapt others */
4479 memcpy( obuf, in, 25 );
4480 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4481 obuf[25] = 0xfe;
4482 obuf[26] = 0xff;
4483
4484 /* Generate and write actual cookie */
4485 p = obuf + 28;
4486 if( f_cookie_write( p_cookie,
4487 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4488 {
4489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4490 }
4491
4492 *olen = p - obuf;
4493
4494 /* Go back and fill length fields */
4495 obuf[27] = (unsigned char)( *olen - 28 );
4496
4497 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4498 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4499 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4500
4501 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4502 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4503
4504 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4505}
4506
4507/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004508 * Handle possible client reconnect with the same UDP quadruplet
4509 * (RFC 6347 Section 4.2.8).
4510 *
4511 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4512 * that looks like a ClientHello.
4513 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004514 * - if the input looks like a ClientHello without cookies,
4515 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004516 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004517 * - if the input looks like a ClientHello with a valid cookie,
4518 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004519 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004520 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004521 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004522 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004523 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4524 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004525 */
4526static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4527{
4528 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004529 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004530
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004531 ret = ssl_check_dtls_clihlo_cookie(
4532 ssl->conf->f_cookie_write,
4533 ssl->conf->f_cookie_check,
4534 ssl->conf->p_cookie,
4535 ssl->cli_id, ssl->cli_id_len,
4536 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004537 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004538
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004539 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4540
4541 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004542 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004543 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004544 * If the error is permanent we'll catch it later,
4545 * if it's not, then hopefully it'll work next time. */
4546 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4547
4548 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004549 }
4550
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004551 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004552 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004553 /* Got a valid cookie, partially reset context */
4554 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4555 {
4556 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4557 return( ret );
4558 }
4559
4560 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004561 }
4562
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004563 return( ret );
4564}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004565#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004566
Hanno Becker46483f12019-05-03 13:25:54 +01004567static int ssl_check_record_type( uint8_t record_type )
4568{
4569 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4570 record_type != MBEDTLS_SSL_MSG_ALERT &&
4571 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4572 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4573 {
4574 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4575 }
4576
4577 return( 0 );
4578}
4579
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004580/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004581 * ContentType type;
4582 * ProtocolVersion version;
4583 * uint16 epoch; // DTLS only
4584 * uint48 sequence_number; // DTLS only
4585 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004586 *
4587 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004588 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004589 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4590 *
4591 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004592 * 1. proceed with the record if this function returns 0
4593 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4594 * 3. return CLIENT_RECONNECT if this function return that value
4595 * 4. drop the whole datagram if this function returns anything else.
4596 * Point 2 is needed when the peer is resending, and we have already received
4597 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004600{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004601 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004602 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004603
Hanno Becker8b09b732019-05-08 12:03:28 +01004604 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004605
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004607 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004608
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004609 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004610#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004611 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004612 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4613 ssl->conf->cid_len != 0 )
4614 {
4615 /* Shift pointers to account for record header including CID
4616 * struct {
4617 * ContentType special_type = tls12_cid;
4618 * ProtocolVersion version;
4619 * uint16 epoch;
4620 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004621 * opaque cid[cid_length]; // Additional field compared to
4622 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004623 * uint16 length;
4624 * opaque enc_content[DTLSCiphertext.length];
4625 * } DTLSCiphertext;
4626 */
4627
4628 /* So far, we only support static CID lengths
4629 * fixed in the configuration. */
4630 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4631 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4632 }
4633 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004634#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004635 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004638
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004639#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004640 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004641 * Section 4.1.2.7, that is, send alert only with TLS */
4642 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4643 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004644 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4645 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004646 }
4647#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004650 }
4651
4652 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004653 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 }
4658
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004659 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4662 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 }
4664
Hanno Becker8b09b732019-05-08 12:03:28 +01004665 /* Now that the total length of the record header is known, ensure
4666 * that the current datagram is large enough to hold it.
4667 * This would fail, for example, if we received a datagram of
4668 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4669 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4670 if( ret != 0 )
4671 {
4672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4673 return( ret );
4674 }
4675 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4676
4677 /* Parse and validate record length
4678 * This must happen after the CID parsing because
4679 * its position in the record header depends on
4680 * the presence of a CID. */
4681
4682 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004683 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004684 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4687 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004688 }
4689
Hanno Becker8b09b732019-05-08 12:03:28 +01004690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004691 "version = [%d:%d], msglen = %d",
4692 ssl->in_msgtype,
4693 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004694
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004695 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004696 * DTLS-related tests.
4697 * Check epoch before checking length constraint because
4698 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4699 * message gets duplicated before the corresponding Finished message,
4700 * the second ChangeCipherSpec should be discarded because it belongs
4701 * to an old epoch, but not because its length is shorter than
4702 * the minimum record length for packets using the new record transform.
4703 * Note that these two kinds of failures are handled differently,
4704 * as an unexpected record is silently skipped but an invalid
4705 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004706 */
4707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004708 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004709 {
4710 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4711
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004712 /* Check epoch (and sequence number) with DTLS */
4713 if( rec_epoch != ssl->in_epoch )
4714 {
4715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4716 "expected %d, received %d",
4717 ssl->in_epoch, rec_epoch ) );
4718
4719#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4720 /*
4721 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4722 * access the first byte of record content (handshake type), as we
4723 * have an active transform (possibly iv_len != 0), so use the
4724 * fact that the record header len is 13 instead.
4725 */
4726 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4727 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4728 rec_epoch == 0 &&
4729 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4730 ssl->in_left > 13 &&
4731 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4732 {
4733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4734 "from the same port" ) );
4735 return( ssl_handle_possible_reconnect( ssl ) );
4736 }
4737 else
4738#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004739 {
4740 /* Consider buffering the record. */
4741 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4742 {
4743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4744 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4745 }
4746
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004747 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004748 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004749 }
4750
4751#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4752 /* Replay detection only works for the current epoch */
4753 if( rec_epoch == ssl->in_epoch &&
4754 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4755 {
4756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4757 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4758 }
4759#endif
4760 }
4761#endif /* MBEDTLS_SSL_PROTO_DTLS */
4762
Hanno Becker52c6dc62017-05-26 16:07:36 +01004763
4764 /* Check length against bounds of the current transform and version */
4765 if( ssl->transform_in == NULL )
4766 {
4767 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004768 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004769 {
4770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4771 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4772 }
4773 }
4774 else
4775 {
4776 if( ssl->in_msglen < ssl->transform_in->minlen )
4777 {
4778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4779 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4780 }
4781
4782#if defined(MBEDTLS_SSL_PROTO_SSL3)
4783 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004784 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004785 {
4786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4787 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4788 }
4789#endif
4790#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4791 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4792 /*
4793 * TLS encrypted messages can have up to 256 bytes of padding
4794 */
4795 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4796 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004797 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004798 {
4799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4800 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4801 }
4802#endif
4803 }
4804
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004805 return( 0 );
4806}
Paul Bakker5121ce52009-01-03 21:22:43 +00004807
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004808/*
4809 * If applicable, decrypt (and decompress) record content
4810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004812{
4813 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004816 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4819 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 ret = mbedtls_ssl_hw_record_read( ssl );
4824 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4827 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004828 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004829
4830 if( ret == 0 )
4831 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004832 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004834 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004835 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004836 mbedtls_record rec;
4837
4838 rec.buf = ssl->in_iv;
4839 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4840 - ( ssl->in_iv - ssl->in_buf );
4841 rec.data_len = ssl->in_msglen;
4842 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004843#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004844 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004845 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004846#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004847
4848 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4849 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4850 ssl->conf->transport, rec.ver );
4851 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004852 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4853 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004856
Hanno Beckera5a2b082019-05-15 14:03:01 +01004857#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004858 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4859 ssl->conf->ignore_unexpected_cid
4860 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4861 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004863 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004864 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004865#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004866
Paul Bakker5121ce52009-01-03 21:22:43 +00004867 return( ret );
4868 }
4869
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004870 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004871 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004872 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4873 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004875
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004876 /* The record content type may change during decryption,
4877 * so re-read it. */
4878 ssl->in_msgtype = rec.type;
4879 /* Also update the input buffer, because unfortunately
4880 * the server-side ssl_parse_client_hello() reparses the
4881 * record header when receiving a ClientHello initiating
4882 * a renegotiation. */
4883 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004884 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004885 ssl->in_msglen = rec.data_len;
4886 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4887 ssl->in_len[1] = (unsigned char)( rec.data_len );
4888
Paul Bakker5121ce52009-01-03 21:22:43 +00004889 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4890 ssl->in_msg, ssl->in_msglen );
4891
Hanno Beckera5a2b082019-05-15 14:03:01 +01004892#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004893 /* We have already checked the record content type
4894 * in ssl_parse_record_header(), failing or silently
4895 * dropping the record in the case of an unknown type.
4896 *
4897 * Since with the use of CIDs, the record content type
4898 * might change during decryption, re-check the record
4899 * content type, but treat a failure as fatal this time. */
4900 if( ssl_check_record_type( ssl->in_msgtype ) )
4901 {
4902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4903 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4904 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004905#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004906
Angus Grattond8213d02016-05-25 20:56:48 +10004907 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4910 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004911 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004912 else if( ssl->in_msglen == 0 )
4913 {
4914#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4915 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4916 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4917 {
4918 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4920 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4921 }
4922#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4923
4924 ssl->nb_zero++;
4925
4926 /*
4927 * Three or more empty messages may be a DoS attack
4928 * (excessive CPU consumption).
4929 */
4930 if( ssl->nb_zero > 3 )
4931 {
4932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004933 "messages, possible DoS attack" ) );
4934 /* Treat the records as if they were not properly authenticated,
4935 * thereby failing the connection if we see more than allowed
4936 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004937 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4938 }
4939 }
4940 else
4941 ssl->nb_zero = 0;
4942
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004943 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4944#if defined(MBEDTLS_SSL_PROTO_TLS)
4945 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004946 {
4947 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004948 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004949 if( ++ssl->in_ctr[i - 1] != 0 )
4950 break;
4951
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004952 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004953 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004954 {
4955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4956 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4957 }
4958 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004959#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004960
Paul Bakker5121ce52009-01-03 21:22:43 +00004961 }
4962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004964 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004966 {
4967 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004970 return( ret );
4971 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004976 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004979 }
4980#endif
4981
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004982 return( 0 );
4983}
4984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004986
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004987/*
4988 * Read a record.
4989 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004990 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4991 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4992 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004993 */
Hanno Becker1097b342018-08-15 14:09:41 +01004994
4995/* Helper functions for mbedtls_ssl_read_record(). */
4996static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004997static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4998static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004999
Hanno Becker327c93b2018-08-15 13:56:18 +01005000int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005001 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005002{
5003 int ret;
5004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005006
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005007 if( ssl->keep_current_message == 0 )
5008 {
5009 do {
Simon Butcher99000142016-10-13 17:21:01 +01005010
Hanno Becker26994592018-08-15 14:14:59 +01005011 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005012 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005013 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005014
Hanno Beckere74d5562018-08-15 14:26:08 +01005015 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005016 {
Hanno Becker40f50842018-08-15 14:48:01 +01005017#if defined(MBEDTLS_SSL_PROTO_DTLS)
5018 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005019
Hanno Becker40f50842018-08-15 14:48:01 +01005020 /* We only check for buffered messages if the
5021 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005022 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005023 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005024 {
Hanno Becker40f50842018-08-15 14:48:01 +01005025 if( ssl_load_buffered_message( ssl ) == 0 )
5026 have_buffered = 1;
5027 }
5028
5029 if( have_buffered == 0 )
5030#endif /* MBEDTLS_SSL_PROTO_DTLS */
5031 {
5032 ret = ssl_get_next_record( ssl );
5033 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5034 continue;
5035
5036 if( ret != 0 )
5037 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005038 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005039 return( ret );
5040 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005041 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005042 }
5043
5044 ret = mbedtls_ssl_handle_message_type( ssl );
5045
Hanno Becker40f50842018-08-15 14:48:01 +01005046#if defined(MBEDTLS_SSL_PROTO_DTLS)
5047 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5048 {
5049 /* Buffer future message */
5050 ret = ssl_buffer_message( ssl );
5051 if( ret != 0 )
5052 return( ret );
5053
5054 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5055 }
5056#endif /* MBEDTLS_SSL_PROTO_DTLS */
5057
Hanno Becker90333da2017-10-10 11:27:13 +01005058 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5059 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005060
5061 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005062 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005063 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005064 return( ret );
5065 }
5066
Hanno Becker327c93b2018-08-15 13:56:18 +01005067 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005068 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005069 {
5070 mbedtls_ssl_update_handshake_status( ssl );
5071 }
Simon Butcher99000142016-10-13 17:21:01 +01005072 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005073 else
Simon Butcher99000142016-10-13 17:21:01 +01005074 {
Hanno Becker02f59072018-08-15 14:00:24 +01005075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005076 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005077 }
5078
5079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5080
5081 return( 0 );
5082}
5083
Hanno Becker40f50842018-08-15 14:48:01 +01005084#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005085static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005086{
Hanno Becker40f50842018-08-15 14:48:01 +01005087 if( ssl->in_left > ssl->next_record_offset )
5088 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005089
Hanno Becker40f50842018-08-15 14:48:01 +01005090 return( 0 );
5091}
5092
5093static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5094{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005095 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005096 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005097 int ret = 0;
5098
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005099 if( hs == NULL )
5100 return( -1 );
5101
Hanno Beckere00ae372018-08-20 09:39:42 +01005102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5103
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005104 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5105 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5106 {
5107 /* Check if we have seen a ChangeCipherSpec before.
5108 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005109 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005110 {
5111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5112 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005113 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005114 }
5115
Hanno Becker39b8bc92018-08-28 17:17:13 +01005116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005117 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5118 ssl->in_msglen = 1;
5119 ssl->in_msg[0] = 1;
5120
5121 /* As long as they are equal, the exact value doesn't matter. */
5122 ssl->in_left = 0;
5123 ssl->next_record_offset = 0;
5124
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005125 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005126 goto exit;
5127 }
Hanno Becker37f95322018-08-16 13:55:32 +01005128
Hanno Beckerb8f50142018-08-28 10:01:34 +01005129#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005130 /* Debug only */
5131 {
5132 unsigned offset;
5133 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5134 {
5135 hs_buf = &hs->buffering.hs[offset];
5136 if( hs_buf->is_valid == 1 )
5137 {
5138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5139 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005140 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005141 }
5142 }
5143 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005144#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005145
5146 /* Check if we have buffered and/or fully reassembled the
5147 * next handshake message. */
5148 hs_buf = &hs->buffering.hs[0];
5149 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5150 {
5151 /* Synthesize a record containing the buffered HS message. */
5152 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5153 ( hs_buf->data[2] << 8 ) |
5154 hs_buf->data[3];
5155
5156 /* Double-check that we haven't accidentally buffered
5157 * a message that doesn't fit into the input buffer. */
5158 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5159 {
5160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5162 }
5163
5164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5165 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5166 hs_buf->data, msg_len + 12 );
5167
5168 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5169 ssl->in_hslen = msg_len + 12;
5170 ssl->in_msglen = msg_len + 12;
5171 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5172
5173 ret = 0;
5174 goto exit;
5175 }
5176 else
5177 {
5178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5179 hs->in_msg_seq ) );
5180 }
5181
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005182 ret = -1;
5183
5184exit:
5185
5186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5187 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005188}
5189
Hanno Beckera02b0b42018-08-21 17:20:27 +01005190static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5191 size_t desired )
5192{
5193 int offset;
5194 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5196 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005197
Hanno Becker01315ea2018-08-21 17:22:17 +01005198 /* Get rid of future records epoch first, if such exist. */
5199 ssl_free_buffered_record( ssl );
5200
5201 /* Check if we have enough space available now. */
5202 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5203 hs->buffering.total_bytes_buffered ) )
5204 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005206 return( 0 );
5207 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005208
Hanno Becker4f432ad2018-08-28 10:02:32 +01005209 /* We don't have enough space to buffer the next expected handshake
5210 * message. Remove buffers used for future messages to gain space,
5211 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005212 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5213 offset >= 0; offset-- )
5214 {
5215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5216 offset ) );
5217
Hanno Beckerb309b922018-08-23 13:18:05 +01005218 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005219
5220 /* Check if we have enough space available now. */
5221 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5222 hs->buffering.total_bytes_buffered ) )
5223 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005225 return( 0 );
5226 }
5227 }
5228
5229 return( -1 );
5230}
5231
Hanno Becker40f50842018-08-15 14:48:01 +01005232static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5233{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005234 int ret = 0;
5235 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5236
5237 if( hs == NULL )
5238 return( 0 );
5239
5240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5241
5242 switch( ssl->in_msgtype )
5243 {
5244 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005246
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005247 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005248 break;
5249
5250 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005251 {
5252 unsigned recv_msg_seq_offset;
5253 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5254 mbedtls_ssl_hs_buffer *hs_buf;
5255 size_t msg_len = ssl->in_hslen - 12;
5256
5257 /* We should never receive an old handshake
5258 * message - double-check nonetheless. */
5259 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5260 {
5261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5263 }
5264
5265 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5266 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5267 {
5268 /* Silently ignore -- message too far in the future */
5269 MBEDTLS_SSL_DEBUG_MSG( 2,
5270 ( "Ignore future HS message with sequence number %u, "
5271 "buffering window %u - %u",
5272 recv_msg_seq, ssl->handshake->in_msg_seq,
5273 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5274
5275 goto exit;
5276 }
5277
5278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5279 recv_msg_seq, recv_msg_seq_offset ) );
5280
5281 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5282
5283 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005284 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005285 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005286 size_t reassembly_buf_sz;
5287
Hanno Becker37f95322018-08-16 13:55:32 +01005288 hs_buf->is_fragmented =
5289 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5290
5291 /* We copy the message back into the input buffer
5292 * after reassembly, so check that it's not too large.
5293 * This is an implementation-specific limitation
5294 * and not one from the standard, hence it is not
5295 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005296 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005297 {
5298 /* Ignore message */
5299 goto exit;
5300 }
5301
Hanno Beckere0b150f2018-08-21 15:51:03 +01005302 /* Check if we have enough space to buffer the message. */
5303 if( hs->buffering.total_bytes_buffered >
5304 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5305 {
5306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5308 }
5309
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005310 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5311 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005312
5313 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5314 hs->buffering.total_bytes_buffered ) )
5315 {
5316 if( recv_msg_seq_offset > 0 )
5317 {
5318 /* If we can't buffer a future message because
5319 * of space limitations -- ignore. */
5320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5321 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5322 (unsigned) hs->buffering.total_bytes_buffered ) );
5323 goto exit;
5324 }
Hanno Beckere1801392018-08-21 16:51:05 +01005325 else
5326 {
5327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5328 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5329 (unsigned) hs->buffering.total_bytes_buffered ) );
5330 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005331
Hanno Beckera02b0b42018-08-21 17:20:27 +01005332 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005333 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5335 (unsigned) msg_len,
5336 (unsigned) reassembly_buf_sz,
5337 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005338 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005339 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5340 goto exit;
5341 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005342 }
5343
5344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5345 msg_len ) );
5346
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005347 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5348 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005349 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005350 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005351 goto exit;
5352 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005353 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005354
5355 /* Prepare final header: copy msg_type, length and message_seq,
5356 * then add standardised fragment_offset and fragment_length */
5357 memcpy( hs_buf->data, ssl->in_msg, 6 );
5358 memset( hs_buf->data + 6, 0, 3 );
5359 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5360
5361 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005362
5363 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005364 }
5365 else
5366 {
5367 /* Make sure msg_type and length are consistent */
5368 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5369 {
5370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5371 /* Ignore */
5372 goto exit;
5373 }
5374 }
5375
Hanno Becker4422bbb2018-08-20 09:40:19 +01005376 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005377 {
5378 size_t frag_len, frag_off;
5379 unsigned char * const msg = hs_buf->data + 12;
5380
5381 /*
5382 * Check and copy current fragment
5383 */
5384
5385 /* Validation of header fields already done in
5386 * mbedtls_ssl_prepare_handshake_record(). */
5387 frag_off = ssl_get_hs_frag_off( ssl );
5388 frag_len = ssl_get_hs_frag_len( ssl );
5389
5390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5391 frag_off, frag_len ) );
5392 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5393
5394 if( hs_buf->is_fragmented )
5395 {
5396 unsigned char * const bitmask = msg + msg_len;
5397 ssl_bitmask_set( bitmask, frag_off, frag_len );
5398 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5399 msg_len ) == 0 );
5400 }
5401 else
5402 {
5403 hs_buf->is_complete = 1;
5404 }
5405
5406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5407 hs_buf->is_complete ? "" : "not yet " ) );
5408 }
5409
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005410 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005411 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005412
5413 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005414 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005415 break;
5416 }
5417
5418exit:
5419
5420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5421 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005422}
5423#endif /* MBEDTLS_SSL_PROTO_DTLS */
5424
Hanno Becker1097b342018-08-15 14:09:41 +01005425static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005426{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005427 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005428 * Consume last content-layer message and potentially
5429 * update in_msglen which keeps track of the contents'
5430 * consumption state.
5431 *
5432 * (1) Handshake messages:
5433 * Remove last handshake message, move content
5434 * and adapt in_msglen.
5435 *
5436 * (2) Alert messages:
5437 * Consume whole record content, in_msglen = 0.
5438 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005439 * (3) Change cipher spec:
5440 * Consume whole record content, in_msglen = 0.
5441 *
5442 * (4) Application data:
5443 * Don't do anything - the record layer provides
5444 * the application data as a stream transport
5445 * and consumes through mbedtls_ssl_read only.
5446 *
5447 */
5448
5449 /* Case (1): Handshake messages */
5450 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005451 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005452 /* Hard assertion to be sure that no application data
5453 * is in flight, as corrupting ssl->in_msglen during
5454 * ssl->in_offt != NULL is fatal. */
5455 if( ssl->in_offt != NULL )
5456 {
5457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5459 }
5460
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005461 /*
5462 * Get next Handshake message in the current record
5463 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005464
Hanno Becker4a810fb2017-05-24 16:27:30 +01005465 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005466 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005467 * current handshake content: If DTLS handshake
5468 * fragmentation is used, that's the fragment
5469 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005470 * size here is faulty and should be changed at
5471 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005472 * (2) While it doesn't seem to cause problems, one
5473 * has to be very careful not to assume that in_hslen
5474 * is always <= in_msglen in a sensible communication.
5475 * Again, it's wrong for DTLS handshake fragmentation.
5476 * The following check is therefore mandatory, and
5477 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005478 * Additionally, ssl->in_hslen might be arbitrarily out of
5479 * bounds after handling a DTLS message with an unexpected
5480 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005481 */
5482 if( ssl->in_hslen < ssl->in_msglen )
5483 {
5484 ssl->in_msglen -= ssl->in_hslen;
5485 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5486 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005487
Hanno Becker4a810fb2017-05-24 16:27:30 +01005488 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5489 ssl->in_msg, ssl->in_msglen );
5490 }
5491 else
5492 {
5493 ssl->in_msglen = 0;
5494 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005495
Hanno Becker4a810fb2017-05-24 16:27:30 +01005496 ssl->in_hslen = 0;
5497 }
5498 /* Case (4): Application data */
5499 else if( ssl->in_offt != NULL )
5500 {
5501 return( 0 );
5502 }
5503 /* Everything else (CCS & Alerts) */
5504 else
5505 {
5506 ssl->in_msglen = 0;
5507 }
5508
Hanno Becker1097b342018-08-15 14:09:41 +01005509 return( 0 );
5510}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005511
Hanno Beckere74d5562018-08-15 14:26:08 +01005512static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5513{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005514 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005515 return( 1 );
5516
5517 return( 0 );
5518}
5519
Hanno Becker5f066e72018-08-16 14:56:31 +01005520#if defined(MBEDTLS_SSL_PROTO_DTLS)
5521
5522static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5523{
5524 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5525 if( hs == NULL )
5526 return;
5527
Hanno Becker01315ea2018-08-21 17:22:17 +01005528 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005529 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005530 hs->buffering.total_bytes_buffered -=
5531 hs->buffering.future_record.len;
5532
5533 mbedtls_free( hs->buffering.future_record.data );
5534 hs->buffering.future_record.data = NULL;
5535 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005536}
5537
5538static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5539{
5540 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5541 unsigned char * rec;
5542 size_t rec_len;
5543 unsigned rec_epoch;
5544
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005545 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005546 return( 0 );
5547
5548 if( hs == NULL )
5549 return( 0 );
5550
Hanno Becker5f066e72018-08-16 14:56:31 +01005551 rec = hs->buffering.future_record.data;
5552 rec_len = hs->buffering.future_record.len;
5553 rec_epoch = hs->buffering.future_record.epoch;
5554
5555 if( rec == NULL )
5556 return( 0 );
5557
Hanno Becker4cb782d2018-08-20 11:19:05 +01005558 /* Only consider loading future records if the
5559 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005560 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005561 return( 0 );
5562
Hanno Becker5f066e72018-08-16 14:56:31 +01005563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5564
5565 if( rec_epoch != ssl->in_epoch )
5566 {
5567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5568 goto exit;
5569 }
5570
5571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5572
5573 /* Double-check that the record is not too large */
5574 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5575 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5576 {
5577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5579 }
5580
5581 memcpy( ssl->in_hdr, rec, rec_len );
5582 ssl->in_left = rec_len;
5583 ssl->next_record_offset = 0;
5584
5585 ssl_free_buffered_record( ssl );
5586
5587exit:
5588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5589 return( 0 );
5590}
5591
5592static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5593{
5594 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5595 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005596 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005597
5598 /* Don't buffer future records outside handshakes. */
5599 if( hs == NULL )
5600 return( 0 );
5601
5602 /* Only buffer handshake records (we are only interested
5603 * in Finished messages). */
5604 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5605 return( 0 );
5606
5607 /* Don't buffer more than one future epoch record. */
5608 if( hs->buffering.future_record.data != NULL )
5609 return( 0 );
5610
Hanno Becker01315ea2018-08-21 17:22:17 +01005611 /* Don't buffer record if there's not enough buffering space remaining. */
5612 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5613 hs->buffering.total_bytes_buffered ) )
5614 {
5615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5616 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5617 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005618 return( 0 );
5619 }
5620
Hanno Becker5f066e72018-08-16 14:56:31 +01005621 /* Buffer record */
5622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5623 ssl->in_epoch + 1 ) );
5624 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5625 rec_hdr_len + ssl->in_msglen );
5626
5627 /* ssl_parse_record_header() only considers records
5628 * of the next epoch as candidates for buffering. */
5629 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005630 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005631
5632 hs->buffering.future_record.data =
5633 mbedtls_calloc( 1, hs->buffering.future_record.len );
5634 if( hs->buffering.future_record.data == NULL )
5635 {
5636 /* If we run out of RAM trying to buffer a
5637 * record from the next epoch, just ignore. */
5638 return( 0 );
5639 }
5640
Hanno Becker01315ea2018-08-21 17:22:17 +01005641 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005642
Hanno Becker01315ea2018-08-21 17:22:17 +01005643 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005644 return( 0 );
5645}
5646
5647#endif /* MBEDTLS_SSL_PROTO_DTLS */
5648
Hanno Beckere74d5562018-08-15 14:26:08 +01005649static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005650{
5651 int ret;
5652
Hanno Becker5f066e72018-08-16 14:56:31 +01005653#if defined(MBEDTLS_SSL_PROTO_DTLS)
5654 /* We might have buffered a future record; if so,
5655 * and if the epoch matches now, load it.
5656 * On success, this call will set ssl->in_left to
5657 * the length of the buffered record, so that
5658 * the calls to ssl_fetch_input() below will
5659 * essentially be no-ops. */
5660 ret = ssl_load_buffered_record( ssl );
5661 if( ret != 0 )
5662 return( ret );
5663#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005664
Hanno Becker8b09b732019-05-08 12:03:28 +01005665 /* Reset in pointers to default state for TLS/DTLS records,
5666 * assuming no CID and no offset between record content and
5667 * record plaintext. */
5668 ssl_update_in_pointers( ssl );
5669
5670 /* Ensure that we have enough space available for the default form
5671 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5672 * with no space for CIDs counted in). */
5673 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5674 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005677 return( ret );
5678 }
5679
5680 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005683 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005684 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005685 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005686 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5687 {
5688 ret = ssl_buffer_future_record( ssl );
5689 if( ret != 0 )
5690 return( ret );
5691
5692 /* Fall through to handling of unexpected records */
5693 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5694 }
5695
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005696 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5697 {
5698 /* Skip unexpected record (but not whole datagram) */
5699 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005700 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005701
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5703 "(header)" ) );
5704 }
5705 else
5706 {
5707 /* Skip invalid record and the rest of the datagram */
5708 ssl->next_record_offset = 0;
5709 ssl->in_left = 0;
5710
5711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5712 "(header)" ) );
5713 }
5714
5715 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005716 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005717 }
5718#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005719 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005720 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005721
5722 /*
5723 * Read and optionally decrypt the message contents
5724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005726 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005729 return( ret );
5730 }
5731
5732 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005734 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005735 {
Hanno Becker43395762019-05-03 14:46:38 +01005736 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005737 if( ssl->next_record_offset < ssl->in_left )
5738 {
5739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5740 }
5741 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005742 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005743#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005744#if defined(MBEDTLS_SSL_PROTO_TLS)
5745 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005746 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005747 }
5748#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005749
5750 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005753 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005754 {
5755 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005756 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005757 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005758 /* Except when waiting for Finished as a bad mac here
5759 * probably means something went wrong in the handshake
5760 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5761 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5762 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5763 {
5764#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5765 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5766 {
5767 mbedtls_ssl_send_alert_message( ssl,
5768 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5769 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5770 }
5771#endif
5772 return( ret );
5773 }
5774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005776 if( ssl->conf->badmac_limit != 0 &&
5777 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5780 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005781 }
5782#endif
5783
Hanno Becker4a810fb2017-05-24 16:27:30 +01005784 /* As above, invalid records cause
5785 * dismissal of the whole datagram. */
5786
5787 ssl->next_record_offset = 0;
5788 ssl->in_left = 0;
5789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005791 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005792 }
5793
5794 return( ret );
5795 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005796 MBEDTLS_SSL_TRANSPORT_ELSE
5797#endif /* MBEDTLS_SSL_PROTO_DTLS */
5798#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005799 {
5800 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5802 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 mbedtls_ssl_send_alert_message( ssl,
5805 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5806 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005807 }
5808#endif
5809 return( ret );
5810 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005811#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005812 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005813
Simon Butcher99000142016-10-13 17:21:01 +01005814 return( 0 );
5815}
5816
5817int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5818{
5819 int ret;
5820
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005821 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005822 * Handle particular types of records
5823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 {
Simon Butcher99000142016-10-13 17:21:01 +01005826 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5827 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005828 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005829 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005830 }
5831
Hanno Beckere678eaa2018-08-21 14:57:46 +01005832 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005833 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005834 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005835 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5837 ssl->in_msglen ) );
5838 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005839 }
5840
Hanno Beckere678eaa2018-08-21 14:57:46 +01005841 if( ssl->in_msg[0] != 1 )
5842 {
5843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5844 ssl->in_msg[0] ) );
5845 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5846 }
5847
5848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005849 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005850 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5851 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5852 {
5853 if( ssl->handshake == NULL )
5854 {
5855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5856 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5857 }
5858
5859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5860 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5861 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005862#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005863 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005866 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005867 if( ssl->in_msglen != 2 )
5868 {
5869 /* Note: Standard allows for more than one 2 byte alert
5870 to be packed in a single message, but Mbed TLS doesn't
5871 currently support this. */
5872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5873 ssl->in_msglen ) );
5874 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5875 }
5876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005878 ssl->in_msg[0], ssl->in_msg[1] ) );
5879
5880 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005881 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005886 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005888 }
5889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5891 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5894 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005896
5897#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5898 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5899 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5900 {
Hanno Becker90333da2017-10-10 11:27:13 +01005901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005902 /* Will be handled when trying to parse ServerHello */
5903 return( 0 );
5904 }
5905#endif
5906
5907#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5908 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5909 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5910 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5911 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5912 {
5913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5914 /* Will be handled in mbedtls_ssl_parse_certificate() */
5915 return( 0 );
5916 }
5917#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5918
5919 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005920 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005921 }
5922
Hanno Beckerc76c6192017-06-06 10:03:17 +01005923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005924 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005925 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005926 /* Drop unexpected ApplicationData records,
5927 * except at the beginning of renegotiations */
5928 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5929 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5930#if defined(MBEDTLS_SSL_RENEGOTIATION)
5931 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5932 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005933#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005934 )
5935 {
5936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5937 return( MBEDTLS_ERR_SSL_NON_FATAL );
5938 }
5939
5940 if( ssl->handshake != NULL &&
5941 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5942 {
5943 ssl_handshake_wrapup_free_hs_transform( ssl );
5944 }
5945 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005946#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005947
Paul Bakker5121ce52009-01-03 21:22:43 +00005948 return( 0 );
5949}
5950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005952{
5953 int ret;
5954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5956 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5957 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005958 {
5959 return( ret );
5960 }
5961
5962 return( 0 );
5963}
5964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005966 unsigned char level,
5967 unsigned char message )
5968{
5969 int ret;
5970
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005971 if( ssl == NULL || ssl->conf == NULL )
5972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005978 ssl->out_msglen = 2;
5979 ssl->out_msg[0] = level;
5980 ssl->out_msg[1] = message;
5981
Hanno Becker67bc7c32018-08-06 11:33:50 +01005982 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005985 return( ret );
5986 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005988
5989 return( 0 );
5990}
5991
Hanno Becker17572472019-02-08 07:19:04 +00005992#if defined(MBEDTLS_X509_CRT_PARSE_C)
5993static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5994{
5995#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5996 if( session->peer_cert != NULL )
5997 {
5998 mbedtls_x509_crt_free( session->peer_cert );
5999 mbedtls_free( session->peer_cert );
6000 session->peer_cert = NULL;
6001 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006002#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006003 if( session->peer_cert_digest != NULL )
6004 {
6005 /* Zeroization is not necessary. */
6006 mbedtls_free( session->peer_cert_digest );
6007 session->peer_cert_digest = NULL;
6008 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6009 session->peer_cert_digest_len = 0;
6010 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006011#else
6012 ((void) session);
6013#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006014}
6015#endif /* MBEDTLS_X509_CRT_PARSE_C */
6016
Paul Bakker5121ce52009-01-03 21:22:43 +00006017/*
6018 * Handshake functions
6019 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006020#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006021/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006023{
Hanno Becker8759e162017-12-27 21:34:08 +00006024 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006027
Hanno Becker5097cba2019-02-05 13:36:46 +00006028 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006031 ssl->state++;
6032 return( 0 );
6033 }
6034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006037}
6038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040{
Hanno Becker8759e162017-12-27 21:34:08 +00006041 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006044
Hanno Becker5097cba2019-02-05 13:36:46 +00006045 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006048 ssl->state++;
6049 return( 0 );
6050 }
6051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006054}
Gilles Peskinef9828522017-05-03 12:28:43 +02006055
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006056#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006057/* Some certificate support -> implement write and parse */
6058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006062 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006064 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006067
Hanno Becker5097cba2019-02-05 13:36:46 +00006068 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006071 ssl->state++;
6072 return( 0 );
6073 }
6074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006076 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 {
6078 if( ssl->client_auth == 0 )
6079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081 ssl->state++;
6082 return( 0 );
6083 }
6084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006086 /*
6087 * If using SSLv3 and got no cert, send an Alert message
6088 * (otherwise an empty Certificate message will be sent).
6089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6091 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 {
6093 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6095 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6096 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 goto write_msg;
6100 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103#endif /* MBEDTLS_SSL_CLI_C */
6104#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006105 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6110 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 }
6112 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006113#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006116
6117 /*
6118 * 0 . 0 handshake type
6119 * 1 . 3 handshake length
6120 * 4 . 6 length of all certs
6121 * 7 . 9 length of cert. 1
6122 * 10 . n-1 peer certificate
6123 * n . n+2 length of cert. 2
6124 * n+3 . ... upper level cert, etc.
6125 */
6126 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006128
Paul Bakker29087132010-03-21 21:03:34 +00006129 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 {
6131 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006132 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006135 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137 }
6138
6139 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6140 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6141 ssl->out_msg[i + 2] = (unsigned char)( n );
6142
6143 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6144 i += n; crt = crt->next;
6145 }
6146
6147 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6148 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6149 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6150
6151 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6153 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006154
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006155#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006156write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006157#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006158
6159 ssl->state++;
6160
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006161 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006162 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006164 return( ret );
6165 }
6166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006168
Paul Bakkered27a042013-04-18 22:46:23 +02006169 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006170}
6171
Hanno Becker285ff0c2019-01-31 07:44:03 +00006172#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006173
6174#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006175static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6176 unsigned char *crt_buf,
6177 size_t crt_buf_len )
6178{
6179 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6180
6181 if( peer_crt == NULL )
6182 return( -1 );
6183
6184 if( peer_crt->raw.len != crt_buf_len )
6185 return( -1 );
6186
Hanno Becker68b856d2019-02-08 14:00:04 +00006187 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006188}
Hanno Becker5882dd02019-06-06 16:25:57 +01006189#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006190static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6191 unsigned char *crt_buf,
6192 size_t crt_buf_len )
6193{
6194 int ret;
6195 unsigned char const * const peer_cert_digest =
6196 ssl->session->peer_cert_digest;
6197 mbedtls_md_type_t const peer_cert_digest_type =
6198 ssl->session->peer_cert_digest_type;
6199 mbedtls_md_info_t const * const digest_info =
6200 mbedtls_md_info_from_type( peer_cert_digest_type );
6201 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6202 size_t digest_len;
6203
6204 if( peer_cert_digest == NULL || digest_info == NULL )
6205 return( -1 );
6206
6207 digest_len = mbedtls_md_get_size( digest_info );
6208 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6209 return( -1 );
6210
6211 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6212 if( ret != 0 )
6213 return( -1 );
6214
6215 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6216}
Hanno Becker5882dd02019-06-06 16:25:57 +01006217#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006218#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006219
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006220/*
6221 * Once the certificate message is read, parse it into a cert chain and
6222 * perform basic checks, but leave actual verification to the caller
6223 */
Hanno Becker35e41772019-02-05 15:37:23 +00006224static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6225 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006226{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006227 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006228#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6229 int crt_cnt=0;
6230#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006231 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006232 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006237 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6238 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006240 }
6241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6243 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006246 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6247 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006249 }
6250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006252
Paul Bakker5121ce52009-01-03 21:22:43 +00006253 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006256 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006257
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006258 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006262 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6263 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006265 }
6266
Hanno Becker33c3dc82019-01-30 14:46:46 +00006267 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6268 i += 3;
6269
Hanno Becker33c3dc82019-01-30 14:46:46 +00006270 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006271 while( i < ssl->in_hslen )
6272 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006273 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006274 if ( i + 3 > ssl->in_hslen ) {
6275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006276 mbedtls_ssl_send_alert_message( ssl,
6277 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6278 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006279 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6280 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006281 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6282 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006283 if( ssl->in_msg[i] != 0 )
6284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006286 mbedtls_ssl_send_alert_message( ssl,
6287 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6288 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006290 }
6291
Hanno Becker33c3dc82019-01-30 14:46:46 +00006292 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6294 | (unsigned int) ssl->in_msg[i + 2];
6295 i += 3;
6296
6297 if( n < 128 || i + n > ssl->in_hslen )
6298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006300 mbedtls_ssl_send_alert_message( ssl,
6301 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6302 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006304 }
6305
Hanno Becker33c3dc82019-01-30 14:46:46 +00006306 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006307#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6308 if( crt_cnt++ == 0 &&
6309 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6310 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006311 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006312 /* During client-side renegotiation, check that the server's
6313 * end-CRTs hasn't changed compared to the initial handshake,
6314 * mitigating the triple handshake attack. On success, reuse
6315 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6317 if( ssl_check_peer_crt_unchanged( ssl,
6318 &ssl->in_msg[i],
6319 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006320 {
Hanno Becker35e41772019-02-05 15:37:23 +00006321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6322 mbedtls_ssl_send_alert_message( ssl,
6323 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6324 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6325 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006326 }
Hanno Becker35e41772019-02-05 15:37:23 +00006327
6328 /* Now we can safely free the original chain. */
6329 ssl_clear_peer_cert( ssl->session );
6330 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006331#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6332
Hanno Becker33c3dc82019-01-30 14:46:46 +00006333 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006334#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006335 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006336#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006337 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006338 * it in-place from the input buffer instead of making a copy. */
6339 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6340#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006341 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006342 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006343 case 0: /*ok*/
6344 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6345 /* Ignore certificate with an unknown algorithm: maybe a
6346 prior certificate was already trusted. */
6347 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006348
Hanno Becker33c3dc82019-01-30 14:46:46 +00006349 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6350 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6351 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006352
Hanno Becker33c3dc82019-01-30 14:46:46 +00006353 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6354 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6355 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006356
Hanno Becker33c3dc82019-01-30 14:46:46 +00006357 default:
6358 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6359 crt_parse_der_failed:
6360 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6361 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6362 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006363 }
6364
6365 i += n;
6366 }
6367
Hanno Becker35e41772019-02-05 15:37:23 +00006368 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006369 return( 0 );
6370}
6371
Hanno Beckerb8a08572019-02-05 12:49:06 +00006372#if defined(MBEDTLS_SSL_SRV_C)
6373static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6374{
6375 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6376 return( -1 );
6377
6378#if defined(MBEDTLS_SSL_PROTO_SSL3)
6379 /*
6380 * Check if the client sent an empty certificate
6381 */
6382 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6383 {
6384 if( ssl->in_msglen == 2 &&
6385 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6386 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6387 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6388 {
6389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6390 return( 0 );
6391 }
6392
6393 return( -1 );
6394 }
6395#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6396
6397#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6398 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6399 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6400 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6401 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6402 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6403 {
6404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6405 return( 0 );
6406 }
6407
Hanno Beckerb8a08572019-02-05 12:49:06 +00006408#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6409 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01006410
6411 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00006412}
6413#endif /* MBEDTLS_SSL_SRV_C */
6414
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006415/* Check if a certificate message is expected.
6416 * Return either
6417 * - SSL_CERTIFICATE_EXPECTED, or
6418 * - SSL_CERTIFICATE_SKIP
6419 * indicating whether a Certificate message is expected or not.
6420 */
6421#define SSL_CERTIFICATE_EXPECTED 0
6422#define SSL_CERTIFICATE_SKIP 1
6423static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6424 int authmode )
6425{
6426 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6427 ssl->handshake->ciphersuite_info;
6428
6429 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6430 return( SSL_CERTIFICATE_SKIP );
6431
6432#if defined(MBEDTLS_SSL_SRV_C)
6433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6434 {
6435 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6436 return( SSL_CERTIFICATE_SKIP );
6437
6438 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6439 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006440 ssl->session_negotiate->verify_result =
6441 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6442 return( SSL_CERTIFICATE_SKIP );
6443 }
6444 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006445#else
6446 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006447#endif /* MBEDTLS_SSL_SRV_C */
6448
6449 return( SSL_CERTIFICATE_EXPECTED );
6450}
6451
Hanno Becker3cf50612019-02-05 14:36:34 +00006452static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6453 int authmode,
6454 mbedtls_x509_crt *chain,
6455 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006456{
Hanno Becker613d4902019-02-05 13:11:17 +00006457 int ret = 0;
Hanno Becker8c13ee62019-02-26 16:48:17 +00006458 int verify_ret;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006459 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6460 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006461 mbedtls_x509_crt *ca_chain;
6462 mbedtls_x509_crl *ca_crl;
6463
6464 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6465 return( 0 );
6466
6467#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6468 if( ssl->handshake->sni_ca_chain != NULL )
6469 {
6470 ca_chain = ssl->handshake->sni_ca_chain;
6471 ca_crl = ssl->handshake->sni_ca_crl;
6472 }
6473 else
6474#endif
6475 {
6476 ca_chain = ssl->conf->ca_chain;
6477 ca_crl = ssl->conf->ca_crl;
6478 }
6479
6480 /*
6481 * Main check: verify certificate
6482 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00006483 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00006484 chain,
6485 ca_chain, ca_crl,
6486 ssl->conf->cert_profile,
6487 ssl->hostname,
6488 &ssl->session_negotiate->verify_result,
6489 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6490
Hanno Becker8c13ee62019-02-26 16:48:17 +00006491 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00006492 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00006493 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00006494 }
6495
6496#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00006497 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00006498 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6499#endif
6500
6501 /*
6502 * Secondary checks: always done, but change 'ret' only if it was 0
6503 */
6504
6505#if defined(MBEDTLS_ECP_C)
6506 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00006507 mbedtls_pk_context *pk;
6508 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
6509 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01006510 {
6511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00006512 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01006513 }
Hanno Becker3cf50612019-02-05 14:36:34 +00006514
6515 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00006516 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
6517 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
6518
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00006519 mbedtls_x509_crt_pk_release( chain );
Hanno Becker8c13ee62019-02-26 16:48:17 +00006520
6521 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00006522 {
6523 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6524
6525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00006526 if( verify_ret == 0 )
6527 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00006528 }
6529 }
6530#endif /* MBEDTLS_ECP_C */
6531
Hanno Becker8c13ee62019-02-26 16:48:17 +00006532 ret = mbedtls_ssl_check_cert_usage( chain,
6533 ciphersuite_info,
6534 ! ssl->conf->endpoint,
6535 &ssl->session_negotiate->verify_result );
6536 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00006537 {
6538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00006539 if( verify_ret == 0 )
6540 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00006541 }
6542
6543 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6544 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6545 * with details encoded in the verification flags. All other kinds
6546 * of error codes, including those from the user provided f_vrfy
6547 * functions, are treated as fatal and lead to a failure of
6548 * ssl_parse_certificate even if verification was optional. */
6549 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00006550 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6551 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00006552 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00006553 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00006554 }
6555
6556 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6557 {
6558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00006559 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00006560 }
6561
Hanno Becker8c13ee62019-02-26 16:48:17 +00006562 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00006563 {
6564 uint8_t alert;
6565
6566 /* The certificate may have been rejected for several reasons.
6567 Pick one and send the corresponding alert. Which alert to send
6568 may be a subject of debate in some cases. */
6569 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6570 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6571 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6572 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6573 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6574 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6575 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6576 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6577 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6578 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6579 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6580 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6581 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6582 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6583 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6584 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6585 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6586 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6587 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6588 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6589 else
6590 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6591 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6592 alert );
6593 }
6594
6595#if defined(MBEDTLS_DEBUG_C)
6596 if( ssl->session_negotiate->verify_result != 0 )
6597 {
6598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6599 ssl->session_negotiate->verify_result ) );
6600 }
6601 else
6602 {
6603 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6604 }
6605#endif /* MBEDTLS_DEBUG_C */
6606
Hanno Becker8c13ee62019-02-26 16:48:17 +00006607 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00006608}
6609
Hanno Becker34106f62019-02-08 14:59:05 +00006610#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01006611
6612#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006613static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6614 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006615{
6616 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00006617 /* Remember digest of the peer's end-CRT. */
6618 ssl->session_negotiate->peer_cert_digest =
6619 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6620 if( ssl->session_negotiate->peer_cert_digest == NULL )
6621 {
6622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6623 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6624 mbedtls_ssl_send_alert_message( ssl,
6625 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6626 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6627
6628 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6629 }
6630
6631 ret = mbedtls_md( mbedtls_md_info_from_type(
6632 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6633 start, len,
6634 ssl->session_negotiate->peer_cert_digest );
6635
6636 ssl->session_negotiate->peer_cert_digest_type =
6637 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6638 ssl->session_negotiate->peer_cert_digest_len =
6639 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6640
6641 return( ret );
6642}
Hanno Becker5882dd02019-06-06 16:25:57 +01006643#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006644
6645static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6646 unsigned char *start, size_t len )
6647{
6648 unsigned char *end = start + len;
6649 int ret;
6650
6651 /* Make a copy of the peer's raw public key. */
6652 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6653 ret = mbedtls_pk_parse_subpubkey( &start, end,
6654 &ssl->handshake->peer_pubkey );
6655 if( ret != 0 )
6656 {
6657 /* We should have parsed the public key before. */
6658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6659 }
6660
6661 return( 0 );
6662}
6663#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6664
Hanno Becker3cf50612019-02-05 14:36:34 +00006665int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6666{
6667 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006668 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006669#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6670 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6671 ? ssl->handshake->sni_authmode
6672 : ssl->conf->authmode;
6673#else
6674 const int authmode = ssl->conf->authmode;
6675#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006676 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006677 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006678
6679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6680
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006681 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6682 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006683 {
6684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006685 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006686 }
6687
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006688#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6689 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006690 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006691 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006692 chain = ssl->handshake->ecrs_peer_cert;
6693 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006694 goto crt_verify;
6695 }
6696#endif
6697
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006698 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006699 {
6700 /* mbedtls_ssl_read_record may have sent an alert already. We
6701 let it decide whether to alert. */
6702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006703 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006704 }
6705
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006706#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00006707 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6708 {
6709 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006710
Hanno Beckerb8a08572019-02-05 12:49:06 +00006711 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006712 ret = 0;
6713 else
6714 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006715
Hanno Becker613d4902019-02-05 13:11:17 +00006716 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006717 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00006718#endif /* MBEDTLS_SSL_SRV_C */
6719
Hanno Becker35e41772019-02-05 15:37:23 +00006720 /* Clear existing peer CRT structure in case we tried to
6721 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006722 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006723
6724 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6725 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006726 {
6727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6728 sizeof( mbedtls_x509_crt ) ) );
6729 mbedtls_ssl_send_alert_message( ssl,
6730 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6731 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006732
Hanno Beckere4aeb762019-02-05 17:19:52 +00006733 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6734 goto exit;
6735 }
6736 mbedtls_x509_crt_init( chain );
6737
6738 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006739 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006740 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006741
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006742#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6743 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006744 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006745
6746crt_verify:
6747 if( ssl->handshake->ecrs_enabled)
6748 rs_ctx = &ssl->handshake->ecrs_ctx;
6749#endif
6750
Hanno Becker3cf50612019-02-05 14:36:34 +00006751 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006752 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006753 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006754 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006755
Hanno Becker3008d282019-02-05 17:02:28 +00006756#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00006757 {
Hanno Becker5882dd02019-06-06 16:25:57 +01006758 size_t pk_len;
6759 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00006760
Hanno Becker34106f62019-02-08 14:59:05 +00006761 /* We parse the CRT chain without copying, so
6762 * these pointers point into the input buffer,
6763 * and are hence still valid after freeing the
6764 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006765
Hanno Becker5882dd02019-06-06 16:25:57 +01006766#if defined(MBEDTLS_SSL_RENEGOTIATION)
6767 unsigned char *crt_start;
6768 size_t crt_len;
6769
Hanno Becker34106f62019-02-08 14:59:05 +00006770 crt_start = chain->raw.p;
6771 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01006772#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006773
Hanno Becker8c13ee62019-02-26 16:48:17 +00006774 pk_start = chain->cache->pk_raw.p;
6775 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00006776
6777 /* Free the CRT structures before computing
6778 * digest and copying the peer's public key. */
6779 mbedtls_x509_crt_free( chain );
6780 mbedtls_free( chain );
6781 chain = NULL;
6782
Hanno Becker5882dd02019-06-06 16:25:57 +01006783#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006784 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006786 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01006787#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006788
Hanno Becker34106f62019-02-08 14:59:05 +00006789 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006790 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00006791 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006792 }
Hanno Becker34106f62019-02-08 14:59:05 +00006793#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6794 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006795 ssl->session_negotiate->peer_cert = chain;
6796 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006797#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800
Hanno Becker613d4902019-02-05 13:11:17 +00006801exit:
6802
Hanno Beckere4aeb762019-02-05 17:19:52 +00006803 if( ret == 0 )
6804 ssl->state++;
6805
6806#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6807 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6808 {
6809 ssl->handshake->ecrs_peer_cert = chain;
6810 chain = NULL;
6811 }
6812#endif
6813
6814 if( chain != NULL )
6815 {
6816 mbedtls_x509_crt_free( chain );
6817 mbedtls_free( chain );
6818 }
6819
Paul Bakker5121ce52009-01-03 21:22:43 +00006820 return( ret );
6821}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006822#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006825{
6826 int ret;
6827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006831 ssl->out_msglen = 1;
6832 ssl->out_msg[0] = 1;
6833
Paul Bakker5121ce52009-01-03 21:22:43 +00006834 ssl->state++;
6835
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006836 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006837 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006839 return( ret );
6840 }
6841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006843
6844 return( 0 );
6845}
6846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006848{
6849 int ret;
6850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006852
Hanno Becker327c93b2018-08-15 13:56:18 +01006853 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856 return( ret );
6857 }
6858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006862 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6863 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006865 }
6866
Hanno Beckere678eaa2018-08-21 14:57:46 +01006867 /* CCS records are only accepted if they have length 1 and content '1',
6868 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006869
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006870 /*
6871 * Switch to our negotiated transform and session parameters for inbound
6872 * data.
6873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006875 ssl->transform_in = ssl->transform_negotiate;
6876 ssl->session_in = ssl->session_negotiate;
6877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006879 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006882 ssl_dtls_replay_reset( ssl );
6883#endif
6884
6885 /* Increment epoch */
6886 if( ++ssl->in_epoch == 0 )
6887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006889 /* This is highly unlikely to happen for legitimate reasons, so
6890 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006892 }
6893 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006894 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006896#if defined(MBEDTLS_SSL_PROTO_TLS)
6897 {
6898 memset( ssl->in_ctr, 0, 8 );
6899 }
6900#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006901
Hanno Beckerf5970a02019-05-08 09:38:41 +01006902 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6905 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006910 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6911 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006913 }
6914 }
6915#endif
6916
Paul Bakker5121ce52009-01-03 21:22:43 +00006917 ssl->state++;
6918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006920
6921 return( 0 );
6922}
6923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6925 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006926{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006927 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6930 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6931 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006932 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006933 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6936#if defined(MBEDTLS_SHA512_C)
6937 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006938 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6939 else
6940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#if defined(MBEDTLS_SHA256_C)
6942 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006943 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006944 else
6945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006949 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006950 }
Paul Bakker380da532012-04-18 16:10:25 +00006951}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006954{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6956 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006957 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6958 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6961#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006962 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006963#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006965 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006968}
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006971 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6974 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006975 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6976 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6979#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006980 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006981#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006983 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006986}
6987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6989 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6990static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006991 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006992{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006993 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6994 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006995}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006996#endif
Paul Bakker380da532012-04-18 16:10:25 +00006997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6999#if defined(MBEDTLS_SHA256_C)
7000static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007001 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007002{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007003 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007004}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007005#endif
Paul Bakker380da532012-04-18 16:10:25 +00007006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007#if defined(MBEDTLS_SHA512_C)
7008static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007009 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007010{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007011 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007012}
Paul Bakker769075d2012-11-24 11:26:46 +01007013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007017static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007019{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007020 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007021 mbedtls_md5_context md5;
7022 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007023
Paul Bakker5121ce52009-01-03 21:22:43 +00007024 unsigned char padbuf[48];
7025 unsigned char md5sum[16];
7026 unsigned char sha1sum[20];
7027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007029 if( !session )
7030 session = ssl->session;
7031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007033
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007034 mbedtls_md5_init( &md5 );
7035 mbedtls_sha1_init( &sha1 );
7036
7037 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7038 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007039
7040 /*
7041 * SSLv3:
7042 * hash =
7043 * MD5( master + pad2 +
7044 * MD5( handshake + sender + master + pad1 ) )
7045 * + SHA1( master + pad2 +
7046 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007047 */
7048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007050 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7051 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007055 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7056 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007060 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007061
Paul Bakker1ef83d62012-04-11 12:09:53 +00007062 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007063
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007064 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7065 mbedtls_md5_update_ret( &md5, session->master, 48 );
7066 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7067 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007068
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007069 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7070 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7071 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7072 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007073
Paul Bakker1ef83d62012-04-11 12:09:53 +00007074 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007075
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007076 mbedtls_md5_starts_ret( &md5 );
7077 mbedtls_md5_update_ret( &md5, session->master, 48 );
7078 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7079 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7080 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007082 mbedtls_sha1_starts_ret( &sha1 );
7083 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7084 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7085 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7086 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007089
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007090 mbedtls_md5_free( &md5 );
7091 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007093 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7094 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7095 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007102static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007104{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007105 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007106 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007107 mbedtls_md5_context md5;
7108 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007109 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007112 if( !session )
7113 session = ssl->session;
7114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007116
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007117 mbedtls_md5_init( &md5 );
7118 mbedtls_sha1_init( &sha1 );
7119
7120 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7121 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007122
Paul Bakker1ef83d62012-04-11 12:09:53 +00007123 /*
7124 * TLSv1:
7125 * hash = PRF( master, finished_label,
7126 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7127 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007130 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7131 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007132#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007135 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7136 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007137#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007140 ? "client finished"
7141 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007142
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007143 mbedtls_md5_finish_ret( &md5, padbuf );
7144 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007145
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007146 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007147 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007150
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007151 mbedtls_md5_free( &md5 );
7152 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007153
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007154 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7161#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007162static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007164{
7165 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007166 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007167 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007168 unsigned char padbuf[32];
7169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007171 if( !session )
7172 session = ssl->session;
7173
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007174 mbedtls_sha256_init( &sha256 );
7175
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007177
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007178 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007179
7180 /*
7181 * TLSv1.2:
7182 * hash = PRF( master, finished_label,
7183 * Hash( handshake ) )[0.11]
7184 */
7185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186#if !defined(MBEDTLS_SHA256_ALT)
7187 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007188 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007189#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007192 ? "client finished"
7193 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007194
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007195 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007196
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007197 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007198 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007201
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007202 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007203
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007204 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007207}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007211static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007213{
7214 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007215 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007216 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007217 unsigned char padbuf[48];
7218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007220 if( !session )
7221 session = ssl->session;
7222
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007223 mbedtls_sha512_init( &sha512 );
7224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007226
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007227 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007228
7229 /*
7230 * TLSv1.2:
7231 * hash = PRF( master, finished_label,
7232 * Hash( handshake ) )[0.11]
7233 */
7234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007236 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7237 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007238#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007241 ? "client finished"
7242 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007243
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007244 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007245
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007246 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007247 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007250
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007251 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007252
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007253 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257#endif /* MBEDTLS_SHA512_C */
7258#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007263
7264 /*
7265 * Free our handshake params
7266 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007267 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007269 ssl->handshake = NULL;
7270
7271 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007272 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007273 */
7274 if( ssl->transform )
7275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 mbedtls_ssl_transform_free( ssl->transform );
7277 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007278 }
7279 ssl->transform = ssl->transform_negotiate;
7280 ssl->transform_negotiate = NULL;
7281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007283}
7284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007286{
7287 int resume = ssl->handshake->resume;
7288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291#if defined(MBEDTLS_SSL_RENEGOTIATION)
7292 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007295 ssl->renego_records_seen = 0;
7296 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007297#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007298
7299 /*
7300 * Free the previous session and switch in the current one
7301 */
Paul Bakker0a597072012-09-25 21:55:46 +00007302 if( ssl->session )
7303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007305 /* RFC 7366 3.1: keep the EtM state */
7306 ssl->session_negotiate->encrypt_then_mac =
7307 ssl->session->encrypt_then_mac;
7308#endif
7309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 mbedtls_ssl_session_free( ssl->session );
7311 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007312 }
7313 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007314 ssl->session_negotiate = NULL;
7315
Paul Bakker0a597072012-09-25 21:55:46 +00007316 /*
7317 * Add cache entry
7318 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007319 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007320 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007321 resume == 0 )
7322 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007323 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007325 }
Paul Bakker0a597072012-09-25 21:55:46 +00007326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007328 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007329 ssl->handshake->flight != NULL )
7330 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007331 /* Cancel handshake timer */
7332 ssl_set_timer( ssl, 0 );
7333
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007334 /* Keep last flight around in case we need to resend it:
7335 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007337 }
7338 else
7339#endif
7340 ssl_handshake_wrapup_free_hs_transform( ssl );
7341
Paul Bakker48916f92012-09-16 19:57:18 +00007342 ssl->state++;
7343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007345}
7346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007348{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007349 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007352
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007353 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007354
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007355 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007356
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007357 /*
7358 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7359 * may define some other value. Currently (early 2016), no defined
7360 * ciphersuite does this (and this is unlikely to change as activity has
7361 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007366 ssl->verify_data_len = hash_len;
7367 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007368#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007369
Paul Bakker5121ce52009-01-03 21:22:43 +00007370 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7372 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007373
7374 /*
7375 * In case of session resuming, invert the client and server
7376 * ChangeCipherSpec messages order.
7377 */
Paul Bakker0a597072012-09-25 21:55:46 +00007378 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007381 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007385 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007387#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007388 }
7389 else
7390 ssl->state++;
7391
Paul Bakker48916f92012-09-16 19:57:18 +00007392 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007393 * Switch to our negotiated transform and session parameters for outbound
7394 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007399 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007400 {
7401 unsigned char i;
7402
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007403 /* Remember current epoch settings for resending */
7404 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007405 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007406
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007407 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007408 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007409
7410 /* Increment epoch */
7411 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007412 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007413 break;
7414
7415 /* The loop goes to its end iff the counter is wrapping */
7416 if( i == 0 )
7417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7419 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007420 }
7421 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007422 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007424#if defined(MBEDTLS_SSL_PROTO_TLS)
7425 {
7426 memset( ssl->cur_out_ctr, 0, 8 );
7427 }
7428#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007429
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007430 ssl->transform_out = ssl->transform_negotiate;
7431 ssl->session_out = ssl->session_negotiate;
7432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7434 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7439 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007440 }
7441 }
7442#endif
7443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007445 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007447#endif
7448
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007449 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007450 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007452 return( ret );
7453 }
7454
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007456 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007457 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7458 {
7459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7460 return( ret );
7461 }
7462#endif
7463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007465
7466 return( 0 );
7467}
7468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007470#define SSL_MAX_HASH_LEN 36
7471#else
7472#define SSL_MAX_HASH_LEN 12
7473#endif
7474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007476{
Paul Bakker23986e52011-04-24 08:57:21 +00007477 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007478 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007479 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007482
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007483 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007484
Hanno Becker327c93b2018-08-15 13:56:18 +01007485 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007488 return( ret );
7489 }
7490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497 }
7498
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007499 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500#if defined(MBEDTLS_SSL_PROTO_SSL3)
7501 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007502 hash_len = 36;
7503 else
7504#endif
7505 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7508 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007511 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7512 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007514 }
7515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007517 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007520 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7521 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007523 }
7524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007526 ssl->verify_data_len = hash_len;
7527 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007528#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007529
Paul Bakker0a597072012-09-25 21:55:46 +00007530 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007533 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007537 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007539#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007540 }
7541 else
7542 ssl->state++;
7543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007545 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007547#endif
7548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
7551 return( 0 );
7552}
7553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7559 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7560 mbedtls_md5_init( &handshake->fin_md5 );
7561 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007562 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7563 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7566#if defined(MBEDTLS_SHA256_C)
7567 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007568 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570#if defined(MBEDTLS_SHA512_C)
7571 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007572 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007573#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007575
7576 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007577
7578#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7579 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7580 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7581#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if defined(MBEDTLS_DHM_C)
7584 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#if defined(MBEDTLS_ECDH_C)
7587 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007588#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007589#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007590 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007591#if defined(MBEDTLS_SSL_CLI_C)
7592 handshake->ecjpake_cache = NULL;
7593 handshake->ecjpake_cache_len = 0;
7594#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007595#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007596
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007597#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007598 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007599#endif
7600
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007601#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7602 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7603#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007604
7605#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7606 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7607 mbedtls_pk_init( &handshake->peer_pubkey );
7608#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007609}
7610
Hanno Becker611a83b2018-01-03 14:27:32 +00007611void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007612{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7616 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007617
Hanno Becker92231322018-01-03 15:32:51 +00007618#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 mbedtls_md_init( &transform->md_ctx_enc );
7620 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007621#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007622}
7623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007627}
7628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007630{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007631 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007632 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007634 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007636 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007637 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007638
7639 /*
7640 * Either the pointers are now NULL or cleared properly and can be freed.
7641 * Now allocate missing structures.
7642 */
7643 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007644 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007645 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007646 }
Paul Bakker48916f92012-09-16 19:57:18 +00007647
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007648 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007649 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007650 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007651 }
Paul Bakker48916f92012-09-16 19:57:18 +00007652
Paul Bakker82788fb2014-10-20 13:59:19 +02007653 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007654 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007655 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007656 }
Paul Bakker48916f92012-09-16 19:57:18 +00007657
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007658 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007659 if( ssl->handshake == NULL ||
7660 ssl->transform_negotiate == NULL ||
7661 ssl->session_negotiate == NULL )
7662 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665 mbedtls_free( ssl->handshake );
7666 mbedtls_free( ssl->transform_negotiate );
7667 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007668
7669 ssl->handshake = NULL;
7670 ssl->transform_negotiate = NULL;
7671 ssl->session_negotiate = NULL;
7672
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007673 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007674 }
7675
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007676 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007678 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007679 ssl_handshake_params_init( ssl->handshake );
7680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007682 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007683 {
7684 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007685
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7687 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7688 else
7689 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007690
7691 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007692 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007693#endif
7694
Paul Bakker48916f92012-09-16 19:57:18 +00007695 return( 0 );
7696}
7697
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007698#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007699/* Dummy cookie callbacks for defaults */
7700static int ssl_cookie_write_dummy( void *ctx,
7701 unsigned char **p, unsigned char *end,
7702 const unsigned char *cli_id, size_t cli_id_len )
7703{
7704 ((void) ctx);
7705 ((void) p);
7706 ((void) end);
7707 ((void) cli_id);
7708 ((void) cli_id_len);
7709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007711}
7712
7713static int ssl_cookie_check_dummy( void *ctx,
7714 const unsigned char *cookie, size_t cookie_len,
7715 const unsigned char *cli_id, size_t cli_id_len )
7716{
7717 ((void) ctx);
7718 ((void) cookie);
7719 ((void) cookie_len);
7720 ((void) cli_id);
7721 ((void) cli_id_len);
7722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007724}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007725#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007726
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007727/* Once ssl->out_hdr as the address of the beginning of the
7728 * next outgoing record is set, deduce the other pointers.
7729 *
7730 * Note: For TLS, we save the implicit record sequence number
7731 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7732 * and the caller has to make sure there's space for this.
7733 */
7734
7735static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7736 mbedtls_ssl_transform *transform )
7737{
7738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007739 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007740 {
7741 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007742#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007743 ssl->out_cid = ssl->out_ctr + 8;
7744 ssl->out_len = ssl->out_cid;
7745 if( transform != NULL )
7746 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007747#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007748 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007749#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007750 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007751 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007752 MBEDTLS_SSL_TRANSPORT_ELSE
7753#endif /* MBEDTLS_SSL_PROTO_DTLS */
7754#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007755 {
7756 ssl->out_ctr = ssl->out_hdr - 8;
7757 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007758#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007759 ssl->out_cid = ssl->out_len;
7760#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007761 ssl->out_iv = ssl->out_hdr + 5;
7762 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007763#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007764
7765 /* Adjust out_msg to make space for explicit IV, if used. */
7766 if( transform != NULL &&
7767 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7768 {
7769 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7770 }
7771 else
7772 ssl->out_msg = ssl->out_iv;
7773}
7774
7775/* Once ssl->in_hdr as the address of the beginning of the
7776 * next incoming record is set, deduce the other pointers.
7777 *
7778 * Note: For TLS, we save the implicit record sequence number
7779 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7780 * and the caller has to make sure there's space for this.
7781 */
7782
Hanno Beckerf5970a02019-05-08 09:38:41 +01007783static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007784{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007785 /* This function sets the pointers to match the case
7786 * of unprotected TLS/DTLS records, with both ssl->in_iv
7787 * and ssl->in_msg pointing to the beginning of the record
7788 * content.
7789 *
7790 * When decrypting a protected record, ssl->in_msg
7791 * will be shifted to point to the beginning of the
7792 * record plaintext.
7793 */
7794
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007795#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007796 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007797 {
Hanno Becker70e79282019-05-03 14:34:53 +01007798 /* This sets the header pointers to match records
7799 * without CID. When we receive a record containing
7800 * a CID, the fields are shifted accordingly in
7801 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007802 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007803#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007804 ssl->in_cid = ssl->in_ctr + 8;
7805 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007806#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007807 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007808#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007809 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007810 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007811 MBEDTLS_SSL_TRANSPORT_ELSE
7812#endif /* MBEDTLS_SSL_PROTO_DTLS */
7813#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007814 {
7815 ssl->in_ctr = ssl->in_hdr - 8;
7816 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007817#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007818 ssl->in_cid = ssl->in_len;
7819#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007820 ssl->in_iv = ssl->in_hdr + 5;
7821 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007822#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007823
Hanno Beckerf5970a02019-05-08 09:38:41 +01007824 /* This will be adjusted at record decryption time. */
7825 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007826}
7827
Paul Bakker5121ce52009-01-03 21:22:43 +00007828/*
7829 * Initialize an SSL context
7830 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007831void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7832{
7833 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7834}
7835
7836/*
7837 * Setup an SSL context
7838 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007839
7840static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7841{
7842 /* Set the incoming and outgoing record pointers. */
7843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007844 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007845 {
7846 ssl->out_hdr = ssl->out_buf;
7847 ssl->in_hdr = ssl->in_buf;
7848 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007849 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007850#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007851#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007852 {
7853 ssl->out_hdr = ssl->out_buf + 8;
7854 ssl->in_hdr = ssl->in_buf + 8;
7855 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007856#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007857
7858 /* Derive other internal pointers. */
7859 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007860 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007861}
7862
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007863int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007864 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007865{
Paul Bakker48916f92012-09-16 19:57:18 +00007866 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007867
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007868 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007869
7870 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007871 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007872 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007873
7874 /* Set to NULL in case of an error condition */
7875 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007876
Angus Grattond8213d02016-05-25 20:56:48 +10007877 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7878 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007879 {
Angus Grattond8213d02016-05-25 20:56:48 +10007880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007881 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007882 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007883 }
7884
7885 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7886 if( ssl->out_buf == NULL )
7887 {
7888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007889 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007890 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007891 }
7892
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007893 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007894
Paul Bakker48916f92012-09-16 19:57:18 +00007895 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007896 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007897
7898 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007899
7900error:
7901 mbedtls_free( ssl->in_buf );
7902 mbedtls_free( ssl->out_buf );
7903
7904 ssl->conf = NULL;
7905
7906 ssl->in_buf = NULL;
7907 ssl->out_buf = NULL;
7908
7909 ssl->in_hdr = NULL;
7910 ssl->in_ctr = NULL;
7911 ssl->in_len = NULL;
7912 ssl->in_iv = NULL;
7913 ssl->in_msg = NULL;
7914
7915 ssl->out_hdr = NULL;
7916 ssl->out_ctr = NULL;
7917 ssl->out_len = NULL;
7918 ssl->out_iv = NULL;
7919 ssl->out_msg = NULL;
7920
k-stachowiak9f7798e2018-07-31 16:52:32 +02007921 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007922}
7923
7924/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007925 * Reset an initialized and used SSL context for re-use while retaining
7926 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007927 *
7928 * If partial is non-zero, keep data in the input buffer and client ID.
7929 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007930 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007931static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007932{
Paul Bakker48916f92012-09-16 19:57:18 +00007933 int ret;
7934
Hanno Becker7e772132018-08-10 12:38:21 +01007935#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7936 !defined(MBEDTLS_SSL_SRV_C)
7937 ((void) partial);
7938#endif
7939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007941
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007942 /* Cancel any possibly running timer */
7943 ssl_set_timer( ssl, 0 );
7944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#if defined(MBEDTLS_SSL_RENEGOTIATION)
7946 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007947 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007948
7949 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7951 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007954
Paul Bakker7eb013f2011-10-06 12:37:39 +00007955 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007956 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007957
7958 ssl->in_msgtype = 0;
7959 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007961 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007962 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007963#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007965 ssl_dtls_replay_reset( ssl );
7966#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007967
7968 ssl->in_hslen = 0;
7969 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007970
7971 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007972
7973 ssl->out_msgtype = 0;
7974 ssl->out_msglen = 0;
7975 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7977 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007978 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007979#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007980
Hanno Becker19859472018-08-06 09:40:20 +01007981 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7982
Paul Bakker48916f92012-09-16 19:57:18 +00007983 ssl->transform_in = NULL;
7984 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007985
Hanno Becker78640902018-08-13 16:35:15 +01007986 ssl->session_in = NULL;
7987 ssl->session_out = NULL;
7988
Angus Grattond8213d02016-05-25 20:56:48 +10007989 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007990
7991#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007992 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007993#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7994 {
7995 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007996 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007997 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8000 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8003 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8006 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008007 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008008 }
8009#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008010
Paul Bakker48916f92012-09-16 19:57:18 +00008011 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 mbedtls_ssl_transform_free( ssl->transform );
8014 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008015 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008016 }
Paul Bakker48916f92012-09-16 19:57:18 +00008017
Paul Bakkerc0463502013-02-14 11:19:38 +01008018 if( ssl->session )
8019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020 mbedtls_ssl_session_free( ssl->session );
8021 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008022 ssl->session = NULL;
8023 }
8024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008026 ssl->alpn_chosen = NULL;
8027#endif
8028
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008029#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008030#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008031 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008032#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008033 {
8034 mbedtls_free( ssl->cli_id );
8035 ssl->cli_id = NULL;
8036 ssl->cli_id_len = 0;
8037 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008038#endif
8039
Paul Bakker48916f92012-09-16 19:57:18 +00008040 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8041 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008042
8043 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008044}
8045
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008046/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008047 * Reset an initialized and used SSL context for re-use while retaining
8048 * all application-set variables, function pointers and data.
8049 */
8050int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8051{
8052 return( ssl_session_reset_int( ssl, 0 ) );
8053}
8054
8055/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008056 * SSL set accessors
8057 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008058void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008060 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008061}
8062
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008063void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008064{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008065 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008066}
8067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008069void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008070{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008071 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008072}
8073#endif
8074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008076void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008077{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008078 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008079}
8080#endif
8081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008083
Hanno Becker1841b0a2018-08-24 11:13:57 +01008084void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8085 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008086{
8087 ssl->disable_datagram_packing = !allow_packing;
8088}
8089
8090void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8091 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008092{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008093 conf->hs_timeout_min = min;
8094 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008095}
8096#endif
8097
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008098void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008099{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008100 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008101}
8102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008104void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008105 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008106 void *p_vrfy )
8107{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008108 conf->f_vrfy = f_vrfy;
8109 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008112
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008113void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008114 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008115 void *p_rng )
8116{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008117 conf->f_rng = f_rng;
8118 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008119}
8120
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008121void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008122 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008123 void *p_dbg )
8124{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008125 conf->f_dbg = f_dbg;
8126 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008127}
8128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008130 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008131 mbedtls_ssl_send_t *f_send,
8132 mbedtls_ssl_recv_t *f_recv,
8133 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008134{
8135 ssl->p_bio = p_bio;
8136 ssl->f_send = f_send;
8137 ssl->f_recv = f_recv;
8138 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008139}
8140
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008141#if defined(MBEDTLS_SSL_PROTO_DTLS)
8142void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8143{
8144 ssl->mtu = mtu;
8145}
8146#endif
8147
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008148void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008149{
8150 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008151}
8152
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008153void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8154 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008155 mbedtls_ssl_set_timer_t *f_set_timer,
8156 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008157{
8158 ssl->p_timer = p_timer;
8159 ssl->f_set_timer = f_set_timer;
8160 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008161
8162 /* Make sure we start with no timer running */
8163 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008164}
8165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008167void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008168 void *p_cache,
8169 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8170 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008171{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008172 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008173 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008174 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178#if defined(MBEDTLS_SSL_CLI_C)
8179int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008180{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008181 int ret;
8182
8183 if( ssl == NULL ||
8184 session == NULL ||
8185 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008186 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008189 }
8190
Hanno Becker58fccf22019-02-06 14:30:46 +00008191 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8192 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008193 return( ret );
8194
Paul Bakker0a597072012-09-25 21:55:46 +00008195 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008196
8197 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008200
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008201void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008202 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008203{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8205 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8206 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8207 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008208}
8209
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008210void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008211 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008212 int major, int minor )
8213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008215 return;
8216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008218 return;
8219
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008220 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008221}
8222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008224void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008225 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008226{
8227 conf->cert_profile = profile;
8228}
8229
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008230/* Append a new keycert entry to a (possibly empty) list */
8231static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8232 mbedtls_x509_crt *cert,
8233 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008234{
niisato8ee24222018-06-25 19:05:48 +09008235 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008236
niisato8ee24222018-06-25 19:05:48 +09008237 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8238 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008239 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008240
niisato8ee24222018-06-25 19:05:48 +09008241 new_cert->cert = cert;
8242 new_cert->key = key;
8243 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008244
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008245 /* Update head is the list was null, else add to the end */
8246 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008247 {
niisato8ee24222018-06-25 19:05:48 +09008248 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008249 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008250 else
8251 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008252 mbedtls_ssl_key_cert *cur = *head;
8253 while( cur->next != NULL )
8254 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008255 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008256 }
8257
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008258 return( 0 );
8259}
8260
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008261int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008262 mbedtls_x509_crt *own_cert,
8263 mbedtls_pk_context *pk_key )
8264{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008265 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008266}
8267
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008268void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008269 mbedtls_x509_crt *ca_chain,
8270 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008271{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008272 conf->ca_chain = ca_chain;
8273 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008274}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008276
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008277#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8278int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8279 mbedtls_x509_crt *own_cert,
8280 mbedtls_pk_context *pk_key )
8281{
8282 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8283 own_cert, pk_key ) );
8284}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008285
8286void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8287 mbedtls_x509_crt *ca_chain,
8288 mbedtls_x509_crl *ca_crl )
8289{
8290 ssl->handshake->sni_ca_chain = ca_chain;
8291 ssl->handshake->sni_ca_crl = ca_crl;
8292}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008293
8294void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8295 int authmode )
8296{
8297 ssl->handshake->sni_authmode = authmode;
8298}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008299#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8300
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008301#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008302/*
8303 * Set EC J-PAKE password for current handshake
8304 */
8305int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8306 const unsigned char *pw,
8307 size_t pw_len )
8308{
8309 mbedtls_ecjpake_role role;
8310
Janos Follath8eb64132016-06-03 15:40:57 +01008311 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8313
8314 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8315 role = MBEDTLS_ECJPAKE_SERVER;
8316 else
8317 role = MBEDTLS_ECJPAKE_CLIENT;
8318
8319 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8320 role,
8321 MBEDTLS_MD_SHA256,
8322 MBEDTLS_ECP_DP_SECP256R1,
8323 pw, pw_len ) );
8324}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008325#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008328int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008329 const unsigned char *psk, size_t psk_len,
8330 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008331{
Paul Bakker6db455e2013-09-18 17:29:31 +02008332 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008337
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008338 /* Identity len will be encoded on two bytes */
8339 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008340 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008341 {
8342 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8343 }
8344
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008345 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008346 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008347 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008348
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008349 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008350 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008351 conf->psk_len = 0;
8352 }
8353 if( conf->psk_identity != NULL )
8354 {
8355 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008356 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008357 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008358 }
8359
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008360 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8361 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008362 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008363 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008364 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008365 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008366 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008367 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008368 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008369
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008370 conf->psk_len = psk_len;
8371 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008372
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008373 memcpy( conf->psk, psk, conf->psk_len );
8374 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008375
8376 return( 0 );
8377}
8378
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008379int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8380 const unsigned char *psk, size_t psk_len )
8381{
8382 if( psk == NULL || ssl->handshake == NULL )
8383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8384
8385 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8387
8388 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008389 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008390 mbedtls_platform_zeroize( ssl->handshake->psk,
8391 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008392 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008393 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008394 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008395
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008396 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008397 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008398
8399 ssl->handshake->psk_len = psk_len;
8400 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8401
8402 return( 0 );
8403}
8404
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008405void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008407 size_t),
8408 void *p_psk )
8409{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008410 conf->f_psk = f_psk;
8411 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008414
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008415#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008416
8417#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008418int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008419{
8420 int ret;
8421
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008422 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8423 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8424 {
8425 mbedtls_mpi_free( &conf->dhm_P );
8426 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008427 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008428 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008429
8430 return( 0 );
8431}
Hanno Becker470a8c42017-10-04 15:28:46 +01008432#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008433
Hanno Beckera90658f2017-10-04 15:29:08 +01008434int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8435 const unsigned char *dhm_P, size_t P_len,
8436 const unsigned char *dhm_G, size_t G_len )
8437{
8438 int ret;
8439
8440 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8441 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8442 {
8443 mbedtls_mpi_free( &conf->dhm_P );
8444 mbedtls_mpi_free( &conf->dhm_G );
8445 return( ret );
8446 }
8447
8448 return( 0 );
8449}
Paul Bakker5121ce52009-01-03 21:22:43 +00008450
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008451int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008452{
8453 int ret;
8454
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008455 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8456 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8457 {
8458 mbedtls_mpi_free( &conf->dhm_P );
8459 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008460 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008461 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008462
8463 return( 0 );
8464}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008465#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008466
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008467#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8468/*
8469 * Set the minimum length for Diffie-Hellman parameters
8470 */
8471void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8472 unsigned int bitlen )
8473{
8474 conf->dhm_min_bitlen = bitlen;
8475}
8476#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8477
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008478#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008479/*
8480 * Set allowed/preferred hashes for handshake signatures
8481 */
8482void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8483 const int *hashes )
8484{
8485 conf->sig_hashes = hashes;
8486}
Hanno Becker947194e2017-04-07 13:25:49 +01008487#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008488
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008489#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008490/*
8491 * Set the allowed elliptic curves
8492 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008493void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008494 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008495{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008496 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008497}
Hanno Becker947194e2017-04-07 13:25:49 +01008498#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008499
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008500#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008502{
Hanno Becker947194e2017-04-07 13:25:49 +01008503 /* Initialize to suppress unnecessary compiler warning */
8504 size_t hostname_len = 0;
8505
8506 /* Check if new hostname is valid before
8507 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008508 if( hostname != NULL )
8509 {
8510 hostname_len = strlen( hostname );
8511
8512 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8514 }
8515
8516 /* Now it's clear that we will overwrite the old hostname,
8517 * so we can free it safely */
8518
8519 if( ssl->hostname != NULL )
8520 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008521 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008522 mbedtls_free( ssl->hostname );
8523 }
8524
8525 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008526
Paul Bakker5121ce52009-01-03 21:22:43 +00008527 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008528 {
8529 ssl->hostname = NULL;
8530 }
8531 else
8532 {
8533 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008534 if( ssl->hostname == NULL )
8535 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008536
Hanno Becker947194e2017-04-07 13:25:49 +01008537 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008538
Hanno Becker947194e2017-04-07 13:25:49 +01008539 ssl->hostname[hostname_len] = '\0';
8540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008541
8542 return( 0 );
8543}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008544#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008545
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008546#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008547void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008549 const unsigned char *, size_t),
8550 void *p_sni )
8551{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008552 conf->f_sni = f_sni;
8553 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008554}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008558int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008559{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008560 size_t cur_len, tot_len;
8561 const char **p;
8562
8563 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008564 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8565 * MUST NOT be truncated."
8566 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008567 */
8568 tot_len = 0;
8569 for( p = protos; *p != NULL; p++ )
8570 {
8571 cur_len = strlen( *p );
8572 tot_len += cur_len;
8573
8574 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008576 }
8577
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008578 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008579
8580 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008581}
8582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008584{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008585 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008588
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008589void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008590{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008591 conf->max_major_ver = major;
8592 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008593}
8594
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008595void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008596{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008597 conf->min_major_ver = major;
8598 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008599}
8600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008602void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008603{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008604 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008605}
8606#endif
8607
Janos Follath088ce432017-04-10 12:42:31 +01008608#if defined(MBEDTLS_SSL_SRV_C)
8609void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8610 char cert_req_ca_list )
8611{
8612 conf->cert_req_ca_list = cert_req_ca_list;
8613}
8614#endif
8615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008617void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008618{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008619 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008620}
8621#endif
8622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008624void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008625{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008626 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008627}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008628
8629void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008630 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008631{
8632 conf->enforce_extended_master_secret = ems_enf;
8633}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008634#endif
8635
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008636#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008637void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008638{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008639 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008640}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008641#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008644int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008645{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008647 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008650 }
8651
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008652 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008653
8654 return( 0 );
8655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008659void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008660{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008661 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008666void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008667{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008668 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008669}
8670#endif
8671
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008672void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008673{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008674 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008675}
8676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008678void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008680 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008681}
8682
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008683void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008685 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008686}
8687
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008688void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008689 const unsigned char period[8] )
8690{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008691 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008696#if defined(MBEDTLS_SSL_CLI_C)
8697void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008698{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008699 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008700}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008701#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008702
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008703#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008704void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8705 mbedtls_ssl_ticket_write_t *f_ticket_write,
8706 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8707 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008708{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008709 conf->f_ticket_write = f_ticket_write;
8710 conf->f_ticket_parse = f_ticket_parse;
8711 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008712}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008713#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008715
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008716#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8717void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8718 mbedtls_ssl_export_keys_t *f_export_keys,
8719 void *p_export_keys )
8720{
8721 conf->f_export_keys = f_export_keys;
8722 conf->p_export_keys = p_export_keys;
8723}
8724#endif
8725
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008726#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008727void mbedtls_ssl_conf_async_private_cb(
8728 mbedtls_ssl_config *conf,
8729 mbedtls_ssl_async_sign_t *f_async_sign,
8730 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8731 mbedtls_ssl_async_resume_t *f_async_resume,
8732 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008733 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008734{
8735 conf->f_async_sign_start = f_async_sign;
8736 conf->f_async_decrypt_start = f_async_decrypt;
8737 conf->f_async_resume = f_async_resume;
8738 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008739 conf->p_async_config_data = async_config_data;
8740}
8741
Gilles Peskine8f97af72018-04-26 11:46:10 +02008742void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8743{
8744 return( conf->p_async_config_data );
8745}
8746
Gilles Peskine1febfef2018-04-30 11:54:39 +02008747void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008748{
8749 if( ssl->handshake == NULL )
8750 return( NULL );
8751 else
8752 return( ssl->handshake->user_async_ctx );
8753}
8754
Gilles Peskine1febfef2018-04-30 11:54:39 +02008755void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008756 void *ctx )
8757{
8758 if( ssl->handshake != NULL )
8759 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008760}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008761#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008762
Paul Bakker5121ce52009-01-03 21:22:43 +00008763/*
8764 * SSL get accessors
8765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008767{
8768 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8769}
8770
Hanno Becker8b170a02017-10-10 11:51:19 +01008771int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8772{
8773 /*
8774 * Case A: We're currently holding back
8775 * a message for further processing.
8776 */
8777
8778 if( ssl->keep_current_message == 1 )
8779 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008780 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008781 return( 1 );
8782 }
8783
8784 /*
8785 * Case B: Further records are pending in the current datagram.
8786 */
8787
8788#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008789 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008790 ssl->in_left > ssl->next_record_offset )
8791 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008793 return( 1 );
8794 }
8795#endif /* MBEDTLS_SSL_PROTO_DTLS */
8796
8797 /*
8798 * Case C: A handshake message is being processed.
8799 */
8800
Hanno Becker8b170a02017-10-10 11:51:19 +01008801 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8802 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008804 return( 1 );
8805 }
8806
8807 /*
8808 * Case D: An application data message is being processed
8809 */
8810 if( ssl->in_offt != NULL )
8811 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008812 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008813 return( 1 );
8814 }
8815
8816 /*
8817 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008818 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008819 * we implement support for multiple alerts in single records.
8820 */
8821
8822 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8823 return( 0 );
8824}
8825
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008826uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008827{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008828 if( ssl->session != NULL )
8829 return( ssl->session->verify_result );
8830
8831 if( ssl->session_negotiate != NULL )
8832 return( ssl->session_negotiate->verify_result );
8833
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008834 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008835}
8836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008838{
Paul Bakker926c8e42013-03-06 10:23:34 +01008839 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008840 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008843}
8844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008848 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008849 {
8850 switch( ssl->minor_ver )
8851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008853 return( "DTLSv1.0" );
8854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008856 return( "DTLSv1.2" );
8857
8858 default:
8859 return( "unknown (DTLS)" );
8860 }
8861 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008862 MBEDTLS_SSL_TRANSPORT_ELSE
8863#endif /* MBEDTLS_SSL_PROTO_DTLS */
8864#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008865 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008866 switch( ssl->minor_ver )
8867 {
8868 case MBEDTLS_SSL_MINOR_VERSION_0:
8869 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008870
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008871 case MBEDTLS_SSL_MINOR_VERSION_1:
8872 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008873
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008874 case MBEDTLS_SSL_MINOR_VERSION_2:
8875 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008876
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008877 case MBEDTLS_SSL_MINOR_VERSION_3:
8878 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008879
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008880 default:
8881 return( "unknown" );
8882 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008883 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008884#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008885}
8886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008888{
Hanno Becker3136ede2018-08-17 15:28:19 +01008889 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008891 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008892
Hanno Becker43395762019-05-03 14:46:38 +01008893 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8894
Hanno Becker78640902018-08-13 16:35:15 +01008895 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008896 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898#if defined(MBEDTLS_ZLIB_SUPPORT)
8899 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8900 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008901#endif
8902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905 case MBEDTLS_MODE_GCM:
8906 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008907 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008909 transform_expansion = transform->minlen;
8910 break;
8911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008913
8914 block_size = mbedtls_cipher_get_block_size(
8915 &transform->cipher_ctx_enc );
8916
Hanno Becker3136ede2018-08-17 15:28:19 +01008917 /* Expansion due to the addition of the MAC. */
8918 transform_expansion += transform->maclen;
8919
8920 /* Expansion due to the addition of CBC padding;
8921 * Theoretically up to 256 bytes, but we never use
8922 * more than the block size of the underlying cipher. */
8923 transform_expansion += block_size;
8924
8925 /* For TLS 1.1 or higher, an explicit IV is added
8926 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008927#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8928 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008929 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008930#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008931
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008932 break;
8933
8934 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008937 }
8938
Hanno Beckera5a2b082019-05-15 14:03:01 +01008939#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008940 if( transform->out_cid_len != 0 )
8941 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008942#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008943
Hanno Becker43395762019-05-03 14:46:38 +01008944 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008945}
8946
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008947#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8948size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8949{
8950 size_t max_len;
8951
8952 /*
8953 * Assume mfl_code is correct since it was checked when set
8954 */
Angus Grattond8213d02016-05-25 20:56:48 +10008955 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008956
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008957 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008958 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008959 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008960 {
Angus Grattond8213d02016-05-25 20:56:48 +10008961 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008962 }
8963
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008964 /* During a handshake, use the value being negotiated */
8965 if( ssl->session_negotiate != NULL &&
8966 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8967 {
8968 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8969 }
8970
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008971 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008972}
8973#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8974
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008975#if defined(MBEDTLS_SSL_PROTO_DTLS)
8976static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8977{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008978 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8979 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8980 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8981 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8982 return ( 0 );
8983
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008984 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8985 return( ssl->mtu );
8986
8987 if( ssl->mtu == 0 )
8988 return( ssl->handshake->mtu );
8989
8990 return( ssl->mtu < ssl->handshake->mtu ?
8991 ssl->mtu : ssl->handshake->mtu );
8992}
8993#endif /* MBEDTLS_SSL_PROTO_DTLS */
8994
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008995int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8996{
8997 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8998
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008999#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9000 !defined(MBEDTLS_SSL_PROTO_DTLS)
9001 (void) ssl;
9002#endif
9003
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009004#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9005 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9006
9007 if( max_len > mfl )
9008 max_len = mfl;
9009#endif
9010
9011#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009012 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009013 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009014 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009015 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9016 const size_t overhead = (size_t) ret;
9017
9018 if( ret < 0 )
9019 return( ret );
9020
9021 if( mtu <= overhead )
9022 {
9023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9024 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9025 }
9026
9027 if( max_len > mtu - overhead )
9028 max_len = mtu - overhead;
9029 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009030#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009031
Hanno Becker0defedb2018-08-10 12:35:02 +01009032#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9033 !defined(MBEDTLS_SSL_PROTO_DTLS)
9034 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009035#endif
9036
9037 return( (int) max_len );
9038}
9039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040#if defined(MBEDTLS_X509_CRT_PARSE_C)
9041const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009042{
9043 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009044 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009045
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009046#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009047 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009048#else
9049 return( NULL );
9050#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009055int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9056 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009057{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009058 if( ssl == NULL ||
9059 dst == NULL ||
9060 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009061 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009064 }
9065
Hanno Becker58fccf22019-02-06 14:30:46 +00009066 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009069
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009070const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9071{
9072 if( ssl == NULL )
9073 return( NULL );
9074
9075 return( ssl->session );
9076}
9077
Paul Bakker5121ce52009-01-03 21:22:43 +00009078/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009079 * Define ticket header determining Mbed TLS version
9080 * and structure of the ticket.
9081 */
9082
Hanno Becker41527622019-05-16 12:50:45 +01009083/*
Hanno Becker26829e92019-05-28 14:30:45 +01009084 * Define bitflag determining compile-time settings influencing
9085 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009086 */
9087
Hanno Becker26829e92019-05-28 14:30:45 +01009088#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009089#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009090#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009091#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009092#endif /* MBEDTLS_HAVE_TIME */
9093
9094#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009095#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009096#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009097#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009098#endif /* MBEDTLS_X509_CRT_PARSE_C */
9099
9100#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009101#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009102#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009103#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009104#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9105
9106#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009107#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009108#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009109#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009110#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9111
9112#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009113#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009114#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009115#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009116#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9117
9118#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009119#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009120#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009121#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009122#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9123
Hanno Becker41527622019-05-16 12:50:45 +01009124#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9125#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9126#else
9127#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9128#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9129
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009130#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9131#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9132#else
9133#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9134#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9135
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009136#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9137#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9138#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9139#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9140#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9141#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9142#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009143#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009144
Hanno Becker26829e92019-05-28 14:30:45 +01009145#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009146 ( (uint16_t) ( \
9147 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9148 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9149 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9150 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9151 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9152 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009153 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9154 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009155
Hanno Becker557fe9f2019-05-16 12:41:07 +01009156static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009157 MBEDTLS_VERSION_MAJOR,
9158 MBEDTLS_VERSION_MINOR,
9159 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009160 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9161 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009162};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009163
9164/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009165 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009166 * (in the presentation language of TLS, RFC 8446 section 3)
9167 *
Hanno Becker26829e92019-05-28 14:30:45 +01009168 * opaque mbedtls_version[3]; // major, minor, patch
9169 * opaque session_format[2]; // version-specific 16-bit field determining
9170 * // the format of the remaining
9171 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009172 *
9173 * Note: When updating the format, remember to keep
9174 * these version+format bytes.
9175 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009176 * // In this version, `session_format` determines
9177 * // the setting of those compile-time
9178 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009179 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009180 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009181 * uint8 ciphersuite[2]; // defined by the standard
9182 * uint8 compression; // 0 or 1
9183 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009184 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009185 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009186 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009187 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9188 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9189 * case disabled: uint8_t peer_cert_digest_type;
9190 * opaque peer_cert_digest<0..2^8-1>;
9191 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009192 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009193 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009194 * uint8 mfl_code; // up to 255 according to standard
9195 * uint8 trunc_hmac; // 0 or 1
9196 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009197 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009198 * The order is the same as in the definition of the structure, except
9199 * verify_result is put before peer_cert so that all mandatory fields come
9200 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009201 */
9202int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9203 unsigned char *buf,
9204 size_t buf_len,
9205 size_t *olen )
9206{
9207 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009208 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009209#if defined(MBEDTLS_HAVE_TIME)
9210 uint64_t start;
9211#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009212#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009213#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009214 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009215#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009216#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009217
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009218 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009219 * Add version identifier
9220 */
9221
9222 used += sizeof( ssl_serialized_session_header );
9223
9224 if( used <= buf_len )
9225 {
9226 memcpy( p, ssl_serialized_session_header,
9227 sizeof( ssl_serialized_session_header ) );
9228 p += sizeof( ssl_serialized_session_header );
9229 }
9230
9231 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009232 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009233 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009234#if defined(MBEDTLS_HAVE_TIME)
9235 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009236
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009237 if( used <= buf_len )
9238 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009239 start = (uint64_t) session->start;
9240
9241 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9242 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9243 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9244 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9245 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9246 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9247 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9248 *p++ = (unsigned char)( ( start ) & 0xFF );
9249 }
9250#endif /* MBEDTLS_HAVE_TIME */
9251
9252 /*
9253 * Basic mandatory fields
9254 */
9255 used += 2 /* ciphersuite */
9256 + 1 /* compression */
9257 + 1 /* id_len */
9258 + sizeof( session->id )
9259 + sizeof( session->master )
9260 + 4; /* verify_result */
9261
9262 if( used <= buf_len )
9263 {
9264 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9265 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9266
9267 *p++ = (unsigned char)( session->compression & 0xFF );
9268
9269 *p++ = (unsigned char)( session->id_len & 0xFF );
9270 memcpy( p, session->id, 32 );
9271 p += 32;
9272
9273 memcpy( p, session->master, 48 );
9274 p += 48;
9275
9276 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9277 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9278 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9279 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009280 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009281
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009282 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009283 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009284 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009285#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009286#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009287 if( session->peer_cert == NULL )
9288 cert_len = 0;
9289 else
9290 cert_len = session->peer_cert->raw.len;
9291
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009292 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009293
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009294 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009295 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009296 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9297 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9298 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9299
9300 if( session->peer_cert != NULL )
9301 {
9302 memcpy( p, session->peer_cert->raw.p, cert_len );
9303 p += cert_len;
9304 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009305 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009306
Hanno Becker5882dd02019-06-06 16:25:57 +01009307#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009308 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009309 if( session->peer_cert_digest != NULL )
9310 {
9311 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9312 if( used <= buf_len )
9313 {
9314 *p++ = (unsigned char) session->peer_cert_digest_type;
9315 *p++ = (unsigned char) session->peer_cert_digest_len;
9316 memcpy( p, session->peer_cert_digest,
9317 session->peer_cert_digest_len );
9318 p += session->peer_cert_digest_len;
9319 }
9320 }
9321 else
9322 {
9323 used += 2;
9324 if( used <= buf_len )
9325 {
9326 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9327 *p++ = 0;
9328 }
9329 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009330#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009331#endif /* MBEDTLS_X509_CRT_PARSE_C */
9332
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009333 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009334 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009335 */
9336#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009337 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009338
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009339 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009340 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009341 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9342 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9343 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9344
9345 if( session->ticket != NULL )
9346 {
9347 memcpy( p, session->ticket, session->ticket_len );
9348 p += session->ticket_len;
9349 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009350
9351 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9352 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9353 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9354 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009355 }
9356#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9357
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009358 /*
9359 * Misc extension-related info
9360 */
9361#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9362 used += 1;
9363
9364 if( used <= buf_len )
9365 *p++ = session->mfl_code;
9366#endif
9367
9368#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9369 used += 1;
9370
9371 if( used <= buf_len )
9372 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9373#endif
9374
9375#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9376 used += 1;
9377
9378 if( used <= buf_len )
9379 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9380#endif
9381
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009382 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009383 *olen = used;
9384
9385 if( used > buf_len )
9386 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009387
9388 return( 0 );
9389}
9390
9391/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009392 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009393 *
9394 * This internal version is wrapped by a public function that cleans up in
9395 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009396 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009397static int ssl_session_load( mbedtls_ssl_session *session,
9398 const unsigned char *buf,
9399 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009400{
9401 const unsigned char *p = buf;
9402 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009403#if defined(MBEDTLS_HAVE_TIME)
9404 uint64_t start;
9405#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009406#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009407#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009408 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009409#endif
9410#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009411
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009412 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009413 * Check version identifier
9414 */
9415
9416 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009418
9419 if( memcmp( p, ssl_serialized_session_header,
9420 sizeof( ssl_serialized_session_header ) ) != 0 )
9421 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009422 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009423 }
9424 p += sizeof( ssl_serialized_session_header );
9425
9426 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009427 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009428 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009429#if defined(MBEDTLS_HAVE_TIME)
9430 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9432
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009433 start = ( (uint64_t) p[0] << 56 ) |
9434 ( (uint64_t) p[1] << 48 ) |
9435 ( (uint64_t) p[2] << 40 ) |
9436 ( (uint64_t) p[3] << 32 ) |
9437 ( (uint64_t) p[4] << 24 ) |
9438 ( (uint64_t) p[5] << 16 ) |
9439 ( (uint64_t) p[6] << 8 ) |
9440 ( (uint64_t) p[7] );
9441 p += 8;
9442
9443 session->start = (time_t) start;
9444#endif /* MBEDTLS_HAVE_TIME */
9445
9446 /*
9447 * Basic mandatory fields
9448 */
9449 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9451
9452 session->ciphersuite = ( p[0] << 8 ) | p[1];
9453 p += 2;
9454
9455 session->compression = *p++;
9456
9457 session->id_len = *p++;
9458 memcpy( session->id, p, 32 );
9459 p += 32;
9460
9461 memcpy( session->master, p, 48 );
9462 p += 48;
9463
9464 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9465 ( (uint32_t) p[1] << 16 ) |
9466 ( (uint32_t) p[2] << 8 ) |
9467 ( (uint32_t) p[3] );
9468 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009469
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009470 /* Immediately clear invalid pointer values that have been read, in case
9471 * we exit early before we replaced them with valid ones. */
9472#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009473#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009474 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009475#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009476 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009477#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009478#endif
9479#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9480 session->ticket = NULL;
9481#endif
9482
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009483 /*
9484 * Peer certificate
9485 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009486#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009487#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009488 if( 3 > (size_t)( end - p ) )
9489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9490
9491 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9492 p += 3;
9493
9494 if( cert_len == 0 )
9495 {
9496 session->peer_cert = NULL;
9497 }
9498 else
9499 {
9500 int ret;
9501
9502 if( cert_len > (size_t)( end - p ) )
9503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9504
9505 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9506
9507 if( session->peer_cert == NULL )
9508 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9509
9510 mbedtls_x509_crt_init( session->peer_cert );
9511
9512 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9513 p, cert_len ) ) != 0 )
9514 {
9515 mbedtls_x509_crt_free( session->peer_cert );
9516 mbedtls_free( session->peer_cert );
9517 session->peer_cert = NULL;
9518 return( ret );
9519 }
9520
9521 p += cert_len;
9522 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009523#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009524 /* Deserialize CRT digest from the end of the ticket. */
9525 if( 2 > (size_t)( end - p ) )
9526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9527
9528 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9529 session->peer_cert_digest_len = (size_t) *p++;
9530
9531 if( session->peer_cert_digest_len != 0 )
9532 {
Hanno Becker2326d202019-06-06 14:54:55 +01009533 const mbedtls_md_info_t *md_info =
9534 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9535 if( md_info == NULL )
9536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9537 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9539
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009540 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9542
9543 session->peer_cert_digest =
9544 mbedtls_calloc( 1, session->peer_cert_digest_len );
9545 if( session->peer_cert_digest == NULL )
9546 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9547
9548 memcpy( session->peer_cert_digest, p,
9549 session->peer_cert_digest_len );
9550 p += session->peer_cert_digest_len;
9551 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009552#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009553#endif /* MBEDTLS_X509_CRT_PARSE_C */
9554
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009555 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009556 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009557 */
9558#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9559 if( 3 > (size_t)( end - p ) )
9560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9561
9562 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9563 p += 3;
9564
9565 if( session->ticket_len != 0 )
9566 {
9567 if( session->ticket_len > (size_t)( end - p ) )
9568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9569
9570 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9571 if( session->ticket == NULL )
9572 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9573
9574 memcpy( session->ticket, p, session->ticket_len );
9575 p += session->ticket_len;
9576 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009577
9578 if( 4 > (size_t)( end - p ) )
9579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9580
9581 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9582 ( (uint32_t) p[1] << 16 ) |
9583 ( (uint32_t) p[2] << 8 ) |
9584 ( (uint32_t) p[3] );
9585 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009586#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9587
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009588 /*
9589 * Misc extension-related info
9590 */
9591#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9592 if( 1 > (size_t)( end - p ) )
9593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9594
9595 session->mfl_code = *p++;
9596#endif
9597
9598#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9599 if( 1 > (size_t)( end - p ) )
9600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9601
9602 session->trunc_hmac = *p++;
9603#endif
9604
9605#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9606 if( 1 > (size_t)( end - p ) )
9607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9608
9609 session->encrypt_then_mac = *p++;
9610#endif
9611
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009612 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009613 if( p != end )
9614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9615
9616 return( 0 );
9617}
9618
9619/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009620 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009621 */
9622int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9623 const unsigned char *buf,
9624 size_t len )
9625{
9626 int ret = ssl_session_load( session, buf, len );
9627
9628 if( ret != 0 )
9629 mbedtls_ssl_session_free( session );
9630
9631 return( ret );
9632}
9633
9634/*
Paul Bakker1961b702013-01-25 14:49:24 +01009635 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009640
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009641 if( ssl == NULL || ssl->conf == NULL )
9642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009645 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009647#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009649 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009651#endif
9652
Paul Bakker1961b702013-01-25 14:49:24 +01009653 return( ret );
9654}
9655
9656/*
9657 * Perform the SSL handshake
9658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009660{
9661 int ret = 0;
9662
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009663 if( ssl == NULL || ssl->conf == NULL )
9664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009671
9672 if( ret != 0 )
9673 break;
9674 }
9675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009677
9678 return( ret );
9679}
9680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681#if defined(MBEDTLS_SSL_RENEGOTIATION)
9682#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009683/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009684 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009687{
9688 int ret;
9689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009691
9692 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9694 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009695
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009696 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009697 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009698 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009699 return( ret );
9700 }
9701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009703
9704 return( 0 );
9705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009707
9708/*
9709 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710 * - any side: calling mbedtls_ssl_renegotiate(),
9711 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9712 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009713 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009714 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009718{
9719 int ret;
9720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009722
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009723 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9724 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009725
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009726 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9727 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009729 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009731 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009732 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009733 ssl->handshake->out_msg_seq = 1;
9734 else
9735 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009736 }
9737#endif
9738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9740 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009745 return( ret );
9746 }
9747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009749
9750 return( 0 );
9751}
9752
9753/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009754 * Renegotiate current connection on client,
9755 * or request renegotiation on server
9756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009760
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009761 if( ssl == NULL || ssl->conf == NULL )
9762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009765 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009766 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9769 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009772
9773 /* Did we already try/start sending HelloRequest? */
9774 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009776
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009777 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009778 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009782 /*
9783 * On client, either start the renegotiation process or,
9784 * if already in progress, continue the handshake
9785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009790
9791 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009794 return( ret );
9795 }
9796 }
9797 else
9798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009802 return( ret );
9803 }
9804 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009806
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009807 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009808}
9809
9810/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009811 * Check record counters and renegotiate if they're above the limit.
9812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009814{
Andres AG2196c7f2016-12-15 17:01:16 +00009815 size_t ep_len = ssl_ep_len( ssl );
9816 int in_ctr_cmp;
9817 int out_ctr_cmp;
9818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9820 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009821 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009822 {
9823 return( 0 );
9824 }
9825
Andres AG2196c7f2016-12-15 17:01:16 +00009826 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9827 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009828 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009829 ssl->conf->renego_period + ep_len, 8 - ep_len );
9830
9831 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009832 {
9833 return( 0 );
9834 }
9835
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009838}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009840
9841/*
9842 * Receive application data decrypted from the SSL layer
9843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009845{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009846 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009847 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009848
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009849 if( ssl == NULL || ssl->conf == NULL )
9850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009855 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009858 return( ret );
9859
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009860 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009862 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009863 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009864 return( ret );
9865 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009866 }
9867#endif
9868
Hanno Becker4a810fb2017-05-24 16:27:30 +01009869 /*
9870 * Check if renegotiation is necessary and/or handshake is
9871 * in process. If yes, perform/continue, and fall through
9872 * if an unexpected packet is received while the client
9873 * is waiting for the ServerHello.
9874 *
9875 * (There is no equivalent to the last condition on
9876 * the server-side as it is not treated as within
9877 * a handshake while waiting for the ClientHello
9878 * after a renegotiation request.)
9879 */
9880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009882 ret = ssl_check_ctr_renegotiate( ssl );
9883 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9884 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009886 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009887 return( ret );
9888 }
9889#endif
9890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009894 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9895 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009898 return( ret );
9899 }
9900 }
9901
Hanno Beckere41158b2017-10-23 13:30:32 +01009902 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009903 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009904 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009905 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009906 if( ssl->f_get_timer != NULL &&
9907 ssl->f_get_timer( ssl->p_timer ) == -1 )
9908 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009909 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009910 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009911
Hanno Becker327c93b2018-08-15 13:56:18 +01009912 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009913 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009914 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9915 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009916
Hanno Becker4a810fb2017-05-24 16:27:30 +01009917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9918 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009919 }
9920
9921 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009923 {
9924 /*
9925 * OpenSSL sends empty messages to randomize the IV
9926 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009927 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009930 return( 0 );
9931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009933 return( ret );
9934 }
9935 }
9936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009940
Hanno Becker4a810fb2017-05-24 16:27:30 +01009941 /*
9942 * - For client-side, expect SERVER_HELLO_REQUEST.
9943 * - For server-side, expect CLIENT_HELLO.
9944 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9945 */
9946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009948 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009950 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009953
9954 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009956 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009957 {
9958 continue;
9959 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009960 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009961#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009962#if defined(MBEDTLS_SSL_PROTO_TLS)
9963 {
9964 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9965 }
9966#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009967 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009968#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009969
Hanno Becker4a810fb2017-05-24 16:27:30 +01009970#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009971 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009972 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009975
9976 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009978 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009979 {
9980 continue;
9981 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009982 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009983#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009984#if defined(MBEDTLS_SSL_PROTO_TLS)
9985 {
9986 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9987 }
9988#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009989 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009990#endif /* MBEDTLS_SSL_SRV_C */
9991
Hanno Becker21df7f92017-10-17 11:03:26 +01009992#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009993 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009994 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9995 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9996 ssl->conf->allow_legacy_renegotiation ==
9997 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9998 {
9999 /*
10000 * Accept renegotiation request
10001 */
Paul Bakker48916f92012-09-16 19:57:18 +000010002
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010003 /* DTLS clients need to know renego is server-initiated */
10004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010005 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010006 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10007 {
10008 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10009 }
10010#endif
10011 ret = ssl_start_renegotiation( ssl );
10012 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10013 ret != 0 )
10014 {
10015 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10016 return( ret );
10017 }
10018 }
10019 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010020#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010021 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010022 /*
10023 * Refuse renegotiation
10024 */
10025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028#if defined(MBEDTLS_SSL_PROTO_SSL3)
10029 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010030 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010031 /* SSLv3 does not have a "no_renegotiation" warning, so
10032 we send a fatal alert and abort the connection. */
10033 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10034 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10035 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010036 }
10037 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10039#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10040 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10041 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10044 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10045 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010046 {
10047 return( ret );
10048 }
Paul Bakker48916f92012-09-16 19:57:18 +000010049 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010050 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10052 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010056 }
Paul Bakker48916f92012-09-16 19:57:18 +000010057 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010058
Hanno Becker90333da2017-10-10 11:27:13 +010010059 /* At this point, we don't know whether the renegotiation has been
10060 * completed or not. The cases to consider are the following:
10061 * 1) The renegotiation is complete. In this case, no new record
10062 * has been read yet.
10063 * 2) The renegotiation is incomplete because the client received
10064 * an application data record while awaiting the ServerHello.
10065 * 3) The renegotiation is incomplete because the client received
10066 * a non-handshake, non-application data message while awaiting
10067 * the ServerHello.
10068 * In each of these case, looping will be the proper action:
10069 * - For 1), the next iteration will read a new record and check
10070 * if it's application data.
10071 * - For 2), the loop condition isn't satisfied as application data
10072 * is present, hence continue is the same as break
10073 * - For 3), the loop condition is satisfied and read_record
10074 * will re-deliver the message that was held back by the client
10075 * when expecting the ServerHello.
10076 */
10077 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010078 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010079#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010081 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010082 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010083 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010084 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010087 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010089 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010090 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010091 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10095 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010098 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010099 }
10100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10104 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010105 }
10106
10107 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010108
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010109 /* We're going to return something now, cancel timer,
10110 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010112 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010113
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010114#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010115 /* If we requested renego but received AppData, resend HelloRequest.
10116 * Do it now, after setting in_offt, to avoid taking this branch
10117 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010119 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010121 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010122 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010125 return( ret );
10126 }
10127 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010129#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010130 }
10131
10132 n = ( len < ssl->in_msglen )
10133 ? len : ssl->in_msglen;
10134
10135 memcpy( buf, ssl->in_offt, n );
10136 ssl->in_msglen -= n;
10137
10138 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010139 {
10140 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010141 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010142 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010143 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010144 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010145 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010146 /* more data available */
10147 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010148 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010151
Paul Bakker23986e52011-04-24 08:57:21 +000010152 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010153}
10154
10155/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010156 * Send application data to be encrypted by the SSL layer, taking care of max
10157 * fragment length and buffer size.
10158 *
10159 * According to RFC 5246 Section 6.2.1:
10160 *
10161 * Zero-length fragments of Application data MAY be sent as they are
10162 * potentially useful as a traffic analysis countermeasure.
10163 *
10164 * Therefore, it is possible that the input message length is 0 and the
10165 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010166 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010167static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010168 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010169{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010170 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10171 const size_t max_len = (size_t) ret;
10172
10173 if( ret < 0 )
10174 {
10175 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10176 return( ret );
10177 }
10178
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010179 if( len > max_len )
10180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010181#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010182 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010185 "maximum fragment length: %d > %d",
10186 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010188 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010189 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010190#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010191#if defined(MBEDTLS_SSL_PROTO_TLS)
10192 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010193 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010194 }
10195#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010196 }
Paul Bakker887bd502011-06-08 13:10:54 +000010197
Paul Bakker5121ce52009-01-03 21:22:43 +000010198 if( ssl->out_left != 0 )
10199 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010200 /*
10201 * The user has previously tried to send the data and
10202 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10203 * written. In this case, we expect the high-level write function
10204 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010209 return( ret );
10210 }
10211 }
Paul Bakker887bd502011-06-08 13:10:54 +000010212 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010213 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010214 /*
10215 * The user is trying to send a message the first time, so we need to
10216 * copy the data into the internal buffers and setup the data structure
10217 * to keep track of partial writes
10218 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010219 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010221 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010222
Hanno Becker67bc7c32018-08-06 11:33:50 +010010223 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010226 return( ret );
10227 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010228 }
10229
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010230 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010231}
10232
10233/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010234 * Write application data, doing 1/n-1 splitting if necessary.
10235 *
10236 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010237 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010238 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010241static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010242 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010243{
10244 int ret;
10245
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010246 if( ssl->conf->cbc_record_splitting ==
10247 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010248 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10250 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10251 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010252 {
10253 return( ssl_write_real( ssl, buf, len ) );
10254 }
10255
10256 if( ssl->split_done == 0 )
10257 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010258 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010259 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010260 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010261 }
10262
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010263 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10264 return( ret );
10265 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010266
10267 return( ret + 1 );
10268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010270
10271/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010272 * Write application data (public-facing wrapper)
10273 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010274int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010275{
10276 int ret;
10277
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010279
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010280 if( ssl == NULL || ssl->conf == NULL )
10281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10282
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010283#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010284 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10285 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010286 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010287 return( ret );
10288 }
10289#endif
10290
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010291 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010292 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010293 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010294 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010296 return( ret );
10297 }
10298 }
10299
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010300#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010301 ret = ssl_write_split( ssl, buf, len );
10302#else
10303 ret = ssl_write_real( ssl, buf, len );
10304#endif
10305
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010307
10308 return( ret );
10309}
10310
10311/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010312 * Notify the peer that the connection is being closed
10313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010315{
10316 int ret;
10317
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010318 if( ssl == NULL || ssl->conf == NULL )
10319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010322
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010323 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10329 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10330 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010333 return( ret );
10334 }
10335 }
10336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010338
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010339 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010340}
10341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010342void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010343{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010344 if( transform == NULL )
10345 return;
10346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010348 deflateEnd( &transform->ctx_deflate );
10349 inflateEnd( &transform->ctx_inflate );
10350#endif
10351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10353 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010354
Hanno Becker92231322018-01-03 15:32:51 +000010355#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356 mbedtls_md_free( &transform->md_ctx_enc );
10357 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010358#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010359
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010360 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010361}
10362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363#if defined(MBEDTLS_X509_CRT_PARSE_C)
10364static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010366 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010367
10368 while( cur != NULL )
10369 {
10370 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010372 cur = next;
10373 }
10374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010376
Hanno Becker0271f962018-08-16 13:23:47 +010010377#if defined(MBEDTLS_SSL_PROTO_DTLS)
10378
10379static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10380{
10381 unsigned offset;
10382 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10383
10384 if( hs == NULL )
10385 return;
10386
Hanno Becker283f5ef2018-08-24 09:34:47 +010010387 ssl_free_buffered_record( ssl );
10388
Hanno Becker0271f962018-08-16 13:23:47 +010010389 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010390 ssl_buffering_free_slot( ssl, offset );
10391}
10392
10393static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10394 uint8_t slot )
10395{
10396 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10397 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010398
10399 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10400 return;
10401
Hanno Beckere605b192018-08-21 15:59:07 +010010402 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010403 {
Hanno Beckere605b192018-08-21 15:59:07 +010010404 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010405 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010406 mbedtls_free( hs_buf->data );
10407 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010408 }
10409}
10410
10411#endif /* MBEDTLS_SSL_PROTO_DTLS */
10412
Gilles Peskine9b562d52018-04-25 20:32:43 +020010413void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010414{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010415 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10416
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010417 if( handshake == NULL )
10418 return;
10419
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010420#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10421 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10422 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010423 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010424 handshake->async_in_progress = 0;
10425 }
10426#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10427
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010428#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10429 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10430 mbedtls_md5_free( &handshake->fin_md5 );
10431 mbedtls_sha1_free( &handshake->fin_sha1 );
10432#endif
10433#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10434#if defined(MBEDTLS_SHA256_C)
10435 mbedtls_sha256_free( &handshake->fin_sha256 );
10436#endif
10437#if defined(MBEDTLS_SHA512_C)
10438 mbedtls_sha512_free( &handshake->fin_sha512 );
10439#endif
10440#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442#if defined(MBEDTLS_DHM_C)
10443 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010444#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010445#if defined(MBEDTLS_ECDH_C)
10446 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010447#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010448#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010449 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010450#if defined(MBEDTLS_SSL_CLI_C)
10451 mbedtls_free( handshake->ecjpake_cache );
10452 handshake->ecjpake_cache = NULL;
10453 handshake->ecjpake_cache_len = 0;
10454#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010455#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010456
Janos Follath4ae5c292016-02-10 11:27:43 +000010457#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10458 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010459 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010461#endif
10462
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010463#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10464 if( handshake->psk != NULL )
10465 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010466 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010467 mbedtls_free( handshake->psk );
10468 }
10469#endif
10470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10472 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010473 /*
10474 * Free only the linked list wrapper, not the keys themselves
10475 * since the belong to the SNI callback
10476 */
10477 if( handshake->sni_key_cert != NULL )
10478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010480
10481 while( cur != NULL )
10482 {
10483 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010484 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010485 cur = next;
10486 }
10487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010489
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010490#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010491 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010492 if( handshake->ecrs_peer_cert != NULL )
10493 {
10494 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10495 mbedtls_free( handshake->ecrs_peer_cert );
10496 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010497#endif
10498
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010499#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10500 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10501 mbedtls_pk_free( &handshake->peer_pubkey );
10502#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504#if defined(MBEDTLS_SSL_PROTO_DTLS)
10505 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010506 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010507 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010508#endif
10509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010510 mbedtls_platform_zeroize( handshake,
10511 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010512}
10513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010515{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010516 if( session == NULL )
10517 return;
10518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010520 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010521#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010522
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010523#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010524 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010525#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010526
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010527 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010528}
10529
Paul Bakker5121ce52009-01-03 21:22:43 +000010530/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010531 * Serialize a full SSL context
10532 */
10533int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10534 unsigned char *buf,
10535 size_t buf_len,
10536 size_t *olen )
10537{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010538 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010539 (void) ssl;
10540
10541 if( buf != NULL )
10542 memset( buf, 0, buf_len );
10543
10544 *olen = 0;
10545
10546 return( 0 );
10547}
10548
10549/*
10550 * Deserialize a full SSL context
10551 */
10552int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10553 const unsigned char *buf,
10554 size_t len )
10555{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010556 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010557 (void) ssl;
10558 (void) buf;
10559 (void) len;
10560
10561 return( 0 );
10562}
10563
10564/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010565 * Free an SSL context
10566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010567void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010568{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010569 if( ssl == NULL )
10570 return;
10571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010573
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010574 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010575 {
Angus Grattond8213d02016-05-25 20:56:48 +100010576 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010578 }
10579
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010580 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010581 {
Angus Grattond8213d02016-05-25 20:56:48 +100010582 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010583 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010584 }
10585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010586#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010587 if( ssl->compress_buf != NULL )
10588 {
Angus Grattond8213d02016-05-25 20:56:48 +100010589 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010591 }
10592#endif
10593
Paul Bakker48916f92012-09-16 19:57:18 +000010594 if( ssl->transform )
10595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596 mbedtls_ssl_transform_free( ssl->transform );
10597 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010598 }
10599
10600 if( ssl->handshake )
10601 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010602 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10604 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606 mbedtls_free( ssl->handshake );
10607 mbedtls_free( ssl->transform_negotiate );
10608 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010609 }
10610
Paul Bakkerc0463502013-02-14 11:19:38 +010010611 if( ssl->session )
10612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010613 mbedtls_ssl_session_free( ssl->session );
10614 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010615 }
10616
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010617#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010618 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010619 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010620 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010622 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010623#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10626 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10629 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010630 }
10631#endif
10632
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010633#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010634 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010635#endif
10636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010638
Paul Bakker86f04f42013-02-14 11:20:09 +010010639 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010640 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010641}
10642
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010643/*
10644 * Initialze mbedtls_ssl_config
10645 */
10646void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10647{
10648 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010649
10650#if !defined(MBEDTLS_SSL_PROTO_TLS)
10651 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10652#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010653}
10654
Simon Butcherc97b6972015-12-27 23:48:17 +000010655#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010656static int ssl_preset_default_hashes[] = {
10657#if defined(MBEDTLS_SHA512_C)
10658 MBEDTLS_MD_SHA512,
10659 MBEDTLS_MD_SHA384,
10660#endif
10661#if defined(MBEDTLS_SHA256_C)
10662 MBEDTLS_MD_SHA256,
10663 MBEDTLS_MD_SHA224,
10664#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010665#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010666 MBEDTLS_MD_SHA1,
10667#endif
10668 MBEDTLS_MD_NONE
10669};
Simon Butcherc97b6972015-12-27 23:48:17 +000010670#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010671
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010672static int ssl_preset_suiteb_ciphersuites[] = {
10673 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10674 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10675 0
10676};
10677
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010678#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010679static int ssl_preset_suiteb_hashes[] = {
10680 MBEDTLS_MD_SHA256,
10681 MBEDTLS_MD_SHA384,
10682 MBEDTLS_MD_NONE
10683};
10684#endif
10685
10686#if defined(MBEDTLS_ECP_C)
10687static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10688 MBEDTLS_ECP_DP_SECP256R1,
10689 MBEDTLS_ECP_DP_SECP384R1,
10690 MBEDTLS_ECP_DP_NONE
10691};
10692#endif
10693
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010694/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010695 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010696 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010697int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010698 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010699{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010700#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010701 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010702#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010703
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010704 /* Use the functions here so that they are covered in tests,
10705 * but otherwise access member directly for efficiency */
10706 mbedtls_ssl_conf_endpoint( conf, endpoint );
10707 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010708
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010709 /*
10710 * Things that are common to all presets
10711 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010712#if defined(MBEDTLS_SSL_CLI_C)
10713 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10714 {
10715 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10716#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10717 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10718#endif
10719 }
10720#endif
10721
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010722#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010723 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010724#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010725
10726#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10727 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10728#endif
10729
10730#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10731 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010732 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010733 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010734#endif
10735
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010736#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10737 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10738#endif
10739
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010740#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010741 conf->f_cookie_write = ssl_cookie_write_dummy;
10742 conf->f_cookie_check = ssl_cookie_check_dummy;
10743#endif
10744
10745#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10746 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10747#endif
10748
Janos Follath088ce432017-04-10 12:42:31 +010010749#if defined(MBEDTLS_SSL_SRV_C)
10750 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10751#endif
10752
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010753#if defined(MBEDTLS_SSL_PROTO_DTLS)
10754 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10755 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10756#endif
10757
10758#if defined(MBEDTLS_SSL_RENEGOTIATION)
10759 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010760 memset( conf->renego_period, 0x00, 2 );
10761 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010762#endif
10763
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010764#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10765 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10766 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010767 const unsigned char dhm_p[] =
10768 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10769 const unsigned char dhm_g[] =
10770 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10771
Hanno Beckera90658f2017-10-04 15:29:08 +010010772 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10773 dhm_p, sizeof( dhm_p ),
10774 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010775 {
10776 return( ret );
10777 }
10778 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010779#endif
10780
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010781 /*
10782 * Preset-specific defaults
10783 */
10784 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010785 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010786 /*
10787 * NSA Suite B
10788 */
10789 case MBEDTLS_SSL_PRESET_SUITEB:
10790 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10791 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10792 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10793 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10794
10795 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10796 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10797 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10798 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10799 ssl_preset_suiteb_ciphersuites;
10800
10801#if defined(MBEDTLS_X509_CRT_PARSE_C)
10802 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010803#endif
10804
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010805#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010806 conf->sig_hashes = ssl_preset_suiteb_hashes;
10807#endif
10808
10809#if defined(MBEDTLS_ECP_C)
10810 conf->curve_list = ssl_preset_suiteb_curves;
10811#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010812 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010813
10814 /*
10815 * Default
10816 */
10817 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010818 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10819 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10820 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10821 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10822 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10823 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10824 MBEDTLS_SSL_MIN_MINOR_VERSION :
10825 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010826 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10827 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10828
10829#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010830 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010831 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10832#endif
10833
10834 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10835 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10836 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10837 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10838 mbedtls_ssl_list_ciphersuites();
10839
10840#if defined(MBEDTLS_X509_CRT_PARSE_C)
10841 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10842#endif
10843
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010844#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010845 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010846#endif
10847
10848#if defined(MBEDTLS_ECP_C)
10849 conf->curve_list = mbedtls_ecp_grp_id_list();
10850#endif
10851
10852#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10853 conf->dhm_min_bitlen = 1024;
10854#endif
10855 }
10856
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010857 return( 0 );
10858}
10859
10860/*
10861 * Free mbedtls_ssl_config
10862 */
10863void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10864{
10865#if defined(MBEDTLS_DHM_C)
10866 mbedtls_mpi_free( &conf->dhm_P );
10867 mbedtls_mpi_free( &conf->dhm_G );
10868#endif
10869
10870#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10871 if( conf->psk != NULL )
10872 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010873 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010874 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010875 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010876 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010877 }
10878
10879 if( conf->psk_identity != NULL )
10880 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010881 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010882 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010883 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010884 conf->psk_identity_len = 0;
10885 }
10886#endif
10887
10888#if defined(MBEDTLS_X509_CRT_PARSE_C)
10889 ssl_key_cert_free( conf->key_cert );
10890#endif
10891
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010892 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010893}
10894
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010895#if defined(MBEDTLS_PK_C) && \
10896 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010897/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010900unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010901{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902#if defined(MBEDTLS_RSA_C)
10903 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10904 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906#if defined(MBEDTLS_ECDSA_C)
10907 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10908 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010911}
10912
Hanno Becker7e5437a2017-04-28 17:15:26 +010010913unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10914{
10915 switch( type ) {
10916 case MBEDTLS_PK_RSA:
10917 return( MBEDTLS_SSL_SIG_RSA );
10918 case MBEDTLS_PK_ECDSA:
10919 case MBEDTLS_PK_ECKEY:
10920 return( MBEDTLS_SSL_SIG_ECDSA );
10921 default:
10922 return( MBEDTLS_SSL_SIG_ANON );
10923 }
10924}
10925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010927{
10928 switch( sig )
10929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010930#if defined(MBEDTLS_RSA_C)
10931 case MBEDTLS_SSL_SIG_RSA:
10932 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010934#if defined(MBEDTLS_ECDSA_C)
10935 case MBEDTLS_SSL_SIG_ECDSA:
10936 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010937#endif
10938 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010939 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010940 }
10941}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010942#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010943
Hanno Becker7e5437a2017-04-28 17:15:26 +010010944#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10945 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10946
10947/* Find an entry in a signature-hash set matching a given hash algorithm. */
10948mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10949 mbedtls_pk_type_t sig_alg )
10950{
10951 switch( sig_alg )
10952 {
10953 case MBEDTLS_PK_RSA:
10954 return( set->rsa );
10955 case MBEDTLS_PK_ECDSA:
10956 return( set->ecdsa );
10957 default:
10958 return( MBEDTLS_MD_NONE );
10959 }
10960}
10961
10962/* Add a signature-hash-pair to a signature-hash set */
10963void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10964 mbedtls_pk_type_t sig_alg,
10965 mbedtls_md_type_t md_alg )
10966{
10967 switch( sig_alg )
10968 {
10969 case MBEDTLS_PK_RSA:
10970 if( set->rsa == MBEDTLS_MD_NONE )
10971 set->rsa = md_alg;
10972 break;
10973
10974 case MBEDTLS_PK_ECDSA:
10975 if( set->ecdsa == MBEDTLS_MD_NONE )
10976 set->ecdsa = md_alg;
10977 break;
10978
10979 default:
10980 break;
10981 }
10982}
10983
10984/* Allow exactly one hash algorithm for each signature. */
10985void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10986 mbedtls_md_type_t md_alg )
10987{
10988 set->rsa = md_alg;
10989 set->ecdsa = md_alg;
10990}
10991
10992#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10993 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10994
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010995/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010996 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010998mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010999{
11000 switch( hash )
11001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011002#if defined(MBEDTLS_MD5_C)
11003 case MBEDTLS_SSL_HASH_MD5:
11004 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011005#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006#if defined(MBEDTLS_SHA1_C)
11007 case MBEDTLS_SSL_HASH_SHA1:
11008 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011009#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011010#if defined(MBEDTLS_SHA256_C)
11011 case MBEDTLS_SSL_HASH_SHA224:
11012 return( MBEDTLS_MD_SHA224 );
11013 case MBEDTLS_SSL_HASH_SHA256:
11014 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016#if defined(MBEDTLS_SHA512_C)
11017 case MBEDTLS_SSL_HASH_SHA384:
11018 return( MBEDTLS_MD_SHA384 );
11019 case MBEDTLS_SSL_HASH_SHA512:
11020 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011021#endif
11022 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011023 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011024 }
11025}
11026
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011027/*
11028 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11029 */
11030unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11031{
11032 switch( md )
11033 {
11034#if defined(MBEDTLS_MD5_C)
11035 case MBEDTLS_MD_MD5:
11036 return( MBEDTLS_SSL_HASH_MD5 );
11037#endif
11038#if defined(MBEDTLS_SHA1_C)
11039 case MBEDTLS_MD_SHA1:
11040 return( MBEDTLS_SSL_HASH_SHA1 );
11041#endif
11042#if defined(MBEDTLS_SHA256_C)
11043 case MBEDTLS_MD_SHA224:
11044 return( MBEDTLS_SSL_HASH_SHA224 );
11045 case MBEDTLS_MD_SHA256:
11046 return( MBEDTLS_SSL_HASH_SHA256 );
11047#endif
11048#if defined(MBEDTLS_SHA512_C)
11049 case MBEDTLS_MD_SHA384:
11050 return( MBEDTLS_SSL_HASH_SHA384 );
11051 case MBEDTLS_MD_SHA512:
11052 return( MBEDTLS_SSL_HASH_SHA512 );
11053#endif
11054 default:
11055 return( MBEDTLS_SSL_HASH_NONE );
11056 }
11057}
11058
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011059#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011060/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011061 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011062 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011063 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011064int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011067
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011068 if( ssl->conf->curve_list == NULL )
11069 return( -1 );
11070
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011071 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011072 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011073 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011074
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011075 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011076}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011077#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011078
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011079#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011080/*
11081 * Check if a hash proposed by the peer is in our list.
11082 * Return 0 if we're willing to use it, -1 otherwise.
11083 */
11084int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11085 mbedtls_md_type_t md )
11086{
11087 const int *cur;
11088
11089 if( ssl->conf->sig_hashes == NULL )
11090 return( -1 );
11091
11092 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11093 if( *cur == (int) md )
11094 return( 0 );
11095
11096 return( -1 );
11097}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011098#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011100#if defined(MBEDTLS_X509_CRT_PARSE_C)
11101int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11102 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011103 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011104 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011105{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011106 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011107#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011108 int usage = 0;
11109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011110#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011111 const char *ext_oid;
11112 size_t ext_len;
11113#endif
11114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011115#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11116 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011117 ((void) cert);
11118 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011119 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011120#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011122#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11123 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011124 {
11125 /* Server part of the key exchange */
11126 switch( ciphersuite->key_exchange )
11127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011128 case MBEDTLS_KEY_EXCHANGE_RSA:
11129 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011130 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011131 break;
11132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011133 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11134 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11135 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11136 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011137 break;
11138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011139 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11140 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011141 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011142 break;
11143
11144 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011145 case MBEDTLS_KEY_EXCHANGE_NONE:
11146 case MBEDTLS_KEY_EXCHANGE_PSK:
11147 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11148 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011149 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011150 usage = 0;
11151 }
11152 }
11153 else
11154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011155 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11156 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011157 }
11158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011159 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011160 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011161 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011162 ret = -1;
11163 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011164#else
11165 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011166#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011168#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11169 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011171 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11172 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011173 }
11174 else
11175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011176 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11177 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011178 }
11179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011180 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011181 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011182 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011183 ret = -1;
11184 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011185#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011186
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011187 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011189#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011190
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011191/*
11192 * Convert version numbers to/from wire format
11193 * and, for DTLS, to/from TLS equivalent.
11194 *
11195 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011196 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011197 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11198 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011200void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011201 unsigned char ver[2] )
11202{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011203#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11204 ((void) transport);
11205#endif
11206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011208 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011210 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011211 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11212
11213 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11214 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11215 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011216 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011217#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011218#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011219 {
11220 ver[0] = (unsigned char) major;
11221 ver[1] = (unsigned char) minor;
11222 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011223#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011224}
11225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011226void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011227 const unsigned char ver[2] )
11228{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011229#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11230 ((void) transport);
11231#endif
11232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011234 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011235 {
11236 *major = 255 - ver[0] + 2;
11237 *minor = 255 - ver[1] + 1;
11238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011239 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011240 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11241 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011242 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011243#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011244#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011245 {
11246 *major = ver[0];
11247 *minor = ver[1];
11248 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011249#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011250}
11251
Simon Butcher99000142016-10-13 17:21:01 +010011252int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11253{
11254#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11255 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11256 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11257
11258 switch( md )
11259 {
11260#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11261#if defined(MBEDTLS_MD5_C)
11262 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011263 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011264#endif
11265#if defined(MBEDTLS_SHA1_C)
11266 case MBEDTLS_SSL_HASH_SHA1:
11267 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11268 break;
11269#endif
11270#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11271#if defined(MBEDTLS_SHA512_C)
11272 case MBEDTLS_SSL_HASH_SHA384:
11273 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11274 break;
11275#endif
11276#if defined(MBEDTLS_SHA256_C)
11277 case MBEDTLS_SSL_HASH_SHA256:
11278 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11279 break;
11280#endif
11281 default:
11282 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11283 }
11284
11285 return 0;
11286#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11287 (void) ssl;
11288 (void) md;
11289
11290 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11291#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11292}
11293
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011294#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11295 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11296int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11297 unsigned char *output,
11298 unsigned char *data, size_t data_len )
11299{
11300 int ret = 0;
11301 mbedtls_md5_context mbedtls_md5;
11302 mbedtls_sha1_context mbedtls_sha1;
11303
11304 mbedtls_md5_init( &mbedtls_md5 );
11305 mbedtls_sha1_init( &mbedtls_sha1 );
11306
11307 /*
11308 * digitally-signed struct {
11309 * opaque md5_hash[16];
11310 * opaque sha_hash[20];
11311 * };
11312 *
11313 * md5_hash
11314 * MD5(ClientHello.random + ServerHello.random
11315 * + ServerParams);
11316 * sha_hash
11317 * SHA(ClientHello.random + ServerHello.random
11318 * + ServerParams);
11319 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011320 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011321 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011323 goto exit;
11324 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011325 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011326 ssl->handshake->randbytes, 64 ) ) != 0 )
11327 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011329 goto exit;
11330 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011331 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011332 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_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_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011337 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011339 goto exit;
11340 }
11341
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011342 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011343 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345 goto exit;
11346 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011347 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011348 ssl->handshake->randbytes, 64 ) ) != 0 )
11349 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011351 goto exit;
11352 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011353 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354 data_len ) ) != 0 )
11355 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011357 goto exit;
11358 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011359 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011360 output + 16 ) ) != 0 )
11361 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011363 goto exit;
11364 }
11365
11366exit:
11367 mbedtls_md5_free( &mbedtls_md5 );
11368 mbedtls_sha1_free( &mbedtls_sha1 );
11369
11370 if( ret != 0 )
11371 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11372 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11373
11374 return( ret );
11375
11376}
11377#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11378 MBEDTLS_SSL_PROTO_TLS1_1 */
11379
11380#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11381 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11382int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011383 unsigned char *hash, size_t *hashlen,
11384 unsigned char *data, size_t data_len,
11385 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011386{
11387 int ret = 0;
11388 mbedtls_md_context_t ctx;
11389 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011390 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011391
11392 mbedtls_md_init( &ctx );
11393
11394 /*
11395 * digitally-signed struct {
11396 * opaque client_random[32];
11397 * opaque server_random[32];
11398 * ServerDHParams params;
11399 * };
11400 */
11401 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11402 {
11403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11404 goto exit;
11405 }
11406 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11407 {
11408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11409 goto exit;
11410 }
11411 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11412 {
11413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11414 goto exit;
11415 }
11416 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11417 {
11418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11419 goto exit;
11420 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011421 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011422 {
11423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11424 goto exit;
11425 }
11426
11427exit:
11428 mbedtls_md_free( &ctx );
11429
11430 if( ret != 0 )
11431 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11432 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11433
11434 return( ret );
11435}
11436#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11437 MBEDTLS_SSL_PROTO_TLS1_2 */
11438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011439#endif /* MBEDTLS_SSL_TLS_C */