blob: e06ce999ccf0a760fd300e9978ecd23b887626c9 [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
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200371#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200378 if( src->peer_cert != NULL )
379 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200380 int ret;
381
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200382 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200384 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200389 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 dst->peer_cert = NULL;
393 return( ret );
394 }
395 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200397
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200398#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399 if( src->ticket != NULL )
400 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200401 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200402 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200403 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200404
405 memcpy( dst->ticket, src->ticket, src->ticket_len );
406 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200407#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200408
409 return( 0 );
410}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200411#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
414int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200415 const unsigned char *key_enc, const unsigned char *key_dec,
416 size_t keylen,
417 const unsigned char *iv_enc, const unsigned char *iv_dec,
418 size_t ivlen,
419 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200420 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
422int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
423int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
424int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
425int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
426#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000427
Paul Bakker5121ce52009-01-03 21:22:43 +0000428/*
429 * Key material generation
430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200432static int ssl3_prf( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000435 unsigned char *dstbuf, size_t dlen )
436{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100437 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000438 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200439 mbedtls_md5_context md5;
440 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000441 unsigned char padding[16];
442 unsigned char sha1sum[20];
443 ((void)label);
444
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200445 mbedtls_md5_init( &md5 );
446 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200447
Paul Bakker5f70b252012-09-13 14:23:06 +0000448 /*
449 * SSLv3:
450 * block =
451 * MD5( secret + SHA1( 'A' + secret + random ) ) +
452 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
453 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
454 * ...
455 */
456 for( i = 0; i < dlen / 16; i++ )
457 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200458 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000459
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100461 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100463 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100464 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100465 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000470
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100473 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100474 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000479 }
480
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200482 mbedtls_md5_free( &md5 );
483 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000484
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500485 mbedtls_platform_zeroize( padding, sizeof( padding ) );
486 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000487
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200493static int tls1_prf( const unsigned char *secret, size_t slen,
494 const char *label,
495 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000496 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000497{
Paul Bakker23986e52011-04-24 08:57:21 +0000498 size_t nb, hs;
499 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200500 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 unsigned char tmp[128];
502 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 const mbedtls_md_info_t *md_info;
504 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100505 int ret;
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 hs = ( slen + 1 ) / 2;
513 S1 = secret;
514 S2 = secret + slen - hs;
515
516 nb = strlen( label );
517 memcpy( tmp + 20, label, nb );
518 memcpy( tmp + 20 + nb, random, rlen );
519 nb += rlen;
520
521 /*
522 * First compute P_md5(secret,label+random)[0..dlen]
523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100528 return( ret );
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
531 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
532 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534 for( i = 0; i < dlen; i += 16 )
535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_md_hmac_reset ( &md_ctx );
537 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
538 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_md_hmac_reset ( &md_ctx );
541 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
542 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
545
546 for( j = 0; j < k; j++ )
547 dstbuf[i + j] = h_i[j];
548 }
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100551
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 /*
553 * XOR out with P_sha1(secret,label+random)[0..dlen]
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100559 return( ret );
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
562 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
563 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565 for( i = 0; i < dlen; i += 20 )
566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_md_hmac_reset ( &md_ctx );
568 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
569 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_md_hmac_reset ( &md_ctx );
572 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
573 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
576
577 for( j = 0; j < k; j++ )
578 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
579 }
580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100582
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500583 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
584 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 return( 0 );
587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
591static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100592 const unsigned char *secret, size_t slen,
593 const char *label,
594 const unsigned char *random, size_t rlen,
595 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000596{
597 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100598 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000599 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
601 const mbedtls_md_info_t *md_info;
602 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100603 int ret;
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100611
612 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614
615 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100616 memcpy( tmp + md_len, label, nb );
617 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000618 nb += rlen;
619
620 /*
621 * Compute P_<hash>(secret, label + random)[0..dlen]
622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100624 return( ret );
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
627 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
628 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100629
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100630 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
634 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_md_hmac_reset ( &md_ctx );
637 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
638 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000639
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100640 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000641
642 for( j = 0; j < k; j++ )
643 dstbuf[i + j] = h_i[j];
644 }
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100647
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500648 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
649 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000650
651 return( 0 );
652}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100655static int tls_prf_sha256( const unsigned char *secret, size_t slen,
656 const char *label,
657 const unsigned char *random, size_t rlen,
658 unsigned char *dstbuf, size_t dlen )
659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100661 label, random, rlen, dstbuf, dlen ) );
662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200666static int tls_prf_sha384( const unsigned char *secret, size_t slen,
667 const char *label,
668 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000669 unsigned char *dstbuf, size_t dlen )
670{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100672 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#endif /* MBEDTLS_SHA512_C */
675#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
680 defined(MBEDTLS_SSL_PROTO_TLS1_1)
681static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200682#endif
Paul Bakker380da532012-04-18 16:10:25 +0000683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200685static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200687#endif
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200690static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200692#endif
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
695#if defined(MBEDTLS_SHA256_C)
696static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200697static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200699#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#if defined(MBEDTLS_SHA512_C)
702static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200703static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000707
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200708/* Type for the TLS PRF */
709typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
710 const unsigned char *, size_t,
711 unsigned char *, size_t);
712
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200713/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200714 * Populate a transform structure with session keys and all the other
715 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200716 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200717 * Parameters:
718 * - [in/out]: transform: structure to populate
719 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200720 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200721 * - [in] ciphersuite
722 * - [in] master
723 * - [in] encrypt_then_mac
724 * - [in] trunc_hmac
725 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200726 * - [in] tls_prf: pointer to PRF to use for key derivation
727 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200728 * - [in] minor_ver: SSL/TLS minor version
729 * - [in] endpoint: client or server
730 * - [in] ssl: optionally used for:
731 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
732 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
733 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200734 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200735static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200736 int ciphersuite,
737 const unsigned char master[48],
738#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
739 int encrypt_then_mac,
740#endif
741#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
742 int trunc_hmac,
743#endif
744#if defined(MBEDTLS_ZLIB_SUPPORT)
745 int compression,
746#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200747 ssl_tls_prf_t tls_prf,
748 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200749 int minor_ver,
750 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000752{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200753 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 unsigned char keyblk[256];
755 unsigned char *key1;
756 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100757 unsigned char *mac_enc;
758 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000759 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200760 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000761 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000762 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 const mbedtls_cipher_info_t *cipher_info;
764 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100765
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200766#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
767 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
768 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200769 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200770 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000771#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000772
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200773 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200775 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000776#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200777 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200779 /*
780 * Get various info structures
781 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200782 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200783 if( ciphersuite_info == NULL )
784 {
785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200786 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
788 }
789
Hanno Becker8759e162017-12-27 21:34:08 +0000790 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100791 if( cipher_info == NULL )
792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000794 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100796 }
797
Hanno Becker8759e162017-12-27 21:34:08 +0000798 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100799 if( md_info == NULL )
800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000802 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100804 }
805
Hanno Beckera5a2b082019-05-15 14:03:01 +0100806#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100807 /* Copy own and peer's CID if the use of the CID
808 * extension has been negotiated. */
809 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
810 {
811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100812
Hanno Becker4932f9f2019-05-03 15:23:51 +0100813 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100814 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100815 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100816 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100817
818 transform->out_cid_len = ssl->handshake->peer_cid_len;
819 memcpy( transform->out_cid, ssl->handshake->peer_cid,
820 ssl->handshake->peer_cid_len );
821 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
822 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100823 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100824#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100825
Paul Bakker5121ce52009-01-03 21:22:43 +0000826 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200827 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200829 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100830 if( ret != 0 )
831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100833 return( ret );
834 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200837 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200838 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200839 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 /*
843 * Determine the appropriate key, IV and MAC length.
844 */
Paul Bakker68884e32013-01-07 18:20:04 +0100845
Hanno Beckere7f2df02017-12-27 08:17:40 +0000846 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200847
Hanno Beckerf1229442018-01-03 15:32:31 +0000848#if defined(MBEDTLS_GCM_C) || \
849 defined(MBEDTLS_CCM_C) || \
850 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200852 cipher_info->mode == MBEDTLS_MODE_CCM ||
853 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 {
Hanno Becker8759e162017-12-27 21:34:08 +0000855 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200856
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200857 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000858 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000859 transform->taglen =
860 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200861
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200862 /* All modes haves 96-bit IVs;
863 * GCM and CCM has 4 implicit and 8 explicit bytes
864 * ChachaPoly has all 12 bytes implicit
865 */
Paul Bakker68884e32013-01-07 18:20:04 +0100866 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
868 transform->fixed_ivlen = 12;
869 else
870 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200871
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200872 /* Minimum length of encrypted record */
873 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000874 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100875 }
876 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000877#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
878#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
879 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
880 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100881 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200882 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
884 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200887 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100888 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000891 mac_key_len = mbedtls_md_get_size( md_info );
892 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200895 /*
896 * If HMAC is to be truncated, we shall keep the leftmost bytes,
897 * (rfc 6066 page 13 or rfc 2104 section 4),
898 * so we only need to adjust the length here.
899 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200900 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000903
904#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
905 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000906 * HMAC implementation which also truncates the key
907 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000908 mac_key_len = transform->maclen;
909#endif
910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
913 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100914 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200916 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200918 transform->minlen = transform->maclen;
919 else
Paul Bakker68884e32013-01-07 18:20:04 +0100920 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200921 /*
922 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100923 * 1. if EtM is in use: one block plus MAC
924 * otherwise: * first multiple of blocklen greater than maclen
925 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200928 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100929 {
930 transform->minlen = transform->maclen
931 + cipher_info->block_size;
932 }
933 else
934#endif
935 {
936 transform->minlen = transform->maclen
937 + cipher_info->block_size
938 - transform->maclen % cipher_info->block_size;
939 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200942 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
943 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200944 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100945 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200948 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
949 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200950 {
951 transform->minlen += transform->ivlen;
952 }
953 else
954#endif
955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
957 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200958 }
Paul Bakker68884e32013-01-07 18:20:04 +0100959 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000961 else
962#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
963 {
964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
966 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Hanno Beckere7f2df02017-12-27 08:17:40 +0000968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
969 (unsigned) keylen,
970 (unsigned) transform->minlen,
971 (unsigned) transform->ivlen,
972 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
974 /*
975 * Finally setup the cipher contexts, IVs and MAC secrets.
976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200978 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000980 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000981 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Paul Bakker68884e32013-01-07 18:20:04 +0100983 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000984 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000986 /*
987 * This is not used in TLS v1.1.
988 */
Paul Bakker48916f92012-09-16 19:57:18 +0000989 iv_copy_len = ( transform->fixed_ivlen ) ?
990 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000991 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
992 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000993 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 }
995 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#endif /* MBEDTLS_SSL_CLI_C */
997#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200998 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001000 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001001 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001004 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001005
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001006 /*
1007 * This is not used in TLS v1.1.
1008 */
Paul Bakker48916f92012-09-16 19:57:18 +00001009 iv_copy_len = ( transform->fixed_ivlen ) ?
1010 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001011 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1012 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001013 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001015 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker92231322018-01-03 15:32:51 +00001022#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001024 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001025 {
Hanno Becker92231322018-01-03 15:32:51 +00001026 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001030 }
1031
Hanno Becker81c7b182017-11-09 18:39:33 +00001032 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1033 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001034 }
1035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1037#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1038 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001039 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001040 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001041 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1042 For AEAD-based ciphersuites, there is nothing to do here. */
1043 if( mac_key_len != 0 )
1044 {
1045 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1046 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1047 }
Paul Bakker68884e32013-01-07 18:20:04 +01001048 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001049 else
1050#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001054 }
Hanno Becker92231322018-01-03 15:32:51 +00001055#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1058 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001059 {
1060 int ret = 0;
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001063
Hanno Beckere7f2df02017-12-27 08:17:40 +00001064 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001065 transform->iv_enc, transform->iv_dec,
1066 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001067 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001068 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1071 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001072 }
1073 }
Hanno Becker92231322018-01-03 15:32:51 +00001074#else
1075 ((void) mac_dec);
1076 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001078
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001079#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1080 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001081 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001082 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001083 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001084 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001085 iv_copy_len );
1086 }
1087#endif
1088
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001089 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001093 return( ret );
1094 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001095
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001096 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001097 cipher_info ) ) != 0 )
1098 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001100 return( ret );
1101 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001104 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001108 return( ret );
1109 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001112 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 return( ret );
1117 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119#if defined(MBEDTLS_CIPHER_MODE_CBC)
1120 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1123 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001126 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001127 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1130 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001133 return( ret );
1134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001138 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001140 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001142 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001145
Paul Bakker48916f92012-09-16 19:57:18 +00001146 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1147 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001148
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001149 if( deflateInit( &transform->ctx_deflate,
1150 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001151 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1154 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001155 }
1156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001158
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 return( 0 );
1160}
1161
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001162/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001163 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001164 *
1165 * Inputs:
1166 * - SSL/TLS minor version
1167 * - hash associated with the ciphersuite (only used by TLS 1.2)
1168 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001169 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001170 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1171 */
1172static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1173 int minor_ver,
1174 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001175{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001176#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1177 (void) hash;
1178#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001179
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001180#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001181 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001182 {
1183 handshake->tls_prf = ssl3_prf;
1184 handshake->calc_verify = ssl_calc_verify_ssl;
1185 handshake->calc_finished = ssl_calc_finished_ssl;
1186 }
1187 else
1188#endif
1189#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001190 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001191 {
1192 handshake->tls_prf = tls1_prf;
1193 handshake->calc_verify = ssl_calc_verify_tls;
1194 handshake->calc_finished = ssl_calc_finished_tls;
1195 }
1196 else
1197#endif
1198#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1199#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1201 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001202 {
1203 handshake->tls_prf = tls_prf_sha384;
1204 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1205 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1206 }
1207 else
1208#endif
1209#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001210 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001211 {
1212 handshake->tls_prf = tls_prf_sha256;
1213 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1214 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1215 }
1216 else
1217#endif
1218#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1219 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1221 }
1222
1223 return( 0 );
1224}
1225
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001226/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001227 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001228 *
1229 * Parameters:
1230 * [in/out] handshake
1231 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1232 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001233 * [out] master
1234 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001235 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001236static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001237 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001238 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001239{
1240 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001241
1242#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001243 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001244 (void) ssl;
1245#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001246
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001247 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001248 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1250 return( 0 );
1251 }
1252
1253 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1254 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001255
1256#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001257 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001258 {
1259 unsigned char session_hash[48];
1260 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001261
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001262 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001264 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1265 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001266
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001267 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268 "extended master secret",
1269 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001270 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001271 }
1272 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001273#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001274 {
1275 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1276 "master secret",
1277 handshake->randbytes, 64,
1278 master, 48 );
1279 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001280 if( ret != 0 )
1281 {
1282 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1283 return( ret );
1284 }
1285
1286 mbedtls_platform_zeroize( handshake->premaster,
1287 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001288
1289 return( 0 );
1290}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001291
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001292int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1293{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001294 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001295 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1296 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001297
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1299
1300 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001301 ret = ssl_set_handshake_prfs( ssl->handshake,
1302 ssl->minor_ver,
1303 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001304 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001305 {
1306 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001307 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001308 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001309
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001310 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001311 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001312 ssl->session_negotiate->master,
1313 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001314 if( ret != 0 )
1315 {
1316 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1317 return( ret );
1318 }
1319
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001320 /* Swap the client and server random values:
1321 * - MS derivation wanted client+server (RFC 5246 8.1)
1322 * - key derivation wants server+client (RFC 5246 6.3) */
1323 {
1324 unsigned char tmp[64];
1325 memcpy( tmp, ssl->handshake->randbytes, 64 );
1326 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1327 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1328 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1329 }
1330
1331 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001332 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001333 ssl->session_negotiate->ciphersuite,
1334 ssl->session_negotiate->master,
1335#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1336 ssl->session_negotiate->encrypt_then_mac,
1337#endif
1338#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1339 ssl->session_negotiate->trunc_hmac,
1340#endif
1341#if defined(MBEDTLS_ZLIB_SUPPORT)
1342 ssl->session_negotiate->compression,
1343#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001344 ssl->handshake->tls_prf,
1345 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001346 ssl->minor_ver,
1347 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001348 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001349 if( ret != 0 )
1350 {
1351 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1352 return( ret );
1353 }
1354
1355 /* We no longer need Server/ClientHello.random values */
1356 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1357 sizeof( ssl->handshake->randbytes ) );
1358
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001359 /* Allocate compression buffer */
1360#if defined(MBEDTLS_ZLIB_SUPPORT)
1361 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1362 ssl->compress_buf == NULL )
1363 {
1364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1365 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1366 if( ssl->compress_buf == NULL )
1367 {
1368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001369 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001370 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1371 }
1372 }
1373#endif
1374
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1376
1377 return( 0 );
1378}
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001381void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1382 unsigned char hash[36],
1383 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001385 mbedtls_md5_context md5;
1386 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 unsigned char pad_1[48];
1388 unsigned char pad_2[48];
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001392 mbedtls_md5_init( &md5 );
1393 mbedtls_sha1_init( &sha1 );
1394
1395 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1396 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Paul Bakker380da532012-04-18 16:10:25 +00001398 memset( pad_1, 0x36, 48 );
1399 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001401 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1402 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1403 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001405 mbedtls_md5_starts_ret( &md5 );
1406 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1407 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1408 mbedtls_md5_update_ret( &md5, hash, 16 );
1409 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001411 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1412 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1413 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001414
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001415 mbedtls_sha1_starts_ret( &sha1 );
1416 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1417 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1418 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1419 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001420
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001421 *hlen = 36;
1422
1423 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001425
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001426 mbedtls_md5_free( &md5 );
1427 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001428
Paul Bakker380da532012-04-18 16:10:25 +00001429 return;
1430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001434void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1435 unsigned char hash[36],
1436 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001437{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001438 mbedtls_md5_context md5;
1439 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001443 mbedtls_md5_init( &md5 );
1444 mbedtls_sha1_init( &sha1 );
1445
1446 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1447 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001448
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001449 mbedtls_md5_finish_ret( &md5, hash );
1450 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001452 *hlen = 36;
1453
1454 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001457 mbedtls_md5_free( &md5 );
1458 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001459
Paul Bakker380da532012-04-18 16:10:25 +00001460 return;
1461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1465#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001466void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1467 unsigned char hash[32],
1468 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001469{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001470 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001471
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001472 mbedtls_sha256_init( &sha256 );
1473
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001475
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001476 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001477 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001479 *hlen = 32;
1480
1481 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001484 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001485
Paul Bakker380da532012-04-18 16:10:25 +00001486 return;
1487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001491void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1492 unsigned char hash[48],
1493 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001494{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001496
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001497 mbedtls_sha512_init( &sha512 );
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001501 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001502 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001504 *hlen = 48;
1505
1506 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001509 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001510
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 return;
1512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_SHA512_C */
1514#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1517int 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 +02001518{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001519 unsigned char *p = ssl->handshake->premaster;
1520 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001521 const unsigned char *psk = ssl->conf->psk;
1522 size_t psk_len = ssl->conf->psk_len;
1523
1524 /* If the psk callback was called, use its result */
1525 if( ssl->handshake->psk != NULL )
1526 {
1527 psk = ssl->handshake->psk;
1528 psk_len = ssl->handshake->psk_len;
1529 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001530
1531 /*
1532 * PMS = struct {
1533 * opaque other_secret<0..2^16-1>;
1534 * opaque psk<0..2^16-1>;
1535 * };
1536 * with "other_secret" depending on the particular key exchange
1537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1539 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001540 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001541 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001543
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001544 *(p++) = (unsigned char)( psk_len >> 8 );
1545 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001546
1547 if( end < p || (size_t)( end - p ) < psk_len )
1548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1549
1550 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001551 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001552 }
1553 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1555#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1556 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001557 {
1558 /*
1559 * other_secret already set by the ClientKeyExchange message,
1560 * and is 48 bytes long
1561 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001562 if( end - p < 2 )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001565 *p++ = 0;
1566 *p++ = 48;
1567 p += 48;
1568 }
1569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1571#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1572 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001573 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001574 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001575 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001576
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001577 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001579 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001580 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001583 return( ret );
1584 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001585 *(p++) = (unsigned char)( len >> 8 );
1586 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001587 p += len;
1588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001590 }
1591 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1593#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1594 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001595 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001596 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 size_t zlen;
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001600 p + 2, end - ( p + 2 ),
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_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604 return( ret );
1605 }
1606
1607 *(p++) = (unsigned char)( zlen >> 8 );
1608 *(p++) = (unsigned char)( zlen );
1609 p += zlen;
1610
Janos Follath3fbdada2018-08-15 10:26:53 +01001611 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1612 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 }
1614 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1618 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 }
1620
1621 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001622 if( end - p < 2 )
1623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001624
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001625 *(p++) = (unsigned char)( psk_len >> 8 );
1626 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001627
1628 if( end < p || (size_t)( end - p ) < psk_len )
1629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1630
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001631 memcpy( p, psk, psk_len );
1632 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633
1634 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1635
1636 return( 0 );
1637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001641/*
1642 * SSLv3.0 MAC functions
1643 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001644#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001645static void ssl_mac( mbedtls_md_context_t *md_ctx,
1646 const unsigned char *secret,
1647 const unsigned char *buf, size_t len,
1648 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001649 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
1651 unsigned char header[11];
1652 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001653 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1655 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001656
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001657 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001659 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001660 else
Paul Bakker68884e32013-01-07 18:20:04 +01001661 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
1663 memcpy( header, ctr, 8 );
1664 header[ 8] = (unsigned char) type;
1665 header[ 9] = (unsigned char)( len >> 8 );
1666 header[10] = (unsigned char)( len );
1667
Paul Bakker68884e32013-01-07 18:20:04 +01001668 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 mbedtls_md_starts( md_ctx );
1670 mbedtls_md_update( md_ctx, secret, md_size );
1671 mbedtls_md_update( md_ctx, padding, padlen );
1672 mbedtls_md_update( md_ctx, header, 11 );
1673 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001674 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Paul Bakker68884e32013-01-07 18:20:04 +01001676 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_md_starts( md_ctx );
1678 mbedtls_md_update( md_ctx, secret, md_size );
1679 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001680 mbedtls_md_update( md_ctx, out, md_size );
1681 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001684
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001685/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001686 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001687#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001688 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1689 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1690 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1691/* This function makes sure every byte in the memory region is accessed
1692 * (in ascending addresses order) */
1693static void ssl_read_memory( unsigned char *p, size_t len )
1694{
1695 unsigned char acc = 0;
1696 volatile unsigned char force;
1697
1698 for( ; len != 0; p++, len-- )
1699 acc ^= *p;
1700
1701 force = acc;
1702 (void) force;
1703}
1704#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1705
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001706/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001708 */
Hanno Becker3307b532017-12-27 21:37:21 +00001709
Hanno Beckera5a2b082019-05-15 14:03:01 +01001710#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001711/* This functions transforms a DTLS plaintext fragment and a record content
1712 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001713 *
1714 * struct {
1715 * opaque content[DTLSPlaintext.length];
1716 * ContentType real_type;
1717 * uint8 zeros[length_of_padding];
1718 * } DTLSInnerPlaintext;
1719 *
1720 * Input:
1721 * - `content`: The beginning of the buffer holding the
1722 * plaintext to be wrapped.
1723 * - `*content_size`: The length of the plaintext in Bytes.
1724 * - `max_len`: The number of Bytes available starting from
1725 * `content`. This must be `>= *content_size`.
1726 * - `rec_type`: The desired record content type.
1727 *
1728 * Output:
1729 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1730 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1731 *
1732 * Returns:
1733 * - `0` on success.
1734 * - A negative error code if `max_len` didn't offer enough space
1735 * for the expansion.
1736 */
1737static int ssl_cid_build_inner_plaintext( unsigned char *content,
1738 size_t *content_size,
1739 size_t remaining,
1740 uint8_t rec_type )
1741{
1742 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001743 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1744 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1745 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001746
1747 /* Write real content type */
1748 if( remaining == 0 )
1749 return( -1 );
1750 content[ len ] = rec_type;
1751 len++;
1752 remaining--;
1753
1754 if( remaining < pad )
1755 return( -1 );
1756 memset( content + len, 0, pad );
1757 len += pad;
1758 remaining -= pad;
1759
1760 *content_size = len;
1761 return( 0 );
1762}
1763
Hanno Becker7dc25772019-05-20 15:08:01 +01001764/* This function parses a DTLSInnerPlaintext structure.
1765 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001766static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1767 size_t *content_size,
1768 uint8_t *rec_type )
1769{
1770 size_t remaining = *content_size;
1771
1772 /* Determine length of padding by skipping zeroes from the back. */
1773 do
1774 {
1775 if( remaining == 0 )
1776 return( -1 );
1777 remaining--;
1778 } while( content[ remaining ] == 0 );
1779
1780 *content_size = remaining;
1781 *rec_type = content[ remaining ];
1782
1783 return( 0 );
1784}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001785#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001786
Hanno Becker99abf512019-05-20 14:50:53 +01001787/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001788 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001789static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001790 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001791 mbedtls_record *rec )
1792{
Hanno Becker99abf512019-05-20 14:50:53 +01001793 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001794 *
1795 * additional_data = seq_num + TLSCompressed.type +
1796 * TLSCompressed.version + TLSCompressed.length;
1797 *
Hanno Becker99abf512019-05-20 14:50:53 +01001798 * For the CID extension, this is extended as follows
1799 * (quoting draft-ietf-tls-dtls-connection-id-05,
1800 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001801 *
1802 * additional_data = seq_num + DTLSPlaintext.type +
1803 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001804 * cid +
1805 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001806 * length_of_DTLSInnerPlaintext;
1807 */
1808
Hanno Becker3307b532017-12-27 21:37:21 +00001809 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1810 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001811 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001812
Hanno Beckera5a2b082019-05-15 14:03:01 +01001813#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001814 if( rec->cid_len != 0 )
1815 {
1816 memcpy( add_data + 11, rec->cid, rec->cid_len );
1817 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1818 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1819 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1820 *add_data_len = 13 + 1 + rec->cid_len;
1821 }
1822 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001823#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001824 {
1825 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1826 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1827 *add_data_len = 13;
1828 }
Hanno Becker3307b532017-12-27 21:37:21 +00001829}
1830
Hanno Becker611a83b2018-01-03 14:27:32 +00001831int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1832 mbedtls_ssl_transform *transform,
1833 mbedtls_record *rec,
1834 int (*f_rng)(void *, unsigned char *, size_t),
1835 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001838 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001839 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001840 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001841 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001842 size_t post_avail;
1843
1844 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001845#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001846 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001847 ((void) ssl);
1848#endif
1849
1850 /* The PRNG is used for dynamic IV generation that's used
1851 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1852#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1853 ( defined(MBEDTLS_AES_C) || \
1854 defined(MBEDTLS_ARIA_C) || \
1855 defined(MBEDTLS_CAMELLIA_C) ) && \
1856 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1857 ((void) f_rng);
1858 ((void) p_rng);
1859#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Hanno Becker3307b532017-12-27 21:37:21 +00001863 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001864 {
Hanno Becker3307b532017-12-27 21:37:21 +00001865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1867 }
Hanno Becker505089d2019-05-01 09:45:57 +01001868 if( rec == NULL
1869 || rec->buf == NULL
1870 || rec->buf_len < rec->data_offset
1871 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001872#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001873 || rec->cid_len != 0
1874#endif
1875 )
Hanno Becker3307b532017-12-27 21:37:21 +00001876 {
1877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001879 }
1880
Hanno Becker3307b532017-12-27 21:37:21 +00001881 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001882 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001884 data, rec->data_len );
1885
1886 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1887
1888 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1889 {
1890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1891 (unsigned) rec->data_len,
1892 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1894 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001895
Hanno Beckera5a2b082019-05-15 14:03:01 +01001896#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001897 /*
1898 * Add CID information
1899 */
1900 rec->cid_len = transform->out_cid_len;
1901 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1902 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001903
1904 if( rec->cid_len != 0 )
1905 {
1906 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001907 * Wrap plaintext into DTLSInnerPlaintext structure.
1908 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001909 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001910 * Note that this changes `rec->data_len`, and hence
1911 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001912 */
1913 if( ssl_cid_build_inner_plaintext( data,
1914 &rec->data_len,
1915 post_avail,
1916 rec->type ) != 0 )
1917 {
1918 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1919 }
1920
1921 rec->type = MBEDTLS_SSL_MSG_CID;
1922 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001923#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001924
Hanno Becker92c930f2019-04-29 17:31:37 +01001925 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001928 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001930#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 if( mode == MBEDTLS_MODE_STREAM ||
1932 ( mode == MBEDTLS_MODE_CBC
1933#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001934 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001935#endif
1936 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 {
Hanno Becker3307b532017-12-27 21:37:21 +00001938 if( post_avail < transform->maclen )
1939 {
1940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1941 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1942 }
1943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001945 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001946 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001947 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001948 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1949 data, rec->data_len, rec->ctr, rec->type, mac );
1950 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001951 }
1952 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1955 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001956 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001957 {
Hanno Becker992b6872017-11-09 18:57:39 +00001958 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1959
Hanno Beckere83efe62019-04-29 13:52:53 +01001960 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001961
Hanno Becker3307b532017-12-27 21:37:21 +00001962 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001963 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001964 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1965 data, rec->data_len );
1966 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1967 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1968
1969 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 }
1971 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001972#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001976 }
1977
Hanno Becker3307b532017-12-27 21:37:21 +00001978 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1979 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001980
Hanno Becker3307b532017-12-27 21:37:21 +00001981 rec->data_len += transform->maclen;
1982 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001983 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001984 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001985#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001987 /*
1988 * Encrypt
1989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1991 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001993 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001994 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001996 "including %d bytes of padding",
1997 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
Hanno Becker3307b532017-12-27 21:37:21 +00001999 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2000 transform->iv_enc, transform->ivlen,
2001 data, rec->data_len,
2002 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002005 return( ret );
2006 }
2007
Hanno Becker3307b532017-12-27 21:37:21 +00002008 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
Paul Bakker68884e32013-01-07 18:20:04 +01002014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002016
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002017#if defined(MBEDTLS_GCM_C) || \
2018 defined(MBEDTLS_CCM_C) || \
2019 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002021 mode == MBEDTLS_MODE_CCM ||
2022 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002023 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002024 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002025 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002026 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002027
Hanno Becker3307b532017-12-27 21:37:21 +00002028 /* Check that there's space for both the authentication tag
2029 * and the explicit IV before and after the record content. */
2030 if( post_avail < transform->taglen ||
2031 rec->data_offset < explicit_iv_len )
2032 {
2033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2034 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2035 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002036
Paul Bakker68884e32013-01-07 18:20:04 +01002037 /*
2038 * Generate IV
2039 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2041 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002042 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002043 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002044 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2045 explicit_iv_len );
2046 /* Prefix record content with explicit IV. */
2047 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002048 }
2049 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2050 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002051 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002052 unsigned char i;
2053
2054 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2055
2056 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002057 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 }
2059 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002060 {
2061 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002064 }
2065
Hanno Beckere83efe62019-04-29 13:52:53 +01002066 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002067
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002068 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2069 iv, transform->ivlen );
2070 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002071 data - explicit_iv_len, explicit_iv_len );
2072 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002073 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002075 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002076 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002077
Paul Bakker68884e32013-01-07 18:20:04 +01002078 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002079 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002080 */
Hanno Becker3307b532017-12-27 21:37:21 +00002081
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002083 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002084 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002085 data, rec->data_len, /* source */
2086 data, &rec->data_len, /* destination */
2087 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002090 return( ret );
2091 }
2092
Hanno Becker3307b532017-12-27 21:37:21 +00002093 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2094 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002095
Hanno Becker3307b532017-12-27 21:37:21 +00002096 rec->data_len += transform->taglen + explicit_iv_len;
2097 rec->data_offset -= explicit_iv_len;
2098 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002099 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2103#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002104 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002107 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002108 size_t padlen, i;
2109 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002110
Hanno Becker3307b532017-12-27 21:37:21 +00002111 /* Currently we're always using minimal padding
2112 * (up to 255 bytes would be allowed). */
2113 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2114 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 padlen = 0;
2116
Hanno Becker3307b532017-12-27 21:37:21 +00002117 /* Check there's enough space in the buffer for the padding. */
2118 if( post_avail < padlen + 1 )
2119 {
2120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2121 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2122 }
2123
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002125 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Hanno Becker3307b532017-12-27 21:37:21 +00002127 rec->data_len += padlen + 1;
2128 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002132 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2133 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002134 */
Hanno Becker3307b532017-12-27 21:37:21 +00002135 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002136 {
Hanno Becker3307b532017-12-27 21:37:21 +00002137 if( f_rng == NULL )
2138 {
2139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2141 }
2142
2143 if( rec->data_offset < transform->ivlen )
2144 {
2145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2146 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2147 }
2148
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149 /*
2150 * Generate IV
2151 */
Hanno Becker3307b532017-12-27 21:37:21 +00002152 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002153 if( ret != 0 )
2154 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155
Hanno Becker3307b532017-12-27 21:37:21 +00002156 memcpy( data - transform->ivlen, transform->iv_enc,
2157 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002163 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002164 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002165 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
Hanno Becker3307b532017-12-27 21:37:21 +00002167 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2168 transform->iv_enc,
2169 transform->ivlen,
2170 data, rec->data_len,
2171 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002174 return( ret );
2175 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002176
Hanno Becker3307b532017-12-27 21:37:21 +00002177 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002181 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002184 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002185 {
2186 /*
2187 * Save IV in SSL3 and TLS1
2188 */
Hanno Becker3307b532017-12-27 21:37:21 +00002189 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2190 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 }
Hanno Becker3307b532017-12-27 21:37:21 +00002192 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002193#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002194 {
2195 data -= transform->ivlen;
2196 rec->data_offset -= transform->ivlen;
2197 rec->data_len += transform->ivlen;
2198 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002201 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002202 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002203 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2204
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002205 /*
2206 * MAC(MAC_write_key, seq_num +
2207 * TLSCipherText.type +
2208 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002209 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002210 * IV + // except for TLS 1.0
2211 * ENC(content + padding + padding_length));
2212 */
Hanno Becker3307b532017-12-27 21:37:21 +00002213
2214 if( post_avail < transform->maclen)
2215 {
2216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2217 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2218 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002219
Hanno Beckere83efe62019-04-29 13:52:53 +01002220 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002223 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002224 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002225
Hanno Becker3307b532017-12-27 21:37:21 +00002226 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002227 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002228 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2229 data, rec->data_len );
2230 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2231 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002232
Hanno Becker3307b532017-12-27 21:37:21 +00002233 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234
Hanno Becker3307b532017-12-27 21:37:21 +00002235 rec->data_len += transform->maclen;
2236 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002237 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002238 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002241 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002243 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002249 /* Make extra sure authentication was performed, exactly once */
2250 if( auth_done != 1 )
2251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2253 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002254 }
2255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
2258 return( 0 );
2259}
2260
Hanno Becker611a83b2018-01-03 14:27:32 +00002261int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2262 mbedtls_ssl_transform *transform,
2263 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002265 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002267 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002268#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002269 size_t padlen = 0, correct = 1;
2270#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002271 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002272 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002273 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002274
Hanno Becker611a83b2018-01-03 14:27:32 +00002275#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002276 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002277 ((void) ssl);
2278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002281 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002282 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2285 }
2286 if( rec == NULL ||
2287 rec->buf == NULL ||
2288 rec->buf_len < rec->data_offset ||
2289 rec->buf_len - rec->data_offset < rec->data_len )
2290 {
2291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002293 }
2294
Hanno Becker4c6876b2017-12-27 21:28:58 +00002295 data = rec->buf + rec->data_offset;
2296 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Hanno Beckera5a2b082019-05-15 14:03:01 +01002298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002299 /*
2300 * Match record's CID with incoming CID.
2301 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002302 if( rec->cid_len != transform->in_cid_len ||
2303 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2304 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002305 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002306 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002307#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2310 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002311 {
2312 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002313 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2314 transform->iv_dec,
2315 transform->ivlen,
2316 data, rec->data_len,
2317 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002320 return( ret );
2321 }
2322
Hanno Becker4c6876b2017-12-27 21:28:58 +00002323 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002327 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 }
Paul Bakker68884e32013-01-07 18:20:04 +01002329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002331#if defined(MBEDTLS_GCM_C) || \
2332 defined(MBEDTLS_CCM_C) || \
2333 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 mode == MBEDTLS_MODE_CCM ||
2336 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002337 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002340
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002341 /*
2342 * Compute and update sizes
2343 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002344 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002347 "+ taglen (%d)", rec->data_len,
2348 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002350 }
Paul Bakker68884e32013-01-07 18:20:04 +01002351
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002352 /*
2353 * Prepare IV
2354 */
2355 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2356 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002357 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002358 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002359 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002360
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002361 }
2362 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2363 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002364 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002365 unsigned char i;
2366
2367 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2368
2369 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002370 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002371 }
2372 else
2373 {
2374 /* Reminder if we ever add an AEAD mode with a different size */
2375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2377 }
2378
Hanno Becker4c6876b2017-12-27 21:28:58 +00002379 data += explicit_iv_len;
2380 rec->data_offset += explicit_iv_len;
2381 rec->data_len -= explicit_iv_len + transform->taglen;
2382
Hanno Beckere83efe62019-04-29 13:52:53 +01002383 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002384 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002385 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386
2387 memcpy( transform->iv_dec + transform->fixed_ivlen,
2388 data - explicit_iv_len, explicit_iv_len );
2389
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002390 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002391 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002392 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002393
Paul Bakker68884e32013-01-07 18:20:04 +01002394
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002395 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002396 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002397 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002398 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2399 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401 data, rec->data_len,
2402 data, &olen,
2403 data + rec->data_len,
2404 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2409 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002410
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 return( ret );
2412 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002413 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002414
Hanno Becker4c6876b2017-12-27 21:28:58 +00002415 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002419 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2423#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002424 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002427 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002428
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 /*
Paul Bakker45829992013-01-03 14:52:21 +01002430 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002433 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2434 {
2435 /* The ciphertext is prefixed with the CBC IV. */
2436 minlen += transform->ivlen;
2437 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002438#endif
Paul Bakker45829992013-01-03 14:52:21 +01002439
Hanno Becker4c6876b2017-12-27 21:28:58 +00002440 /* Size considerations:
2441 *
2442 * - The CBC cipher text must not be empty and hence
2443 * at least of size transform->ivlen.
2444 *
2445 * Together with the potential IV-prefix, this explains
2446 * the first of the two checks below.
2447 *
2448 * - The record must contain a MAC, either in plain or
2449 * encrypted, depending on whether Encrypt-then-MAC
2450 * is used or not.
2451 * - If it is, the message contains the IV-prefix,
2452 * the CBC ciphertext, and the MAC.
2453 * - If it is not, the padded plaintext, and hence
2454 * the CBC ciphertext, has at least length maclen + 1
2455 * because there is at least the padding length byte.
2456 *
2457 * As the CBC ciphertext is not empty, both cases give the
2458 * lower bound minlen + maclen + 1 on the record size, which
2459 * we test for in the second check below.
2460 */
2461 if( rec->data_len < minlen + transform->ivlen ||
2462 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002465 "+ 1 ) ( + expl IV )", rec->data_len,
2466 transform->ivlen,
2467 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002469 }
2470
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 /*
2472 * Authenticate before decrypt if enabled
2473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002475 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002476 {
Hanno Becker992b6872017-11-09 18:57:39 +00002477 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002480
Hanno Becker4c6876b2017-12-27 21:28:58 +00002481 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2482 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002483
Hanno Beckere83efe62019-04-29 13:52:53 +01002484 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002485
Hanno Beckere83efe62019-04-29 13:52:53 +01002486 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2487 add_data_len );
2488 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2489 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2491 data, rec->data_len );
2492 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2493 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002494
Hanno Becker4c6876b2017-12-27 21:28:58 +00002495 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2496 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002497 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002498 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Hanno Becker4c6876b2017-12-27 21:28:58 +00002500 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2501 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002506 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002509
2510 /*
2511 * Check length sanity
2512 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002513 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002516 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002518 }
2519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002521 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002522 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002523 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002524 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002525 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002526 /* This is safe because data_len >= minlen + maclen + 1 initially,
2527 * and at this point we have at most subtracted maclen (note that
2528 * minlen == transform->ivlen here). */
2529 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002530
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 data += transform->ivlen;
2532 rec->data_offset += transform->ivlen;
2533 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002536
Hanno Becker4c6876b2017-12-27 21:28:58 +00002537 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2538 transform->iv_dec, transform->ivlen,
2539 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002542 return( ret );
2543 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002544
Hanno Becker4c6876b2017-12-27 21:28:58 +00002545 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002553 {
2554 /*
2555 * Save IV in SSL3 and TLS1
2556 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002557 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2558 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002560#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Hanno Becker4c6876b2017-12-27 21:28:58 +00002562 /* Safe since data_len >= minlen + maclen + 1, so after having
2563 * subtracted at most minlen and maclen up to this point,
2564 * data_len > 0. */
2565 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002566
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( auth_done == 1 )
2568 {
2569 correct *= ( rec->data_len >= padlen + 1 );
2570 padlen *= ( rec->data_len >= padlen + 1 );
2571 }
2572 else
Paul Bakker45829992013-01-03 14:52:21 +01002573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002575 if( rec->data_len < transform->maclen + padlen + 1 )
2576 {
2577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2578 rec->data_len,
2579 transform->maclen,
2580 padlen + 1 ) );
2581 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002582#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583
2584 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2585 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Hanno Becker4c6876b2017-12-27 21:28:58 +00002588 padlen++;
2589
2590 /* Regardless of the validity of the padding,
2591 * we have data_len >= padlen here. */
2592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002594 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002596 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_DEBUG_ALL)
2599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002600 "should be no more than %d",
2601 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002602#endif
Paul Bakker45829992013-01-03 14:52:21 +01002603 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605 }
2606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2608#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2609 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002610 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002612 /* The padding check involves a series of up to 256
2613 * consecutive memory reads at the end of the record
2614 * plaintext buffer. In order to hide the length and
2615 * validity of the padding, always perform exactly
2616 * `min(256,plaintext_len)` reads (but take into account
2617 * only the last `padlen` bytes for the padding check). */
2618 size_t pad_count = 0;
2619 size_t real_count = 0;
2620 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002621
Hanno Becker4c6876b2017-12-27 21:28:58 +00002622 /* Index of first padding byte; it has been ensured above
2623 * that the subtraction is safe. */
2624 size_t const padding_idx = rec->data_len - padlen;
2625 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2626 size_t const start_idx = rec->data_len - num_checks;
2627 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002628
Hanno Becker4c6876b2017-12-27 21:28:58 +00002629 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002630 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002631 real_count |= ( idx >= padding_idx );
2632 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002633 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002634 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002637 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002639#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002640 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002642 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2644 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002648 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002649
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 /* If the padding was found to be invalid, padlen == 0
2651 * and the subtraction is safe. If the padding was found valid,
2652 * padlen hasn't been changed and the previous assertion
2653 * data_len >= padlen still holds. */
2654 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002656 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002658 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002664#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002666 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002667#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
2669 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670 * Authenticate if not done yet.
2671 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002673#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002674 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002675 {
Hanno Becker992b6872017-11-09 18:57:39 +00002676 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002677
Hanno Becker4c6876b2017-12-27 21:28:58 +00002678 /* If the initial value of padlen was such that
2679 * data_len < maclen + padlen + 1, then padlen
2680 * got reset to 1, and the initial check
2681 * data_len >= minlen + maclen + 1
2682 * guarantees that at this point we still
2683 * have at least data_len >= maclen.
2684 *
2685 * If the initial value of padlen was such that
2686 * data_len >= maclen + padlen + 1, then we have
2687 * subtracted either padlen + 1 (if the padding was correct)
2688 * or 0 (if the padding was incorrect) since then,
2689 * hence data_len >= maclen in any case.
2690 */
2691 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Hanno Beckere83efe62019-04-29 13:52:53 +01002693 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002696 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002697 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002698 ssl_mac( &transform->md_ctx_dec,
2699 transform->mac_dec,
2700 data, rec->data_len,
2701 rec->ctr, rec->type,
2702 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002703 }
2704 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2706#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2707 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002708 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002709 {
2710 /*
2711 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002712 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002713 *
2714 * Known timing attacks:
2715 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2716 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002717 * To compensate for different timings for the MAC calculation
2718 * depending on how much padding was removed (which is determined
2719 * by padlen), process extra_run more blocks through the hash
2720 * function.
2721 *
2722 * The formula in the paper is
2723 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2724 * where L1 is the size of the header plus the decrypted message
2725 * plus CBC padding and L2 is the size of the header plus the
2726 * decrypted message. This is for an underlying hash function
2727 * with 64-byte blocks.
2728 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2729 * correctly. We round down instead of up, so -56 is the correct
2730 * value for our calculations instead of -55.
2731 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002732 * Repeat the formula rather than defining a block_size variable.
2733 * This avoids requiring division by a variable at runtime
2734 * (which would be marginally less efficient and would require
2735 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002736 */
2737 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002738 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002739
2740 /*
2741 * The next two sizes are the minimum and maximum values of
2742 * in_msglen over all padlen values.
2743 *
2744 * They're independent of padlen, since we previously did
2745 * in_msglen -= padlen.
2746 *
2747 * Note that max_len + maclen is never more than the buffer
2748 * length, as we previously did in_msglen -= maclen too.
2749 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002750 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002751 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2752
Hanno Becker4c6876b2017-12-27 21:28:58 +00002753 memset( tmp, 0, sizeof( tmp ) );
2754
2755 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002757#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2758 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002759 case MBEDTLS_MD_MD5:
2760 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002762 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002763 extra_run =
2764 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2765 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002766 break;
2767#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002768#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002769 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002770 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002771 extra_run =
2772 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2773 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002774 break;
2775#endif
2776 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2779 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002780
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002781 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002782
Hanno Beckere83efe62019-04-29 13:52:53 +01002783 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2784 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002785 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2786 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002787 /* Make sure we access everything even when padlen > 0. This
2788 * makes the synchronisation requirements for just-in-time
2789 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002790 ssl_read_memory( data + rec->data_len, padlen );
2791 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002792
2793 /* Call mbedtls_md_process at least once due to cache attacks
2794 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002795 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002796 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002797
Hanno Becker4c6876b2017-12-27 21:28:58 +00002798 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002799
2800 /* Make sure we access all the memory that could contain the MAC,
2801 * before we check it in the next code block. This makes the
2802 * synchronisation requirements for just-in-time Prime+Probe
2803 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002804 ssl_read_memory( data + min_len,
2805 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002806 }
2807 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2809 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002815#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002816 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2817 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002818#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2821 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#if defined(MBEDTLS_SSL_DEBUG_ALL)
2824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002825#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002826 correct = 0;
2827 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002828 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002829 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002830
2831 /*
2832 * Finally check the correct flag
2833 */
2834 if( correct == 0 )
2835 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002836#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837
2838 /* Make extra sure authentication was performed, exactly once */
2839 if( auth_done != 1 )
2840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002843 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Hanno Beckera5a2b082019-05-15 14:03:01 +01002845#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002846 if( rec->cid_len != 0 )
2847 {
2848 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2849 &rec->type );
2850 if( ret != 0 )
2851 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2852 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002853#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
2857 return( 0 );
2858}
2859
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002860#undef MAC_NONE
2861#undef MAC_PLAINTEXT
2862#undef MAC_CIPHERTEXT
2863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002865/*
2866 * Compression/decompression functions
2867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002869{
2870 int ret;
2871 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002872 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002873 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002874 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002877
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002878 if( len_pre == 0 )
2879 return( 0 );
2880
Paul Bakker2770fbd2012-07-03 13:30:23 +00002881 memcpy( msg_pre, ssl->out_msg, len_pre );
2882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002884 ssl->out_msglen ) );
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002887 ssl->out_msg, ssl->out_msglen );
2888
Paul Bakker48916f92012-09-16 19:57:18 +00002889 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2890 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2891 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002892 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893
Paul Bakker48916f92012-09-16 19:57:18 +00002894 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002895 if( ret != Z_OK )
2896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2898 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 }
2900
Angus Grattond8213d02016-05-25 20:56:48 +10002901 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002902 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after 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, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908 ssl->out_msg, ssl->out_msglen );
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911
2912 return( 0 );
2913}
2914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002916{
2917 int ret;
2918 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002919 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002921 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002925 if( len_pre == 0 )
2926 return( 0 );
2927
Paul Bakker2770fbd2012-07-03 13:30:23 +00002928 memcpy( msg_pre, ssl->in_msg, len_pre );
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931 ssl->in_msglen ) );
2932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002934 ssl->in_msg, ssl->in_msglen );
2935
Paul Bakker48916f92012-09-16 19:57:18 +00002936 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2937 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2938 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002939 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002940 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002941
Paul Bakker48916f92012-09-16 19:57:18 +00002942 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 if( ret != Z_OK )
2944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2946 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947 }
2948
Angus Grattond8213d02016-05-25 20:56:48 +10002949 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002950 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953 ssl->in_msglen ) );
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956 ssl->in_msg, ssl->in_msglen );
2957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002959
2960 return( 0 );
2961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2965static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#if defined(MBEDTLS_SSL_PROTO_DTLS)
2968static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002969{
2970 /* If renegotiation is not enforced, retransmit until we would reach max
2971 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002972 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002973 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002974 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002975 unsigned char doublings = 1;
2976
2977 while( ratio != 0 )
2978 {
2979 ++doublings;
2980 ratio >>= 1;
2981 }
2982
2983 if( ++ssl->renego_records_seen > doublings )
2984 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002986 return( 0 );
2987 }
2988 }
2989
2990 return( ssl_write_hello_request( ssl ) );
2991}
2992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002994
Paul Bakker5121ce52009-01-03 21:22:43 +00002995/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002996 * Fill the input message buffer by appending data to it.
2997 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002998 *
2999 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3000 * available (from this read and/or a previous one). Otherwise, an error code
3001 * is returned (possibly EOF or WANT_READ).
3002 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003003 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3004 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3005 * since we always read a whole datagram at once.
3006 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003007 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003008 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003011{
Paul Bakker23986e52011-04-24 08:57:21 +00003012 int ret;
3013 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003017 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003020 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003022 }
3023
Angus Grattond8213d02016-05-25 20:56:48 +10003024 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003028 }
3029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003031 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003033 uint32_t timeout;
3034
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003035 /* Just to be sure */
3036 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3037 {
3038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3039 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3041 }
3042
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003043 /*
3044 * The point is, we need to always read a full datagram at once, so we
3045 * sometimes read more then requested, and handle the additional data.
3046 * It could be the rest of the current record (while fetching the
3047 * header) and/or some other records in the same datagram.
3048 */
3049
3050 /*
3051 * Move to the next record in the already read datagram if applicable
3052 */
3053 if( ssl->next_record_offset != 0 )
3054 {
3055 if( ssl->in_left < ssl->next_record_offset )
3056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003059 }
3060
3061 ssl->in_left -= ssl->next_record_offset;
3062
3063 if( ssl->in_left != 0 )
3064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003066 ssl->next_record_offset ) );
3067 memmove( ssl->in_hdr,
3068 ssl->in_hdr + ssl->next_record_offset,
3069 ssl->in_left );
3070 }
3071
3072 ssl->next_record_offset = 0;
3073 }
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003077
3078 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003079 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003080 */
3081 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003084 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003085 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003086
3087 /*
3088 * A record can't be split accross datagrams. If we need to read but
3089 * are not at the beginning of a new record, the caller did something
3090 * wrong.
3091 */
3092 if( ssl->in_left != 0 )
3093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003096 }
3097
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003098 /*
3099 * Don't even try to read if time's out already.
3100 * This avoids by-passing the timer when repeatedly receiving messages
3101 * that will end up being dropped.
3102 */
3103 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003104 {
3105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003106 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003107 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003108 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003109 {
Angus Grattond8213d02016-05-25 20:56:48 +10003110 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113 timeout = ssl->handshake->retransmit_timeout;
3114 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003115 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003118
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003119 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003120 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3121 timeout );
3122 else
3123 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126
3127 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 }
3130
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003131 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003134 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003137 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003138 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003141 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003142 }
3143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003147 return( ret );
3148 }
3149
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003150 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003153 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003155 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003156 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003159 return( ret );
3160 }
3161
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003162 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003163 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003165 }
3166
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 if( ret < 0 )
3168 return( ret );
3169
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003170 ssl->in_left = ret;
3171 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003172 MBEDTLS_SSL_TRANSPORT_ELSE
3173#endif /* MBEDTLS_SSL_PROTO_DTLS */
3174#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003177 ssl->in_left, nb_want ) );
3178
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003179 while( ssl->in_left < nb_want )
3180 {
3181 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003182
3183 if( ssl_check_timer( ssl ) != 0 )
3184 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3185 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003186 {
3187 if( ssl->f_recv_timeout != NULL )
3188 {
3189 ret = ssl->f_recv_timeout( ssl->p_bio,
3190 ssl->in_hdr + ssl->in_left, len,
3191 ssl->conf->read_timeout );
3192 }
3193 else
3194 {
3195 ret = ssl->f_recv( ssl->p_bio,
3196 ssl->in_hdr + ssl->in_left, len );
3197 }
3198 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003201 ssl->in_left, nb_want ) );
3202 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003203
3204 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003206
3207 if( ret < 0 )
3208 return( ret );
3209
mohammad160352aecb92018-03-28 23:41:40 -07003210 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003211 {
Darryl Green11999bb2018-03-13 15:22:58 +00003212 MBEDTLS_SSL_DEBUG_MSG( 1,
3213 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003214 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3216 }
3217
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003218 ssl->in_left += ret;
3219 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003221#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
3225 return( 0 );
3226}
3227
3228/*
3229 * Flush any data not yet written
3230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003232{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003233 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003234 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003238 if( ssl->f_send == NULL )
3239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003241 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003243 }
3244
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003245 /* Avoid incrementing counter if data is flushed */
3246 if( ssl->out_left == 0 )
3247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003249 return( 0 );
3250 }
3251
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 while( ssl->out_left > 0 )
3253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003255 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Hanno Becker2b1e3542018-08-06 11:19:13 +01003257 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003258 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
3262 if( ret <= 0 )
3263 return( ret );
3264
mohammad160352aecb92018-03-28 23:41:40 -07003265 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003266 {
Darryl Green11999bb2018-03-13 15:22:58 +00003267 MBEDTLS_SSL_DEBUG_MSG( 1,
3268 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003269 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3271 }
3272
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 ssl->out_left -= ret;
3274 }
3275
Hanno Becker2b1e3542018-08-06 11:19:13 +01003276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003277 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003278 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003279 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003280 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003281 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003282#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003283#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003284 {
3285 ssl->out_hdr = ssl->out_buf + 8;
3286 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003287#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003288 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291
3292 return( 0 );
3293}
3294
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003295/*
3296 * Functions to handle the DTLS retransmission state machine
3297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003299/*
3300 * Append current handshake message to current outgoing flight
3301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3306 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3307 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003308
3309 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003310 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003311 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003314 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003315 }
3316
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003317 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003321 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003322 }
3323
3324 /* Copy current handshake message with headers */
3325 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3326 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003327 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003328 msg->next = NULL;
3329
3330 /* Append to the current flight */
3331 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003332 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 else
3334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003336 while( cur->next != NULL )
3337 cur = cur->next;
3338 cur->next = msg;
3339 }
3340
Hanno Becker3b235902018-08-06 09:54:53 +01003341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342 return( 0 );
3343}
3344
3345/*
3346 * Free the current flight of handshake messages
3347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_ssl_flight_item *cur = flight;
3351 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352
3353 while( cur != NULL )
3354 {
3355 next = cur->next;
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_free( cur->p );
3358 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003359
3360 cur = next;
3361 }
3362}
3363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3365static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003366#endif
3367
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003369 * Swap transform_out and out_ctr with the alternative ones
3370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003374 unsigned char tmp_out_ctr[8];
3375
3376 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003379 return;
3380 }
3381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003383
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003384 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003385 tmp_transform = ssl->transform_out;
3386 ssl->transform_out = ssl->handshake->alt_transform_out;
3387 ssl->handshake->alt_transform_out = tmp_transform;
3388
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003389 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003390 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3391 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003392 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003393
3394 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003395 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3398 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3403 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003404 }
3405 }
3406#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407}
3408
3409/*
3410 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003411 */
3412int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3413{
3414 int ret = 0;
3415
3416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3417
3418 ret = mbedtls_ssl_flight_transmit( ssl );
3419
3420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3421
3422 return( ret );
3423}
3424
3425/*
3426 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003427 *
3428 * Need to remember the current message in case flush_output returns
3429 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003430 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003431 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003432int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003433{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003434 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003438 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003440
3441 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003442 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003443 ssl_swap_epochs( ssl );
3444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003446 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003447
3448 while( ssl->handshake->cur_msg != NULL )
3449 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003450 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003451 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003452
Hanno Beckere1dcb032018-08-17 16:47:58 +01003453 int const is_finished =
3454 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3455 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3456
Hanno Becker04da1892018-08-14 13:22:10 +01003457 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3458 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3459
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003460 /* Swap epochs before sending Finished: we can't do it after
3461 * sending ChangeCipherSpec, in case write returns WANT_READ.
3462 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003463 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003464 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003466 ssl_swap_epochs( ssl );
3467 }
3468
Hanno Becker67bc7c32018-08-06 11:33:50 +01003469 ret = ssl_get_remaining_payload_in_datagram( ssl );
3470 if( ret < 0 )
3471 return( ret );
3472 max_frag_len = (size_t) ret;
3473
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003474 /* CCS is copied as is, while HS messages may need fragmentation */
3475 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3476 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003477 if( max_frag_len == 0 )
3478 {
3479 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3480 return( ret );
3481
3482 continue;
3483 }
3484
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003485 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003486 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003487 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003488
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003489 /* Update position inside current message */
3490 ssl->handshake->cur_msg_p += cur->len;
3491 }
3492 else
3493 {
3494 const unsigned char * const p = ssl->handshake->cur_msg_p;
3495 const size_t hs_len = cur->len - 12;
3496 const size_t frag_off = p - ( cur->p + 12 );
3497 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003498 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003499
Hanno Beckere1dcb032018-08-17 16:47:58 +01003500 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003501 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003502 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003503 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003504
Hanno Becker67bc7c32018-08-06 11:33:50 +01003505 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3506 return( ret );
3507
3508 continue;
3509 }
3510 max_hs_frag_len = max_frag_len - 12;
3511
3512 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3513 max_hs_frag_len : rem_len;
3514
3515 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003516 {
3517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003518 (unsigned) cur_hs_frag_len,
3519 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003520 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003521
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003522 /* Messages are stored with handshake headers as if not fragmented,
3523 * copy beginning of headers then fill fragmentation fields.
3524 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3525 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003526
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003527 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3528 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3529 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3530
Hanno Becker67bc7c32018-08-06 11:33:50 +01003531 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3532 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3533 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003534
3535 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3536
Hanno Becker3f7b9732018-08-28 09:53:25 +01003537 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003538 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3539 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003540 ssl->out_msgtype = cur->type;
3541
3542 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003543 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003544 }
3545
3546 /* If done with the current message move to the next one if any */
3547 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3548 {
3549 if( cur->next != NULL )
3550 {
3551 ssl->handshake->cur_msg = cur->next;
3552 ssl->handshake->cur_msg_p = cur->next->p + 12;
3553 }
3554 else
3555 {
3556 ssl->handshake->cur_msg = NULL;
3557 ssl->handshake->cur_msg_p = NULL;
3558 }
3559 }
3560
3561 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003562 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003565 return( ret );
3566 }
3567 }
3568
Hanno Becker67bc7c32018-08-06 11:33:50 +01003569 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3570 return( ret );
3571
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003572 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3574 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003575 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003578 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3579 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003580
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582
3583 return( 0 );
3584}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003585
3586/*
3587 * To be called when the last message of an incoming flight is received.
3588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003590{
3591 /* We won't need to resend that one any more */
3592 ssl_flight_free( ssl->handshake->flight );
3593 ssl->handshake->flight = NULL;
3594 ssl->handshake->cur_msg = NULL;
3595
3596 /* The next incoming flight will start with this msg_seq */
3597 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3598
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003599 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003600 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003601
Hanno Becker0271f962018-08-16 13:23:47 +01003602 /* Clear future message buffering structure. */
3603 ssl_buffering_free( ssl );
3604
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003605 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003606 ssl_set_timer( ssl, 0 );
3607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3609 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003612 }
3613 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003615}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003616
3617/*
3618 * To be called when the last message of an outgoing flight is send.
3619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003621{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003622 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003623 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3626 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003629 }
3630 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003634
Paul Bakker5121ce52009-01-03 21:22:43 +00003635/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003636 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003638
3639/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003640 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003641 *
3642 * - fill in handshake headers
3643 * - update handshake checksum
3644 * - DTLS: save message for resending
3645 * - then pass to the record layer
3646 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003647 * DTLS: except for HelloRequest, messages are only queued, and will only be
3648 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003649 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003650 * Inputs:
3651 * - ssl->out_msglen: 4 + actual handshake message len
3652 * (4 is the size of handshake headers for TLS)
3653 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3654 * - ssl->out_msg + 4: the handshake message body
3655 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003656 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657 * - ssl->out_msglen: the length of the record contents
3658 * (including handshake headers but excluding record headers)
3659 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003660 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003661int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003662{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003663 int ret;
3664 const size_t hs_len = ssl->out_msglen - 4;
3665 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003666
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3668
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003669 /*
3670 * Sanity checks
3671 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003672 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003673 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3674 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003675 /* In SSLv3, the client might send a NoCertificate alert. */
3676#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3677 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3678 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3679 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3680#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3681 {
3682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3684 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003687 /* Whenever we send anything different from a
3688 * HelloRequest we should be in a handshake - double check. */
3689 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3690 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003691 ssl->handshake == NULL )
3692 {
3693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3695 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003698 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003699 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003704 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003705#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003706
Hanno Beckerb50a2532018-08-06 11:52:54 +01003707 /* Double-check that we did not exceed the bounds
3708 * of the outgoing record buffer.
3709 * This should never fail as the various message
3710 * writing functions must obey the bounds of the
3711 * outgoing record buffer, but better be safe.
3712 *
3713 * Note: We deliberately do not check for the MTU or MFL here.
3714 */
3715 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3716 {
3717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3718 "size %u, maximum %u",
3719 (unsigned) ssl->out_msglen,
3720 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3722 }
3723
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003724 /*
3725 * Fill handshake headers
3726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003729 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3730 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3731 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003733 /*
3734 * DTLS has additional fields in the Handshake layer,
3735 * between the length field and the actual payload:
3736 * uint16 message_seq;
3737 * uint24 fragment_offset;
3738 * uint24 fragment_length;
3739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003742 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003743 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003744 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003745 {
3746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3747 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003748 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003749 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3751 }
3752
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003753 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003754 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003755
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003756 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003757 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003758 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003759 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3760 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3761 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003762 }
3763 else
3764 {
3765 ssl->out_msg[4] = 0;
3766 ssl->out_msg[5] = 0;
3767 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003768
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 /* Handshake hashes are computed without fragmentation,
3770 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003771 memset( ssl->out_msg + 6, 0x00, 3 );
3772 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003773 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003775
Hanno Becker0207e532018-08-28 10:28:28 +01003776 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003777 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3778 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 }
3780
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003781 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003783 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003784 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3785 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003786 {
3787 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003790 return( ret );
3791 }
3792 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003793 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003794#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003795 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003796 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003797 {
3798 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3799 return( ret );
3800 }
3801 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003802
3803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3804
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003805 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003806}
3807
3808/*
3809 * Record layer functions
3810 */
3811
3812/*
3813 * Write current record.
3814 *
3815 * Uses:
3816 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3817 * - ssl->out_msglen: length of the record content (excl headers)
3818 * - ssl->out_msg: record content
3819 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003821{
3822 int ret, done = 0;
3823 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003824 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003825
3826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003829 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003831 {
3832 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003835 return( ret );
3836 }
3837
3838 len = ssl->out_msglen;
3839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3843 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 ret = mbedtls_ssl_hw_record_write( ssl );
3848 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3851 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003852 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003853
3854 if( ret == 0 )
3855 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003858 if( !done )
3859 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003860 unsigned i;
3861 size_t protected_record_size;
3862
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003863 /* Skip writing the record content type to after the encryption,
3864 * as it may change when using the CID extension. */
3865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003867 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003868
Hanno Becker19859472018-08-06 09:40:20 +01003869 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003870 ssl->out_len[0] = (unsigned char)( len >> 8 );
3871 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003872
Paul Bakker48916f92012-09-16 19:57:18 +00003873 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003874 {
Hanno Becker3307b532017-12-27 21:37:21 +00003875 mbedtls_record rec;
3876
3877 rec.buf = ssl->out_iv;
3878 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3879 ( ssl->out_iv - ssl->out_buf );
3880 rec.data_len = ssl->out_msglen;
3881 rec.data_offset = ssl->out_msg - rec.buf;
3882
3883 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3884 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3885 ssl->conf->transport, rec.ver );
3886 rec.type = ssl->out_msgtype;
3887
Hanno Beckera5a2b082019-05-15 14:03:01 +01003888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003889 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003890 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003891#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003892
Hanno Becker611a83b2018-01-03 14:27:32 +00003893 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003894 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003897 return( ret );
3898 }
3899
Hanno Becker3307b532017-12-27 21:37:21 +00003900 if( rec.data_offset != 0 )
3901 {
3902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3903 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3904 }
3905
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003906 /* Update the record content type and CID. */
3907 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003908#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003909 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003910#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003911 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003912 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3913 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003914 }
3915
Hanno Becker43395762019-05-03 14:46:38 +01003916 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003917
3918#if defined(MBEDTLS_SSL_PROTO_DTLS)
3919 /* In case of DTLS, double-check that we don't exceed
3920 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003921 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003922 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003923 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003924 if( ret < 0 )
3925 return( ret );
3926
3927 if( protected_record_size > (size_t) ret )
3928 {
3929 /* Should never happen */
3930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3931 }
3932 }
3933#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003934
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003935 /* Now write the potentially updated record content type. */
3936 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003939 "version = [%d:%d], msglen = %d",
3940 ssl->out_hdr[0], ssl->out_hdr[1],
3941 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003944 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003945
3946 ssl->out_left += protected_record_size;
3947 ssl->out_hdr += protected_record_size;
3948 ssl_update_out_pointers( ssl, ssl->transform_out );
3949
Hanno Becker04484622018-08-06 09:49:38 +01003950 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3951 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3952 break;
3953
3954 /* The loop goes to its end iff the counter is wrapping */
3955 if( i == ssl_ep_len( ssl ) )
3956 {
3957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3958 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003960 }
3961
Hanno Becker67bc7c32018-08-06 11:33:50 +01003962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003963 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003964 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003965 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003966 size_t remaining;
3967 ret = ssl_get_remaining_payload_in_datagram( ssl );
3968 if( ret < 0 )
3969 {
3970 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3971 ret );
3972 return( ret );
3973 }
3974
3975 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003976 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003977 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003978 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003979 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003980 else
3981 {
Hanno Becker513815a2018-08-20 11:56:09 +01003982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003983 }
3984 }
3985#endif /* MBEDTLS_SSL_PROTO_DTLS */
3986
3987 if( ( flush == SSL_FORCE_FLUSH ) &&
3988 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991 return( ret );
3992 }
3993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003995
3996 return( 0 );
3997}
3998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004000
4001static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4002{
4003 if( ssl->in_msglen < ssl->in_hslen ||
4004 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4005 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4006 {
4007 return( 1 );
4008 }
4009 return( 0 );
4010}
Hanno Becker44650b72018-08-16 12:51:11 +01004011
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004012static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004013{
4014 return( ( ssl->in_msg[9] << 16 ) |
4015 ( ssl->in_msg[10] << 8 ) |
4016 ssl->in_msg[11] );
4017}
4018
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004019static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004020{
4021 return( ( ssl->in_msg[6] << 16 ) |
4022 ( ssl->in_msg[7] << 8 ) |
4023 ssl->in_msg[8] );
4024}
4025
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004026static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004027{
4028 uint32_t msg_len, frag_off, frag_len;
4029
4030 msg_len = ssl_get_hs_total_len( ssl );
4031 frag_off = ssl_get_hs_frag_off( ssl );
4032 frag_len = ssl_get_hs_frag_len( ssl );
4033
4034 if( frag_off > msg_len )
4035 return( -1 );
4036
4037 if( frag_len > msg_len - frag_off )
4038 return( -1 );
4039
4040 if( frag_len + 12 > ssl->in_msglen )
4041 return( -1 );
4042
4043 return( 0 );
4044}
4045
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004046/*
4047 * Mark bits in bitmask (used for DTLS HS reassembly)
4048 */
4049static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4050{
4051 unsigned int start_bits, end_bits;
4052
4053 start_bits = 8 - ( offset % 8 );
4054 if( start_bits != 8 )
4055 {
4056 size_t first_byte_idx = offset / 8;
4057
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004058 /* Special case */
4059 if( len <= start_bits )
4060 {
4061 for( ; len != 0; len-- )
4062 mask[first_byte_idx] |= 1 << ( start_bits - len );
4063
4064 /* Avoid potential issues with offset or len becoming invalid */
4065 return;
4066 }
4067
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004068 offset += start_bits; /* Now offset % 8 == 0 */
4069 len -= start_bits;
4070
4071 for( ; start_bits != 0; start_bits-- )
4072 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4073 }
4074
4075 end_bits = len % 8;
4076 if( end_bits != 0 )
4077 {
4078 size_t last_byte_idx = ( offset + len ) / 8;
4079
4080 len -= end_bits; /* Now len % 8 == 0 */
4081
4082 for( ; end_bits != 0; end_bits-- )
4083 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4084 }
4085
4086 memset( mask + offset / 8, 0xFF, len / 8 );
4087}
4088
4089/*
4090 * Check that bitmask is full
4091 */
4092static int ssl_bitmask_check( unsigned char *mask, size_t len )
4093{
4094 size_t i;
4095
4096 for( i = 0; i < len / 8; i++ )
4097 if( mask[i] != 0xFF )
4098 return( -1 );
4099
4100 for( i = 0; i < len % 8; i++ )
4101 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4102 return( -1 );
4103
4104 return( 0 );
4105}
4106
Hanno Becker56e205e2018-08-16 09:06:12 +01004107/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004108static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004109 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004110{
Hanno Becker56e205e2018-08-16 09:06:12 +01004111 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004112
Hanno Becker56e205e2018-08-16 09:06:12 +01004113 alloc_len = 12; /* Handshake header */
4114 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004115
Hanno Beckerd07df862018-08-16 09:14:58 +01004116 if( add_bitmap )
4117 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004118
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004119 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004120}
Hanno Becker56e205e2018-08-16 09:06:12 +01004121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004123
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004124static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004125{
4126 return( ( ssl->in_msg[1] << 16 ) |
4127 ( ssl->in_msg[2] << 8 ) |
4128 ssl->in_msg[3] );
4129}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004130
Simon Butcher99000142016-10-13 17:21:01 +01004131int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004136 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004138 }
4139
Hanno Becker12555c62018-08-16 12:47:53 +01004140 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004143 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004144 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004146#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004147 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004148 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004149 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004150 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004151
Hanno Becker44650b72018-08-16 12:51:11 +01004152 if( ssl_check_hs_header( ssl ) != 0 )
4153 {
4154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4155 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4156 }
4157
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004158 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004159 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4160 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4161 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4162 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004163 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004164 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4165 {
4166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4167 recv_msg_seq,
4168 ssl->handshake->in_msg_seq ) );
4169 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4170 }
4171
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004172 /* Retransmit only on last message from previous flight, to avoid
4173 * too many retransmissions.
4174 * Besides, No sane server ever retransmits HelloVerifyRequest */
4175 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004179 "message_seq = %d, start_of_flight = %d",
4180 recv_msg_seq,
4181 ssl->handshake->in_flight_start_seq ) );
4182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004186 return( ret );
4187 }
4188 }
4189 else
4190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004192 "message_seq = %d, expected = %d",
4193 recv_msg_seq,
4194 ssl->handshake->in_msg_seq ) );
4195 }
4196
Hanno Becker90333da2017-10-10 11:27:13 +01004197 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004198 }
4199 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004200
Hanno Becker6d97ef52018-08-16 13:09:04 +01004201 /* Message reassembly is handled alongside buffering of future
4202 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004203 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004204 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004205 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004208 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004209 }
4210 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004211 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004213#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004214 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004215 /* With TLS we don't handle fragmentation (for now) */
4216 if( ssl->in_msglen < ssl->in_hslen )
4217 {
4218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4219 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4220 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004221 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004222#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004223
Simon Butcher99000142016-10-13 17:21:01 +01004224 return( 0 );
4225}
4226
4227void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4228{
Hanno Becker0271f962018-08-16 13:23:47 +01004229 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004230
Hanno Becker0271f962018-08-16 13:23:47 +01004231 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004232 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004233 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004234 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004235
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004236 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004238 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004239 ssl->handshake != NULL )
4240 {
Hanno Becker0271f962018-08-16 13:23:47 +01004241 unsigned offset;
4242 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004243
Hanno Becker0271f962018-08-16 13:23:47 +01004244 /* Increment handshake sequence number */
4245 hs->in_msg_seq++;
4246
4247 /*
4248 * Clear up handshake buffering and reassembly structure.
4249 */
4250
4251 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004252 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004253
4254 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004255 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4256 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004257 offset++, hs_buf++ )
4258 {
4259 *hs_buf = *(hs_buf + 1);
4260 }
4261
4262 /* Create a fresh last entry */
4263 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004264 }
4265#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004266}
4267
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004268/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004269 * DTLS anti-replay: RFC 6347 4.1.2.6
4270 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004271 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4272 * Bit n is set iff record number in_window_top - n has been seen.
4273 *
4274 * Usually, in_window_top is the last record number seen and the lsb of
4275 * in_window is set. The only exception is the initial state (record number 0
4276 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4279static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004280{
4281 ssl->in_window_top = 0;
4282 ssl->in_window = 0;
4283}
4284
4285static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4286{
4287 return( ( (uint64_t) buf[0] << 40 ) |
4288 ( (uint64_t) buf[1] << 32 ) |
4289 ( (uint64_t) buf[2] << 24 ) |
4290 ( (uint64_t) buf[3] << 16 ) |
4291 ( (uint64_t) buf[4] << 8 ) |
4292 ( (uint64_t) buf[5] ) );
4293}
4294
4295/*
4296 * Return 0 if sequence number is acceptable, -1 otherwise
4297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004299{
4300 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4301 uint64_t bit;
4302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004303 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004304 return( 0 );
4305
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004306 if( rec_seqnum > ssl->in_window_top )
4307 return( 0 );
4308
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004309 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004310
4311 if( bit >= 64 )
4312 return( -1 );
4313
4314 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4315 return( -1 );
4316
4317 return( 0 );
4318}
4319
4320/*
4321 * Update replay window on new validated record
4322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004324{
4325 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4326
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004327 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004328 return;
4329
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004330 if( rec_seqnum > ssl->in_window_top )
4331 {
4332 /* Update window_top and the contents of the window */
4333 uint64_t shift = rec_seqnum - ssl->in_window_top;
4334
4335 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004336 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004337 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004338 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004340 ssl->in_window |= 1;
4341 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004342
4343 ssl->in_window_top = rec_seqnum;
4344 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345 else
4346 {
4347 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004348 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004349
4350 if( bit < 64 ) /* Always true, but be extra sure */
4351 ssl->in_window |= (uint64_t) 1 << bit;
4352 }
4353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004355
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004356#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004357/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004358static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4359
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004360/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004361 * Without any SSL context, check if a datagram looks like a ClientHello with
4362 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004363 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004364 *
4365 * - if cookie is valid, return 0
4366 * - if ClientHello looks superficially valid but cookie is not,
4367 * fill obuf and set olen, then
4368 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4369 * - otherwise return a specific error code
4370 */
4371static int ssl_check_dtls_clihlo_cookie(
4372 mbedtls_ssl_cookie_write_t *f_cookie_write,
4373 mbedtls_ssl_cookie_check_t *f_cookie_check,
4374 void *p_cookie,
4375 const unsigned char *cli_id, size_t cli_id_len,
4376 const unsigned char *in, size_t in_len,
4377 unsigned char *obuf, size_t buf_len, size_t *olen )
4378{
4379 size_t sid_len, cookie_len;
4380 unsigned char *p;
4381
4382 if( f_cookie_write == NULL || f_cookie_check == NULL )
4383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4384
4385 /*
4386 * Structure of ClientHello with record and handshake headers,
4387 * and expected values. We don't need to check a lot, more checks will be
4388 * done when actually parsing the ClientHello - skipping those checks
4389 * avoids code duplication and does not make cookie forging any easier.
4390 *
4391 * 0-0 ContentType type; copied, must be handshake
4392 * 1-2 ProtocolVersion version; copied
4393 * 3-4 uint16 epoch; copied, must be 0
4394 * 5-10 uint48 sequence_number; copied
4395 * 11-12 uint16 length; (ignored)
4396 *
4397 * 13-13 HandshakeType msg_type; (ignored)
4398 * 14-16 uint24 length; (ignored)
4399 * 17-18 uint16 message_seq; copied
4400 * 19-21 uint24 fragment_offset; copied, must be 0
4401 * 22-24 uint24 fragment_length; (ignored)
4402 *
4403 * 25-26 ProtocolVersion client_version; (ignored)
4404 * 27-58 Random random; (ignored)
4405 * 59-xx SessionID session_id; 1 byte len + sid_len content
4406 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4407 * ...
4408 *
4409 * Minimum length is 61 bytes.
4410 */
4411 if( in_len < 61 ||
4412 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4413 in[3] != 0 || in[4] != 0 ||
4414 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4415 {
4416 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4417 }
4418
4419 sid_len = in[59];
4420 if( sid_len > in_len - 61 )
4421 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4422
4423 cookie_len = in[60 + sid_len];
4424 if( cookie_len > in_len - 60 )
4425 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4426
4427 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4428 cli_id, cli_id_len ) == 0 )
4429 {
4430 /* Valid cookie */
4431 return( 0 );
4432 }
4433
4434 /*
4435 * If we get here, we've got an invalid cookie, let's prepare HVR.
4436 *
4437 * 0-0 ContentType type; copied
4438 * 1-2 ProtocolVersion version; copied
4439 * 3-4 uint16 epoch; copied
4440 * 5-10 uint48 sequence_number; copied
4441 * 11-12 uint16 length; olen - 13
4442 *
4443 * 13-13 HandshakeType msg_type; hello_verify_request
4444 * 14-16 uint24 length; olen - 25
4445 * 17-18 uint16 message_seq; copied
4446 * 19-21 uint24 fragment_offset; copied
4447 * 22-24 uint24 fragment_length; olen - 25
4448 *
4449 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4450 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4451 *
4452 * Minimum length is 28.
4453 */
4454 if( buf_len < 28 )
4455 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4456
4457 /* Copy most fields and adapt others */
4458 memcpy( obuf, in, 25 );
4459 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4460 obuf[25] = 0xfe;
4461 obuf[26] = 0xff;
4462
4463 /* Generate and write actual cookie */
4464 p = obuf + 28;
4465 if( f_cookie_write( p_cookie,
4466 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4467 {
4468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4469 }
4470
4471 *olen = p - obuf;
4472
4473 /* Go back and fill length fields */
4474 obuf[27] = (unsigned char)( *olen - 28 );
4475
4476 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4477 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4478 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4479
4480 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4481 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4482
4483 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4484}
4485
4486/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004487 * Handle possible client reconnect with the same UDP quadruplet
4488 * (RFC 6347 Section 4.2.8).
4489 *
4490 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4491 * that looks like a ClientHello.
4492 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004493 * - if the input looks like a ClientHello without cookies,
4494 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004495 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004496 * - if the input looks like a ClientHello with a valid cookie,
4497 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004498 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004499 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004500 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004501 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004502 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4503 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004504 */
4505static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4506{
4507 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004508 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 ret = ssl_check_dtls_clihlo_cookie(
4511 ssl->conf->f_cookie_write,
4512 ssl->conf->f_cookie_check,
4513 ssl->conf->p_cookie,
4514 ssl->cli_id, ssl->cli_id_len,
4515 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004516 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004517
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004518 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4519
4520 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004521 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004522 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 * If the error is permanent we'll catch it later,
4524 * if it's not, then hopefully it'll work next time. */
4525 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4526
4527 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004528 }
4529
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004530 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004531 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004532 /* Got a valid cookie, partially reset context */
4533 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4534 {
4535 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4536 return( ret );
4537 }
4538
4539 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004540 }
4541
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004542 return( ret );
4543}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004544#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004545
Hanno Becker46483f12019-05-03 13:25:54 +01004546static int ssl_check_record_type( uint8_t record_type )
4547{
4548 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4549 record_type != MBEDTLS_SSL_MSG_ALERT &&
4550 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4551 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4552 {
4553 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4554 }
4555
4556 return( 0 );
4557}
4558
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004559/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004560 * ContentType type;
4561 * ProtocolVersion version;
4562 * uint16 epoch; // DTLS only
4563 * uint48 sequence_number; // DTLS only
4564 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004565 *
4566 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004567 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004568 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4569 *
4570 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004571 * 1. proceed with the record if this function returns 0
4572 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4573 * 3. return CLIENT_RECONNECT if this function return that value
4574 * 4. drop the whole datagram if this function returns anything else.
4575 * Point 2 is needed when the peer is resending, and we have already received
4576 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004579{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004580 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004581 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004582
Hanno Becker8b09b732019-05-08 12:03:28 +01004583 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004584
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004586 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004587
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004588 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004589#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004590 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004591 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4592 ssl->conf->cid_len != 0 )
4593 {
4594 /* Shift pointers to account for record header including CID
4595 * struct {
4596 * ContentType special_type = tls12_cid;
4597 * ProtocolVersion version;
4598 * uint16 epoch;
4599 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004600 * opaque cid[cid_length]; // Additional field compared to
4601 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004602 * uint16 length;
4603 * opaque enc_content[DTLSCiphertext.length];
4604 * } DTLSCiphertext;
4605 */
4606
4607 /* So far, we only support static CID lengths
4608 * fixed in the configuration. */
4609 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4610 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4611 }
4612 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004613#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004614 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004617
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004618#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004619 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004620 * Section 4.1.2.7, that is, send alert only with TLS */
4621 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4622 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004623 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4624 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004625 }
4626#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004629 }
4630
4631 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004632 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4635 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 }
4637
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004638 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4641 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004642 }
4643
Hanno Becker8b09b732019-05-08 12:03:28 +01004644 /* Now that the total length of the record header is known, ensure
4645 * that the current datagram is large enough to hold it.
4646 * This would fail, for example, if we received a datagram of
4647 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4648 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4649 if( ret != 0 )
4650 {
4651 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4652 return( ret );
4653 }
4654 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4655
4656 /* Parse and validate record length
4657 * This must happen after the CID parsing because
4658 * its position in the record header depends on
4659 * the presence of a CID. */
4660
4661 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004662 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004663 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4666 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004667 }
4668
Hanno Becker8b09b732019-05-08 12:03:28 +01004669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004670 "version = [%d:%d], msglen = %d",
4671 ssl->in_msgtype,
4672 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004673
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004674 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004675 * DTLS-related tests.
4676 * Check epoch before checking length constraint because
4677 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4678 * message gets duplicated before the corresponding Finished message,
4679 * the second ChangeCipherSpec should be discarded because it belongs
4680 * to an old epoch, but not because its length is shorter than
4681 * the minimum record length for packets using the new record transform.
4682 * Note that these two kinds of failures are handled differently,
4683 * as an unexpected record is silently skipped but an invalid
4684 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004685 */
4686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004687 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004688 {
4689 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4690
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004691 /* Check epoch (and sequence number) with DTLS */
4692 if( rec_epoch != ssl->in_epoch )
4693 {
4694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4695 "expected %d, received %d",
4696 ssl->in_epoch, rec_epoch ) );
4697
4698#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4699 /*
4700 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4701 * access the first byte of record content (handshake type), as we
4702 * have an active transform (possibly iv_len != 0), so use the
4703 * fact that the record header len is 13 instead.
4704 */
4705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4706 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4707 rec_epoch == 0 &&
4708 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4709 ssl->in_left > 13 &&
4710 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4711 {
4712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4713 "from the same port" ) );
4714 return( ssl_handle_possible_reconnect( ssl ) );
4715 }
4716 else
4717#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004718 {
4719 /* Consider buffering the record. */
4720 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4723 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4724 }
4725
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004726 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004727 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004728 }
4729
4730#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4731 /* Replay detection only works for the current epoch */
4732 if( rec_epoch == ssl->in_epoch &&
4733 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4734 {
4735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4736 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4737 }
4738#endif
4739 }
4740#endif /* MBEDTLS_SSL_PROTO_DTLS */
4741
Hanno Becker52c6dc62017-05-26 16:07:36 +01004742
4743 /* Check length against bounds of the current transform and version */
4744 if( ssl->transform_in == NULL )
4745 {
4746 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004747 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004748 {
4749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4750 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4751 }
4752 }
4753 else
4754 {
4755 if( ssl->in_msglen < ssl->transform_in->minlen )
4756 {
4757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4758 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4759 }
4760
4761#if defined(MBEDTLS_SSL_PROTO_SSL3)
4762 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004763 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004764 {
4765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4766 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4767 }
4768#endif
4769#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4770 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4771 /*
4772 * TLS encrypted messages can have up to 256 bytes of padding
4773 */
4774 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4775 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004776 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004777 {
4778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4779 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4780 }
4781#endif
4782 }
4783
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004784 return( 0 );
4785}
Paul Bakker5121ce52009-01-03 21:22:43 +00004786
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004787/*
4788 * If applicable, decrypt (and decompress) record content
4789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004791{
4792 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004795 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4798 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802 ret = mbedtls_ssl_hw_record_read( ssl );
4803 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4806 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004807 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004808
4809 if( ret == 0 )
4810 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004811 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004813 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004815 mbedtls_record rec;
4816
4817 rec.buf = ssl->in_iv;
4818 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4819 - ( ssl->in_iv - ssl->in_buf );
4820 rec.data_len = ssl->in_msglen;
4821 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004822#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004823 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004824 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004825#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004826
4827 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4828 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4829 ssl->conf->transport, rec.ver );
4830 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004831 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4832 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004835
Hanno Beckera5a2b082019-05-15 14:03:01 +01004836#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004837 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4838 ssl->conf->ignore_unexpected_cid
4839 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4840 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004842 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004843 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004844#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004845
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 return( ret );
4847 }
4848
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004849 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004850 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004851 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4852 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004854
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004855 /* The record content type may change during decryption,
4856 * so re-read it. */
4857 ssl->in_msgtype = rec.type;
4858 /* Also update the input buffer, because unfortunately
4859 * the server-side ssl_parse_client_hello() reparses the
4860 * record header when receiving a ClientHello initiating
4861 * a renegotiation. */
4862 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004863 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004864 ssl->in_msglen = rec.data_len;
4865 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4866 ssl->in_len[1] = (unsigned char)( rec.data_len );
4867
Paul Bakker5121ce52009-01-03 21:22:43 +00004868 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4869 ssl->in_msg, ssl->in_msglen );
4870
Hanno Beckera5a2b082019-05-15 14:03:01 +01004871#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004872 /* We have already checked the record content type
4873 * in ssl_parse_record_header(), failing or silently
4874 * dropping the record in the case of an unknown type.
4875 *
4876 * Since with the use of CIDs, the record content type
4877 * might change during decryption, re-check the record
4878 * content type, but treat a failure as fatal this time. */
4879 if( ssl_check_record_type( ssl->in_msgtype ) )
4880 {
4881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4882 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4883 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004884#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004885
Angus Grattond8213d02016-05-25 20:56:48 +10004886 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4889 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004890 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004891 else if( ssl->in_msglen == 0 )
4892 {
4893#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4894 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4895 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4896 {
4897 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4899 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4900 }
4901#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4902
4903 ssl->nb_zero++;
4904
4905 /*
4906 * Three or more empty messages may be a DoS attack
4907 * (excessive CPU consumption).
4908 */
4909 if( ssl->nb_zero > 3 )
4910 {
4911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004912 "messages, possible DoS attack" ) );
4913 /* Treat the records as if they were not properly authenticated,
4914 * thereby failing the connection if we see more than allowed
4915 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004916 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4917 }
4918 }
4919 else
4920 ssl->nb_zero = 0;
4921
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004922 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4923#if defined(MBEDTLS_SSL_PROTO_TLS)
4924 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004925 {
4926 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004927 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004928 if( ++ssl->in_ctr[i - 1] != 0 )
4929 break;
4930
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004931 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004932 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004933 {
4934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4935 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4936 }
4937 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004938#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004939
Paul Bakker5121ce52009-01-03 21:22:43 +00004940 }
4941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004943 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004945 {
4946 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004948 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004949 return( ret );
4950 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004951 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004955 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004958 }
4959#endif
4960
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004961 return( 0 );
4962}
4963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004965
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004966/*
4967 * Read a record.
4968 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004969 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4970 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4971 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004972 */
Hanno Becker1097b342018-08-15 14:09:41 +01004973
4974/* Helper functions for mbedtls_ssl_read_record(). */
4975static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004976static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4977static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004978
Hanno Becker327c93b2018-08-15 13:56:18 +01004979int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004980 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004981{
4982 int ret;
4983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004985
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004986 if( ssl->keep_current_message == 0 )
4987 {
4988 do {
Simon Butcher99000142016-10-13 17:21:01 +01004989
Hanno Becker26994592018-08-15 14:14:59 +01004990 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004991 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004992 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004993
Hanno Beckere74d5562018-08-15 14:26:08 +01004994 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004995 {
Hanno Becker40f50842018-08-15 14:48:01 +01004996#if defined(MBEDTLS_SSL_PROTO_DTLS)
4997 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004998
Hanno Becker40f50842018-08-15 14:48:01 +01004999 /* We only check for buffered messages if the
5000 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005001 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005002 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005003 {
Hanno Becker40f50842018-08-15 14:48:01 +01005004 if( ssl_load_buffered_message( ssl ) == 0 )
5005 have_buffered = 1;
5006 }
5007
5008 if( have_buffered == 0 )
5009#endif /* MBEDTLS_SSL_PROTO_DTLS */
5010 {
5011 ret = ssl_get_next_record( ssl );
5012 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5013 continue;
5014
5015 if( ret != 0 )
5016 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005017 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005018 return( ret );
5019 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005020 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005021 }
5022
5023 ret = mbedtls_ssl_handle_message_type( ssl );
5024
Hanno Becker40f50842018-08-15 14:48:01 +01005025#if defined(MBEDTLS_SSL_PROTO_DTLS)
5026 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5027 {
5028 /* Buffer future message */
5029 ret = ssl_buffer_message( ssl );
5030 if( ret != 0 )
5031 return( ret );
5032
5033 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5034 }
5035#endif /* MBEDTLS_SSL_PROTO_DTLS */
5036
Hanno Becker90333da2017-10-10 11:27:13 +01005037 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5038 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005039
5040 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005041 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005042 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005043 return( ret );
5044 }
5045
Hanno Becker327c93b2018-08-15 13:56:18 +01005046 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005047 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005048 {
5049 mbedtls_ssl_update_handshake_status( ssl );
5050 }
Simon Butcher99000142016-10-13 17:21:01 +01005051 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005052 else
Simon Butcher99000142016-10-13 17:21:01 +01005053 {
Hanno Becker02f59072018-08-15 14:00:24 +01005054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005055 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005056 }
5057
5058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5059
5060 return( 0 );
5061}
5062
Hanno Becker40f50842018-08-15 14:48:01 +01005063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005064static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005065{
Hanno Becker40f50842018-08-15 14:48:01 +01005066 if( ssl->in_left > ssl->next_record_offset )
5067 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005068
Hanno Becker40f50842018-08-15 14:48:01 +01005069 return( 0 );
5070}
5071
5072static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5073{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005074 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005075 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005076 int ret = 0;
5077
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005078 if( hs == NULL )
5079 return( -1 );
5080
Hanno Beckere00ae372018-08-20 09:39:42 +01005081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5082
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005083 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5084 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5085 {
5086 /* Check if we have seen a ChangeCipherSpec before.
5087 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005088 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005089 {
5090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5091 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005092 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 }
5094
Hanno Becker39b8bc92018-08-28 17:17:13 +01005095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005096 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5097 ssl->in_msglen = 1;
5098 ssl->in_msg[0] = 1;
5099
5100 /* As long as they are equal, the exact value doesn't matter. */
5101 ssl->in_left = 0;
5102 ssl->next_record_offset = 0;
5103
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005104 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005105 goto exit;
5106 }
Hanno Becker37f95322018-08-16 13:55:32 +01005107
Hanno Beckerb8f50142018-08-28 10:01:34 +01005108#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005109 /* Debug only */
5110 {
5111 unsigned offset;
5112 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5113 {
5114 hs_buf = &hs->buffering.hs[offset];
5115 if( hs_buf->is_valid == 1 )
5116 {
5117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5118 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005119 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005120 }
5121 }
5122 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005123#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005124
5125 /* Check if we have buffered and/or fully reassembled the
5126 * next handshake message. */
5127 hs_buf = &hs->buffering.hs[0];
5128 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5129 {
5130 /* Synthesize a record containing the buffered HS message. */
5131 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5132 ( hs_buf->data[2] << 8 ) |
5133 hs_buf->data[3];
5134
5135 /* Double-check that we haven't accidentally buffered
5136 * a message that doesn't fit into the input buffer. */
5137 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5138 {
5139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5141 }
5142
5143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5144 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5145 hs_buf->data, msg_len + 12 );
5146
5147 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5148 ssl->in_hslen = msg_len + 12;
5149 ssl->in_msglen = msg_len + 12;
5150 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5151
5152 ret = 0;
5153 goto exit;
5154 }
5155 else
5156 {
5157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5158 hs->in_msg_seq ) );
5159 }
5160
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005161 ret = -1;
5162
5163exit:
5164
5165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5166 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005167}
5168
Hanno Beckera02b0b42018-08-21 17:20:27 +01005169static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5170 size_t desired )
5171{
5172 int offset;
5173 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5175 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005176
Hanno Becker01315ea2018-08-21 17:22:17 +01005177 /* Get rid of future records epoch first, if such exist. */
5178 ssl_free_buffered_record( ssl );
5179
5180 /* Check if we have enough space available now. */
5181 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5182 hs->buffering.total_bytes_buffered ) )
5183 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005185 return( 0 );
5186 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005187
Hanno Becker4f432ad2018-08-28 10:02:32 +01005188 /* We don't have enough space to buffer the next expected handshake
5189 * message. Remove buffers used for future messages to gain space,
5190 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5192 offset >= 0; offset-- )
5193 {
5194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5195 offset ) );
5196
Hanno Beckerb309b922018-08-23 13:18:05 +01005197 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005198
5199 /* Check if we have enough space available now. */
5200 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5201 hs->buffering.total_bytes_buffered ) )
5202 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005204 return( 0 );
5205 }
5206 }
5207
5208 return( -1 );
5209}
5210
Hanno Becker40f50842018-08-15 14:48:01 +01005211static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5212{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005213 int ret = 0;
5214 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5215
5216 if( hs == NULL )
5217 return( 0 );
5218
5219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5220
5221 switch( ssl->in_msgtype )
5222 {
5223 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005225
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005226 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005227 break;
5228
5229 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005230 {
5231 unsigned recv_msg_seq_offset;
5232 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5233 mbedtls_ssl_hs_buffer *hs_buf;
5234 size_t msg_len = ssl->in_hslen - 12;
5235
5236 /* We should never receive an old handshake
5237 * message - double-check nonetheless. */
5238 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5239 {
5240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5242 }
5243
5244 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5245 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5246 {
5247 /* Silently ignore -- message too far in the future */
5248 MBEDTLS_SSL_DEBUG_MSG( 2,
5249 ( "Ignore future HS message with sequence number %u, "
5250 "buffering window %u - %u",
5251 recv_msg_seq, ssl->handshake->in_msg_seq,
5252 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5253
5254 goto exit;
5255 }
5256
5257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5258 recv_msg_seq, recv_msg_seq_offset ) );
5259
5260 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5261
5262 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005263 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005264 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005265 size_t reassembly_buf_sz;
5266
Hanno Becker37f95322018-08-16 13:55:32 +01005267 hs_buf->is_fragmented =
5268 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5269
5270 /* We copy the message back into the input buffer
5271 * after reassembly, so check that it's not too large.
5272 * This is an implementation-specific limitation
5273 * and not one from the standard, hence it is not
5274 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005275 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005276 {
5277 /* Ignore message */
5278 goto exit;
5279 }
5280
Hanno Beckere0b150f2018-08-21 15:51:03 +01005281 /* Check if we have enough space to buffer the message. */
5282 if( hs->buffering.total_bytes_buffered >
5283 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5284 {
5285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5287 }
5288
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005289 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5290 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005291
5292 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5293 hs->buffering.total_bytes_buffered ) )
5294 {
5295 if( recv_msg_seq_offset > 0 )
5296 {
5297 /* If we can't buffer a future message because
5298 * of space limitations -- ignore. */
5299 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",
5300 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5301 (unsigned) hs->buffering.total_bytes_buffered ) );
5302 goto exit;
5303 }
Hanno Beckere1801392018-08-21 16:51:05 +01005304 else
5305 {
5306 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",
5307 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5308 (unsigned) hs->buffering.total_bytes_buffered ) );
5309 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005310
Hanno Beckera02b0b42018-08-21 17:20:27 +01005311 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005312 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005313 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",
5314 (unsigned) msg_len,
5315 (unsigned) reassembly_buf_sz,
5316 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005317 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005318 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5319 goto exit;
5320 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005321 }
5322
5323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5324 msg_len ) );
5325
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005326 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5327 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005328 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005329 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005330 goto exit;
5331 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005332 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005333
5334 /* Prepare final header: copy msg_type, length and message_seq,
5335 * then add standardised fragment_offset and fragment_length */
5336 memcpy( hs_buf->data, ssl->in_msg, 6 );
5337 memset( hs_buf->data + 6, 0, 3 );
5338 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5339
5340 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005341
5342 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005343 }
5344 else
5345 {
5346 /* Make sure msg_type and length are consistent */
5347 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5348 {
5349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5350 /* Ignore */
5351 goto exit;
5352 }
5353 }
5354
Hanno Becker4422bbb2018-08-20 09:40:19 +01005355 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005356 {
5357 size_t frag_len, frag_off;
5358 unsigned char * const msg = hs_buf->data + 12;
5359
5360 /*
5361 * Check and copy current fragment
5362 */
5363
5364 /* Validation of header fields already done in
5365 * mbedtls_ssl_prepare_handshake_record(). */
5366 frag_off = ssl_get_hs_frag_off( ssl );
5367 frag_len = ssl_get_hs_frag_len( ssl );
5368
5369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5370 frag_off, frag_len ) );
5371 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5372
5373 if( hs_buf->is_fragmented )
5374 {
5375 unsigned char * const bitmask = msg + msg_len;
5376 ssl_bitmask_set( bitmask, frag_off, frag_len );
5377 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5378 msg_len ) == 0 );
5379 }
5380 else
5381 {
5382 hs_buf->is_complete = 1;
5383 }
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5386 hs_buf->is_complete ? "" : "not yet " ) );
5387 }
5388
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005389 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005390 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005391
5392 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005393 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005394 break;
5395 }
5396
5397exit:
5398
5399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5400 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005401}
5402#endif /* MBEDTLS_SSL_PROTO_DTLS */
5403
Hanno Becker1097b342018-08-15 14:09:41 +01005404static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005405{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005406 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005407 * Consume last content-layer message and potentially
5408 * update in_msglen which keeps track of the contents'
5409 * consumption state.
5410 *
5411 * (1) Handshake messages:
5412 * Remove last handshake message, move content
5413 * and adapt in_msglen.
5414 *
5415 * (2) Alert messages:
5416 * Consume whole record content, in_msglen = 0.
5417 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005418 * (3) Change cipher spec:
5419 * Consume whole record content, in_msglen = 0.
5420 *
5421 * (4) Application data:
5422 * Don't do anything - the record layer provides
5423 * the application data as a stream transport
5424 * and consumes through mbedtls_ssl_read only.
5425 *
5426 */
5427
5428 /* Case (1): Handshake messages */
5429 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005430 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005431 /* Hard assertion to be sure that no application data
5432 * is in flight, as corrupting ssl->in_msglen during
5433 * ssl->in_offt != NULL is fatal. */
5434 if( ssl->in_offt != NULL )
5435 {
5436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5438 }
5439
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005440 /*
5441 * Get next Handshake message in the current record
5442 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005443
Hanno Becker4a810fb2017-05-24 16:27:30 +01005444 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005445 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005446 * current handshake content: If DTLS handshake
5447 * fragmentation is used, that's the fragment
5448 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005449 * size here is faulty and should be changed at
5450 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005451 * (2) While it doesn't seem to cause problems, one
5452 * has to be very careful not to assume that in_hslen
5453 * is always <= in_msglen in a sensible communication.
5454 * Again, it's wrong for DTLS handshake fragmentation.
5455 * The following check is therefore mandatory, and
5456 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005457 * Additionally, ssl->in_hslen might be arbitrarily out of
5458 * bounds after handling a DTLS message with an unexpected
5459 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005460 */
5461 if( ssl->in_hslen < ssl->in_msglen )
5462 {
5463 ssl->in_msglen -= ssl->in_hslen;
5464 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5465 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005466
Hanno Becker4a810fb2017-05-24 16:27:30 +01005467 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5468 ssl->in_msg, ssl->in_msglen );
5469 }
5470 else
5471 {
5472 ssl->in_msglen = 0;
5473 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005474
Hanno Becker4a810fb2017-05-24 16:27:30 +01005475 ssl->in_hslen = 0;
5476 }
5477 /* Case (4): Application data */
5478 else if( ssl->in_offt != NULL )
5479 {
5480 return( 0 );
5481 }
5482 /* Everything else (CCS & Alerts) */
5483 else
5484 {
5485 ssl->in_msglen = 0;
5486 }
5487
Hanno Becker1097b342018-08-15 14:09:41 +01005488 return( 0 );
5489}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005490
Hanno Beckere74d5562018-08-15 14:26:08 +01005491static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5492{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005493 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005494 return( 1 );
5495
5496 return( 0 );
5497}
5498
Hanno Becker5f066e72018-08-16 14:56:31 +01005499#if defined(MBEDTLS_SSL_PROTO_DTLS)
5500
5501static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5502{
5503 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5504 if( hs == NULL )
5505 return;
5506
Hanno Becker01315ea2018-08-21 17:22:17 +01005507 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005508 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005509 hs->buffering.total_bytes_buffered -=
5510 hs->buffering.future_record.len;
5511
5512 mbedtls_free( hs->buffering.future_record.data );
5513 hs->buffering.future_record.data = NULL;
5514 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005515}
5516
5517static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5518{
5519 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5520 unsigned char * rec;
5521 size_t rec_len;
5522 unsigned rec_epoch;
5523
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005524 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005525 return( 0 );
5526
5527 if( hs == NULL )
5528 return( 0 );
5529
Hanno Becker5f066e72018-08-16 14:56:31 +01005530 rec = hs->buffering.future_record.data;
5531 rec_len = hs->buffering.future_record.len;
5532 rec_epoch = hs->buffering.future_record.epoch;
5533
5534 if( rec == NULL )
5535 return( 0 );
5536
Hanno Becker4cb782d2018-08-20 11:19:05 +01005537 /* Only consider loading future records if the
5538 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005539 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005540 return( 0 );
5541
Hanno Becker5f066e72018-08-16 14:56:31 +01005542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5543
5544 if( rec_epoch != ssl->in_epoch )
5545 {
5546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5547 goto exit;
5548 }
5549
5550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5551
5552 /* Double-check that the record is not too large */
5553 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5554 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5555 {
5556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5558 }
5559
5560 memcpy( ssl->in_hdr, rec, rec_len );
5561 ssl->in_left = rec_len;
5562 ssl->next_record_offset = 0;
5563
5564 ssl_free_buffered_record( ssl );
5565
5566exit:
5567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5568 return( 0 );
5569}
5570
5571static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5572{
5573 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5574 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005575 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005576
5577 /* Don't buffer future records outside handshakes. */
5578 if( hs == NULL )
5579 return( 0 );
5580
5581 /* Only buffer handshake records (we are only interested
5582 * in Finished messages). */
5583 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5584 return( 0 );
5585
5586 /* Don't buffer more than one future epoch record. */
5587 if( hs->buffering.future_record.data != NULL )
5588 return( 0 );
5589
Hanno Becker01315ea2018-08-21 17:22:17 +01005590 /* Don't buffer record if there's not enough buffering space remaining. */
5591 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5592 hs->buffering.total_bytes_buffered ) )
5593 {
5594 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",
5595 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5596 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005597 return( 0 );
5598 }
5599
Hanno Becker5f066e72018-08-16 14:56:31 +01005600 /* Buffer record */
5601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5602 ssl->in_epoch + 1 ) );
5603 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5604 rec_hdr_len + ssl->in_msglen );
5605
5606 /* ssl_parse_record_header() only considers records
5607 * of the next epoch as candidates for buffering. */
5608 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005609 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005610
5611 hs->buffering.future_record.data =
5612 mbedtls_calloc( 1, hs->buffering.future_record.len );
5613 if( hs->buffering.future_record.data == NULL )
5614 {
5615 /* If we run out of RAM trying to buffer a
5616 * record from the next epoch, just ignore. */
5617 return( 0 );
5618 }
5619
Hanno Becker01315ea2018-08-21 17:22:17 +01005620 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005621
Hanno Becker01315ea2018-08-21 17:22:17 +01005622 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005623 return( 0 );
5624}
5625
5626#endif /* MBEDTLS_SSL_PROTO_DTLS */
5627
Hanno Beckere74d5562018-08-15 14:26:08 +01005628static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005629{
5630 int ret;
5631
Hanno Becker5f066e72018-08-16 14:56:31 +01005632#if defined(MBEDTLS_SSL_PROTO_DTLS)
5633 /* We might have buffered a future record; if so,
5634 * and if the epoch matches now, load it.
5635 * On success, this call will set ssl->in_left to
5636 * the length of the buffered record, so that
5637 * the calls to ssl_fetch_input() below will
5638 * essentially be no-ops. */
5639 ret = ssl_load_buffered_record( ssl );
5640 if( ret != 0 )
5641 return( ret );
5642#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005643
Hanno Becker8b09b732019-05-08 12:03:28 +01005644 /* Reset in pointers to default state for TLS/DTLS records,
5645 * assuming no CID and no offset between record content and
5646 * record plaintext. */
5647 ssl_update_in_pointers( ssl );
5648
5649 /* Ensure that we have enough space available for the default form
5650 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5651 * with no space for CIDs counted in). */
5652 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5653 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005656 return( ret );
5657 }
5658
5659 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005662 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005663 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005664 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005665 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5666 {
5667 ret = ssl_buffer_future_record( ssl );
5668 if( ret != 0 )
5669 return( ret );
5670
5671 /* Fall through to handling of unexpected records */
5672 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5673 }
5674
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005675 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5676 {
5677 /* Skip unexpected record (but not whole datagram) */
5678 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005679 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005680
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5682 "(header)" ) );
5683 }
5684 else
5685 {
5686 /* Skip invalid record and the rest of the datagram */
5687 ssl->next_record_offset = 0;
5688 ssl->in_left = 0;
5689
5690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5691 "(header)" ) );
5692 }
5693
5694 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005695 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005696 }
5697#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005698 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005699 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005700
5701 /*
5702 * Read and optionally decrypt the message contents
5703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005705 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005708 return( ret );
5709 }
5710
5711 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005713 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005714 {
Hanno Becker43395762019-05-03 14:46:38 +01005715 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005716 if( ssl->next_record_offset < ssl->in_left )
5717 {
5718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5719 }
5720 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005721 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005723#if defined(MBEDTLS_SSL_PROTO_TLS)
5724 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005725 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005726 }
5727#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005728
5729 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005732 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005733 {
5734 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005735 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005736 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005737 /* Except when waiting for Finished as a bad mac here
5738 * probably means something went wrong in the handshake
5739 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5740 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5741 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5742 {
5743#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5744 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5745 {
5746 mbedtls_ssl_send_alert_message( ssl,
5747 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5748 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5749 }
5750#endif
5751 return( ret );
5752 }
5753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005755 if( ssl->conf->badmac_limit != 0 &&
5756 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5759 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005760 }
5761#endif
5762
Hanno Becker4a810fb2017-05-24 16:27:30 +01005763 /* As above, invalid records cause
5764 * dismissal of the whole datagram. */
5765
5766 ssl->next_record_offset = 0;
5767 ssl->in_left = 0;
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005770 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005771 }
5772
5773 return( ret );
5774 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005775 MBEDTLS_SSL_TRANSPORT_ELSE
5776#endif /* MBEDTLS_SSL_PROTO_DTLS */
5777#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005778 {
5779 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5781 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783 mbedtls_ssl_send_alert_message( ssl,
5784 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5785 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005786 }
5787#endif
5788 return( ret );
5789 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005790#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005791 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005792
Simon Butcher99000142016-10-13 17:21:01 +01005793 return( 0 );
5794}
5795
5796int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5797{
5798 int ret;
5799
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005800 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005801 * Handle particular types of records
5802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005804 {
Simon Butcher99000142016-10-13 17:21:01 +01005805 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5806 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005807 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 }
5810
Hanno Beckere678eaa2018-08-21 14:57:46 +01005811 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005812 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005813 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005814 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5816 ssl->in_msglen ) );
5817 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005818 }
5819
Hanno Beckere678eaa2018-08-21 14:57:46 +01005820 if( ssl->in_msg[0] != 1 )
5821 {
5822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5823 ssl->in_msg[0] ) );
5824 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5825 }
5826
5827#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005828 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005829 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5830 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5831 {
5832 if( ssl->handshake == NULL )
5833 {
5834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5835 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5836 }
5837
5838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5839 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5840 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005841#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005842 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005846 if( ssl->in_msglen != 2 )
5847 {
5848 /* Note: Standard allows for more than one 2 byte alert
5849 to be packed in a single message, but Mbed TLS doesn't
5850 currently support this. */
5851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5852 ssl->in_msglen ) );
5853 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5854 }
5855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005857 ssl->in_msg[0], ssl->in_msg[1] ) );
5858
5859 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005860 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005865 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005867 }
5868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5870 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5873 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005874 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005875
5876#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5877 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5878 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5879 {
Hanno Becker90333da2017-10-10 11:27:13 +01005880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005881 /* Will be handled when trying to parse ServerHello */
5882 return( 0 );
5883 }
5884#endif
5885
5886#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5887 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5888 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5889 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5890 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5891 {
5892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5893 /* Will be handled in mbedtls_ssl_parse_certificate() */
5894 return( 0 );
5895 }
5896#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5897
5898 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005899 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005900 }
5901
Hanno Beckerc76c6192017-06-06 10:03:17 +01005902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005903 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005904 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005905 /* Drop unexpected ApplicationData records,
5906 * except at the beginning of renegotiations */
5907 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5908 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5909#if defined(MBEDTLS_SSL_RENEGOTIATION)
5910 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5911 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005912#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005913 )
5914 {
5915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5916 return( MBEDTLS_ERR_SSL_NON_FATAL );
5917 }
5918
5919 if( ssl->handshake != NULL &&
5920 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5921 {
5922 ssl_handshake_wrapup_free_hs_transform( ssl );
5923 }
5924 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005925#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005926
Paul Bakker5121ce52009-01-03 21:22:43 +00005927 return( 0 );
5928}
5929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005931{
5932 int ret;
5933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5935 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5936 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005937 {
5938 return( ret );
5939 }
5940
5941 return( 0 );
5942}
5943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005945 unsigned char level,
5946 unsigned char message )
5947{
5948 int ret;
5949
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005950 if( ssl == NULL || ssl->conf == NULL )
5951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005957 ssl->out_msglen = 2;
5958 ssl->out_msg[0] = level;
5959 ssl->out_msg[1] = message;
5960
Hanno Becker67bc7c32018-08-06 11:33:50 +01005961 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005964 return( ret );
5965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005967
5968 return( 0 );
5969}
5970
Paul Bakker5121ce52009-01-03 21:22:43 +00005971/*
5972 * Handshake functions
5973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5975 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5976 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5977 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5978 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5979 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5980 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005981/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005983{
Hanno Becker8759e162017-12-27 21:34:08 +00005984 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5989 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005990 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5991 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005994 ssl->state++;
5995 return( 0 );
5996 }
5997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006000}
6001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006003{
Hanno Becker8759e162017-12-27 21:34:08 +00006004 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6009 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006010 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6011 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006014 ssl->state++;
6015 return( 0 );
6016 }
6017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006020}
Gilles Peskinef9828522017-05-03 12:28:43 +02006021
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006022#else
Gilles Peskinef9828522017-05-03 12:28:43 +02006023/* Some certificate support -> implement write and parse */
6024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006028 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006030 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6035 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006036 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6037 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040 ssl->state++;
6041 return( 0 );
6042 }
6043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006045 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 {
6047 if( ssl->client_auth == 0 )
6048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 ssl->state++;
6051 return( 0 );
6052 }
6053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 /*
6056 * If using SSLv3 and got no cert, send an Alert message
6057 * (otherwise an empty Certificate message will be sent).
6058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6060 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 {
6062 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6064 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6065 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 goto write_msg;
6069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072#endif /* MBEDTLS_SSL_CLI_C */
6073#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006074 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6079 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006080 }
6081 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006085
6086 /*
6087 * 0 . 0 handshake type
6088 * 1 . 3 handshake length
6089 * 4 . 6 length of all certs
6090 * 7 . 9 length of cert. 1
6091 * 10 . n-1 peer certificate
6092 * n . n+2 length of cert. 2
6093 * n+3 . ... upper level cert, etc.
6094 */
6095 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
Paul Bakker29087132010-03-21 21:03:34 +00006098 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
6100 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006101 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006104 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 }
6107
6108 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6109 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6110 ssl->out_msg[i + 2] = (unsigned char)( n );
6111
6112 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6113 i += n; crt = crt->next;
6114 }
6115
6116 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6117 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6118 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6119
6120 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6122 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006123
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006124#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006125write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006127
6128 ssl->state++;
6129
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006130 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 return( ret );
6134 }
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
Paul Bakkered27a042013-04-18 22:46:23 +02006138 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006139}
6140
Hanno Becker285ff0c2019-01-31 07:44:03 +00006141#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006142static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6143 unsigned char *crt_buf,
6144 size_t crt_buf_len )
6145{
6146 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6147
6148 if( peer_crt == NULL )
6149 return( -1 );
6150
6151 if( peer_crt->raw.len != crt_buf_len )
6152 return( -1 );
6153
Hanno Becker68b856d2019-02-08 14:00:04 +00006154 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006155}
Hanno Becker285ff0c2019-01-31 07:44:03 +00006156#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006157
Hanno Becker22141592019-02-05 12:38:15 +00006158static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6159{
6160 if( session->peer_cert != NULL )
6161 {
6162 mbedtls_x509_crt_free( session->peer_cert );
6163 mbedtls_free( session->peer_cert );
6164 session->peer_cert = NULL;
6165 }
6166}
6167
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006168/*
6169 * Once the certificate message is read, parse it into a cert chain and
6170 * perform basic checks, but leave actual verification to the caller
6171 */
6172static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006173{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006174 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006175 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006176 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178#if defined(MBEDTLS_SSL_SRV_C)
6179#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006180 /*
6181 * Check if the client sent an empty certificate
6182 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006183 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006185 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006186 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6188 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6189 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006192
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006193 /* The client was asked for a certificate but didn't send
6194 one. The client should know what's going on, so we
6195 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006196 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006197 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006198 }
6199 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6203 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006204 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6208 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6209 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6210 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006213
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006214 /* The client was asked for a certificate but didn't send
6215 one. The client should know what's going on, so we
6216 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006217 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006218 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 }
6220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6222 MBEDTLS_SSL_PROTO_TLS1_2 */
6223#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006228 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6229 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006231 }
6232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6234 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
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_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006240 }
6241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006243
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006247 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006248
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006249 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006253 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6254 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006256 }
6257
Hanno Becker33c3dc82019-01-30 14:46:46 +00006258 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6259 i += 3;
6260
Hanno Becker22141592019-02-05 12:38:15 +00006261 /* In case we tried to reuse a session but it failed. */
6262 ssl_clear_peer_cert( ssl->session_negotiate );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006263
Hanno Becker33c3dc82019-01-30 14:46:46 +00006264 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006265 while( i < ssl->in_hslen )
6266 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006267 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006268 if ( i + 3 > ssl->in_hslen ) {
6269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006270 mbedtls_ssl_send_alert_message( ssl,
6271 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6272 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006273 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6274 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006275 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6276 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006277 if( ssl->in_msg[i] != 0 )
6278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006280 mbedtls_ssl_send_alert_message( ssl,
6281 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6282 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 }
6285
Hanno Becker33c3dc82019-01-30 14:46:46 +00006286 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006287 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6288 | (unsigned int) ssl->in_msg[i + 2];
6289 i += 3;
6290
6291 if( n < 128 || i + n > ssl->in_hslen )
6292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006294 mbedtls_ssl_send_alert_message( ssl,
6295 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6296 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006298 }
6299
Hanno Becker33c3dc82019-01-30 14:46:46 +00006300 /* Check if we're handling the first CRT in the chain. */
6301 if( ssl->session_negotiate->peer_cert == NULL )
6302 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006303 /* During client-side renegotiation, check that the server's
6304 * end-CRTs hasn't changed compared to the initial handshake,
6305 * mitigating the triple handshake attack. On success, reuse
6306 * the original end-CRT instead of parsing it again. */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006307#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6308 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6309 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6310 {
6311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006312 if( ssl_check_peer_crt_unchanged( ssl,
6313 &ssl->in_msg[i],
6314 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006315 {
6316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006317 mbedtls_ssl_send_alert_message( ssl,
6318 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6319 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006320 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6321 }
6322
Hanno Becker1332f352019-02-05 15:06:15 +00006323 /* Now we can safely free the original chain. */
Hanno Becker22141592019-02-05 12:38:15 +00006324 ssl_clear_peer_cert( ssl->session );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006325
Hanno Becker1332f352019-02-05 15:06:15 +00006326 /* Intentional fallthrough. */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006327 }
6328#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6329
6330 /* Outside of client-side renegotiation, create a fresh X.509 CRT
6331 * instance to parse the end-CRT into. */
6332
6333 ssl->session_negotiate->peer_cert =
6334 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6335 if( ssl->session_negotiate->peer_cert == NULL )
6336 {
6337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6338 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006339 mbedtls_ssl_send_alert_message( ssl,
6340 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6341 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006342 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6343 }
6344
6345 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
6346
6347 /* Intentional fall through */
6348 }
6349
6350 /* Parse the next certificate in the chain. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Hanno Becker33c3dc82019-01-30 14:46:46 +00006352 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006353 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006354 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006355 case 0: /*ok*/
6356 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6357 /* Ignore certificate with an unknown algorithm: maybe a
6358 prior certificate was already trusted. */
6359 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006360
Hanno Becker33c3dc82019-01-30 14:46:46 +00006361 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6362 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6363 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006364
Hanno Becker33c3dc82019-01-30 14:46:46 +00006365 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6366 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6367 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006368
Hanno Becker33c3dc82019-01-30 14:46:46 +00006369 default:
6370 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6371 crt_parse_der_failed:
6372 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6373 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6374 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006375 }
6376
6377 i += n;
6378 }
6379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006381 return( 0 );
6382}
6383
6384int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6385{
6386 int ret;
6387 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006388 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006389#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6390 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6391 ? ssl->handshake->sni_authmode
6392 : ssl->conf->authmode;
6393#else
6394 const int authmode = ssl->conf->authmode;
6395#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006396 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006397
6398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6399
6400 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6401 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6402 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6403 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6404 {
6405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6406 ssl->state++;
6407 return( 0 );
6408 }
6409
6410#if defined(MBEDTLS_SSL_SRV_C)
6411 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6412 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6413 {
6414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6415 ssl->state++;
6416 return( 0 );
6417 }
6418
6419 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6420 authmode == MBEDTLS_SSL_VERIFY_NONE )
6421 {
6422 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006424
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006425 ssl->state++;
6426 return( 0 );
6427 }
6428#endif
6429
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006430#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6431 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006432 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006433 {
6434 goto crt_verify;
6435 }
6436#endif
6437
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006438 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006439 {
6440 /* mbedtls_ssl_read_record may have sent an alert already. We
6441 let it decide whether to alert. */
6442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6443 return( ret );
6444 }
6445
6446 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6447 {
6448#if defined(MBEDTLS_SSL_SRV_C)
6449 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6450 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6451 {
6452 ret = 0;
6453 }
6454#endif
6455
6456 ssl->state++;
6457 return( ret );
6458 }
6459
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006460#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6461 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006462 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006463
6464crt_verify:
6465 if( ssl->handshake->ecrs_enabled)
6466 rs_ctx = &ssl->handshake->ecrs_ctx;
6467#endif
6468
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006469 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006470 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006471 mbedtls_x509_crt *ca_chain;
6472 mbedtls_x509_crl *ca_crl;
6473
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006474#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006475 if( ssl->handshake->sni_ca_chain != NULL )
6476 {
6477 ca_chain = ssl->handshake->sni_ca_chain;
6478 ca_crl = ssl->handshake->sni_ca_crl;
6479 }
6480 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006481#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006482 {
6483 ca_chain = ssl->conf->ca_chain;
6484 ca_crl = ssl->conf->ca_crl;
6485 }
6486
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006487 /*
6488 * Main check: verify certificate
6489 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006490 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006491 ssl->session_negotiate->peer_cert,
6492 ca_chain, ca_crl,
6493 ssl->conf->cert_profile,
6494 ssl->hostname,
6495 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006496 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006497
6498 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006501 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006502
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006503#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6504 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006505 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006506#endif
6507
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006508 /*
6509 * Secondary checks: always done, but change 'ret' only if it was 0
6510 */
6511
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006512#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006515
6516 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006518 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006519 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006520 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006523 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006525 }
6526 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006527#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006530 ciphersuite_info,
6531 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006532 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006535 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006537 }
6538
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006539 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6540 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6541 * with details encoded in the verification flags. All other kinds
6542 * of error codes, including those from the user provided f_vrfy
6543 * functions, are treated as fatal and lead to a failure of
6544 * ssl_parse_certificate even if verification was optional. */
6545 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6546 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6547 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6548 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006549 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006550 }
6551
6552 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6553 {
6554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6555 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6556 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006557
6558 if( ret != 0 )
6559 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006560 uint8_t alert;
6561
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006562 /* The certificate may have been rejected for several reasons.
6563 Pick one and send the corresponding alert. Which alert to send
6564 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006565 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6566 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6567 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6568 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6569 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6570 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6571 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6572 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6573 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6574 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6575 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6576 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6577 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6578 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6579 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6580 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6581 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6582 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6583 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6584 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006585 else
6586 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006587 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6588 alert );
6589 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006590
Hanno Beckere6706e62017-05-15 16:05:15 +01006591#if defined(MBEDTLS_DEBUG_C)
6592 if( ssl->session_negotiate->verify_result != 0 )
6593 {
6594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6595 ssl->session_negotiate->verify_result ) );
6596 }
6597 else
6598 {
6599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6600 }
6601#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 }
6603
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006604 ssl->state++;
6605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006607
6608 return( ret );
6609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6611 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6612 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6613 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6614 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6615 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6616 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006619{
6620 int ret;
6621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006625 ssl->out_msglen = 1;
6626 ssl->out_msg[0] = 1;
6627
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 ssl->state++;
6629
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006630 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006631 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006633 return( ret );
6634 }
6635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006637
6638 return( 0 );
6639}
6640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006642{
6643 int ret;
6644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006646
Hanno Becker327c93b2018-08-15 13:56:18 +01006647 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006650 return( ret );
6651 }
6652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006656 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6657 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006659 }
6660
Hanno Beckere678eaa2018-08-21 14:57:46 +01006661 /* CCS records are only accepted if they have length 1 and content '1',
6662 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006663
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006664 /*
6665 * Switch to our negotiated transform and session parameters for inbound
6666 * data.
6667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006669 ssl->transform_in = ssl->transform_negotiate;
6670 ssl->session_in = ssl->session_negotiate;
6671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006673 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006676 ssl_dtls_replay_reset( ssl );
6677#endif
6678
6679 /* Increment epoch */
6680 if( ++ssl->in_epoch == 0 )
6681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006683 /* This is highly unlikely to happen for legitimate reasons, so
6684 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006686 }
6687 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006688 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006690#if defined(MBEDTLS_SSL_PROTO_TLS)
6691 {
6692 memset( ssl->in_ctr, 0, 8 );
6693 }
6694#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006695
Hanno Beckerf5970a02019-05-08 09:38:41 +01006696 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6699 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006704 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6705 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006707 }
6708 }
6709#endif
6710
Paul Bakker5121ce52009-01-03 21:22:43 +00006711 ssl->state++;
6712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006714
6715 return( 0 );
6716}
6717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6719 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006720{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006721 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6724 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6725 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006726 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006727 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6730#if defined(MBEDTLS_SHA512_C)
6731 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006732 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6733 else
6734#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735#if defined(MBEDTLS_SHA256_C)
6736 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006737 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006738 else
6739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006743 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006744 }
Paul Bakker380da532012-04-18 16:10:25 +00006745}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006748{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6750 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006751 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6752 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6755#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006756 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006759 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006760#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006762}
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006765 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6768 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006769 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6770 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6773#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006774 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006777 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006778#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006780}
6781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6783 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6784static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006785 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006786{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006787 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6788 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006789}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006790#endif
Paul Bakker380da532012-04-18 16:10:25 +00006791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6793#if defined(MBEDTLS_SHA256_C)
6794static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006795 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006796{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006797 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006798}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006799#endif
Paul Bakker380da532012-04-18 16:10:25 +00006800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801#if defined(MBEDTLS_SHA512_C)
6802static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006803 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006804{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006805 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006806}
Paul Bakker769075d2012-11-24 11:26:46 +01006807#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006811static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006813{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006814 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006815 mbedtls_md5_context md5;
6816 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006817
Paul Bakker5121ce52009-01-03 21:22:43 +00006818 unsigned char padbuf[48];
6819 unsigned char md5sum[16];
6820 unsigned char sha1sum[20];
6821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006823 if( !session )
6824 session = ssl->session;
6825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006827
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006828 mbedtls_md5_init( &md5 );
6829 mbedtls_sha1_init( &sha1 );
6830
6831 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6832 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006833
6834 /*
6835 * SSLv3:
6836 * hash =
6837 * MD5( master + pad2 +
6838 * MD5( handshake + sender + master + pad1 ) )
6839 * + SHA1( master + pad2 +
6840 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006841 */
6842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006844 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6845 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006846#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006849 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6850 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006851#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006854 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006855
Paul Bakker1ef83d62012-04-11 12:09:53 +00006856 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006857
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006858 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6859 mbedtls_md5_update_ret( &md5, session->master, 48 );
6860 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6861 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006862
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006863 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6864 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6865 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6866 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006867
Paul Bakker1ef83d62012-04-11 12:09:53 +00006868 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006869
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006870 mbedtls_md5_starts_ret( &md5 );
6871 mbedtls_md5_update_ret( &md5, session->master, 48 );
6872 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6873 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6874 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006875
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006876 mbedtls_sha1_starts_ret( &sha1 );
6877 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6878 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6879 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6880 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006883
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006884 mbedtls_md5_free( &md5 );
6885 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006886
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006887 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6888 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6889 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006892}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006896static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006898{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006899 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006900 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006901 mbedtls_md5_context md5;
6902 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006903 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006906 if( !session )
6907 session = ssl->session;
6908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006910
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006911 mbedtls_md5_init( &md5 );
6912 mbedtls_sha1_init( &sha1 );
6913
6914 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6915 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006916
Paul Bakker1ef83d62012-04-11 12:09:53 +00006917 /*
6918 * TLSv1:
6919 * hash = PRF( master, finished_label,
6920 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6921 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006924 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6925 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006926#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006929 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6930 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006931#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006934 ? "client finished"
6935 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006936
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006937 mbedtls_md5_finish_ret( &md5, padbuf );
6938 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006939
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006940 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006941 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006944
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006945 mbedtls_md5_free( &md5 );
6946 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006947
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006948 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6955#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006956static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006958{
6959 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006960 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006961 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006962 unsigned char padbuf[32];
6963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006965 if( !session )
6966 session = ssl->session;
6967
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006968 mbedtls_sha256_init( &sha256 );
6969
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006971
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006972 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006973
6974 /*
6975 * TLSv1.2:
6976 * hash = PRF( master, finished_label,
6977 * Hash( handshake ) )[0.11]
6978 */
6979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#if !defined(MBEDTLS_SHA256_ALT)
6981 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006982 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006983#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006986 ? "client finished"
6987 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006988
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006989 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006990
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006991 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006992 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006995
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006996 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006997
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006998 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007005static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007007{
7008 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007009 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007010 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007011 unsigned char padbuf[48];
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007014 if( !session )
7015 session = ssl->session;
7016
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007017 mbedtls_sha512_init( &sha512 );
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007020
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007021 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007022
7023 /*
7024 * TLSv1.2:
7025 * hash = PRF( master, finished_label,
7026 * Hash( handshake ) )[0.11]
7027 */
7028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007030 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7031 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007032#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007035 ? "client finished"
7036 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007037
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007038 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007039
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007040 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007041 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007044
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007045 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007046
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007047 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007050}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051#endif /* MBEDTLS_SHA512_C */
7052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007057
7058 /*
7059 * Free our handshake params
7060 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007061 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007063 ssl->handshake = NULL;
7064
7065 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007066 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007067 */
7068 if( ssl->transform )
7069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070 mbedtls_ssl_transform_free( ssl->transform );
7071 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007072 }
7073 ssl->transform = ssl->transform_negotiate;
7074 ssl->transform_negotiate = NULL;
7075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007077}
7078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007080{
7081 int resume = ssl->handshake->resume;
7082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085#if defined(MBEDTLS_SSL_RENEGOTIATION)
7086 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007089 ssl->renego_records_seen = 0;
7090 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007091#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007092
7093 /*
7094 * Free the previous session and switch in the current one
7095 */
Paul Bakker0a597072012-09-25 21:55:46 +00007096 if( ssl->session )
7097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007099 /* RFC 7366 3.1: keep the EtM state */
7100 ssl->session_negotiate->encrypt_then_mac =
7101 ssl->session->encrypt_then_mac;
7102#endif
7103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 mbedtls_ssl_session_free( ssl->session );
7105 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007106 }
7107 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007108 ssl->session_negotiate = NULL;
7109
Paul Bakker0a597072012-09-25 21:55:46 +00007110 /*
7111 * Add cache entry
7112 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007113 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007114 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007115 resume == 0 )
7116 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007117 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007119 }
Paul Bakker0a597072012-09-25 21:55:46 +00007120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007122 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007123 ssl->handshake->flight != NULL )
7124 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007125 /* Cancel handshake timer */
7126 ssl_set_timer( ssl, 0 );
7127
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007128 /* Keep last flight around in case we need to resend it:
7129 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007131 }
7132 else
7133#endif
7134 ssl_handshake_wrapup_free_hs_transform( ssl );
7135
Paul Bakker48916f92012-09-16 19:57:18 +00007136 ssl->state++;
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007139}
7140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007142{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007143 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007146
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007147 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007148
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007149 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007150
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007151 /*
7152 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7153 * may define some other value. Currently (early 2016), no defined
7154 * ciphersuite does this (and this is unlikely to change as activity has
7155 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007160 ssl->verify_data_len = hash_len;
7161 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007162#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007163
Paul Bakker5121ce52009-01-03 21:22:43 +00007164 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7166 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007167
7168 /*
7169 * In case of session resuming, invert the client and server
7170 * ChangeCipherSpec messages order.
7171 */
Paul Bakker0a597072012-09-25 21:55:46 +00007172 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007175 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007177#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007179 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007181#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007182 }
7183 else
7184 ssl->state++;
7185
Paul Bakker48916f92012-09-16 19:57:18 +00007186 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007187 * Switch to our negotiated transform and session parameters for outbound
7188 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007193 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007194 {
7195 unsigned char i;
7196
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007197 /* Remember current epoch settings for resending */
7198 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007199 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007200
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007201 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007202 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007203
7204 /* Increment epoch */
7205 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007206 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007207 break;
7208
7209 /* The loop goes to its end iff the counter is wrapping */
7210 if( i == 0 )
7211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7213 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007214 }
7215 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007216 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007218#if defined(MBEDTLS_SSL_PROTO_TLS)
7219 {
7220 memset( ssl->cur_out_ctr, 0, 8 );
7221 }
7222#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007223
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007224 ssl->transform_out = ssl->transform_negotiate;
7225 ssl->session_out = ssl->session_negotiate;
7226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7228 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7233 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007234 }
7235 }
7236#endif
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007239 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007241#endif
7242
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007243 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007244 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007246 return( ret );
7247 }
7248
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007249#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007250 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007251 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7252 {
7253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7254 return( ret );
7255 }
7256#endif
7257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007259
7260 return( 0 );
7261}
7262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007264#define SSL_MAX_HASH_LEN 36
7265#else
7266#define SSL_MAX_HASH_LEN 12
7267#endif
7268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007270{
Paul Bakker23986e52011-04-24 08:57:21 +00007271 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007272 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007273 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007276
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007277 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
Hanno Becker327c93b2018-08-15 13:56:18 +01007279 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007282 return( ret );
7283 }
7284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007288 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7289 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007291 }
7292
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007293 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294#if defined(MBEDTLS_SSL_PROTO_SSL3)
7295 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007296 hash_len = 36;
7297 else
7298#endif
7299 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7302 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007305 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7306 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007308 }
7309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007311 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007314 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7315 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007317 }
7318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007320 ssl->verify_data_len = hash_len;
7321 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007322#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007323
Paul Bakker0a597072012-09-25 21:55:46 +00007324 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007327 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007329#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007331 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007334 }
7335 else
7336 ssl->state++;
7337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007339 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007341#endif
7342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007344
7345 return( 0 );
7346}
7347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7353 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7354 mbedtls_md5_init( &handshake->fin_md5 );
7355 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007356 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7357 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007358#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7360#if defined(MBEDTLS_SHA256_C)
7361 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007362 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364#if defined(MBEDTLS_SHA512_C)
7365 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007366 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007367#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007369
7370 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007371
7372#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7373 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7374 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7375#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#if defined(MBEDTLS_DHM_C)
7378 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007379#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if defined(MBEDTLS_ECDH_C)
7381 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007382#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007383#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007384 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007385#if defined(MBEDTLS_SSL_CLI_C)
7386 handshake->ecjpake_cache = NULL;
7387 handshake->ecjpake_cache_len = 0;
7388#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007389#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007390
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007391#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007392 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007393#endif
7394
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007395#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7396 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7397#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007398}
7399
Hanno Becker611a83b2018-01-03 14:27:32 +00007400void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7405 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007406
Hanno Becker92231322018-01-03 15:32:51 +00007407#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 mbedtls_md_init( &transform->md_ctx_enc );
7409 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007410#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007411}
7412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007416}
7417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007419{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007420 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007421 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007423 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007425 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007426 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007427
7428 /*
7429 * Either the pointers are now NULL or cleared properly and can be freed.
7430 * Now allocate missing structures.
7431 */
7432 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007433 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007434 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007435 }
Paul Bakker48916f92012-09-16 19:57:18 +00007436
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007437 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007438 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007439 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007440 }
Paul Bakker48916f92012-09-16 19:57:18 +00007441
Paul Bakker82788fb2014-10-20 13:59:19 +02007442 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007443 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007444 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007445 }
Paul Bakker48916f92012-09-16 19:57:18 +00007446
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007447 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007448 if( ssl->handshake == NULL ||
7449 ssl->transform_negotiate == NULL ||
7450 ssl->session_negotiate == NULL )
7451 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 mbedtls_free( ssl->handshake );
7455 mbedtls_free( ssl->transform_negotiate );
7456 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007457
7458 ssl->handshake = NULL;
7459 ssl->transform_negotiate = NULL;
7460 ssl->session_negotiate = NULL;
7461
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007462 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007463 }
7464
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007465 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007467 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007468 ssl_handshake_params_init( ssl->handshake );
7469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007471 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007472 {
7473 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007474
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007475 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7476 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7477 else
7478 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007479
7480 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007481 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007482#endif
7483
Paul Bakker48916f92012-09-16 19:57:18 +00007484 return( 0 );
7485}
7486
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007487#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007488/* Dummy cookie callbacks for defaults */
7489static int ssl_cookie_write_dummy( void *ctx,
7490 unsigned char **p, unsigned char *end,
7491 const unsigned char *cli_id, size_t cli_id_len )
7492{
7493 ((void) ctx);
7494 ((void) p);
7495 ((void) end);
7496 ((void) cli_id);
7497 ((void) cli_id_len);
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007500}
7501
7502static int ssl_cookie_check_dummy( void *ctx,
7503 const unsigned char *cookie, size_t cookie_len,
7504 const unsigned char *cli_id, size_t cli_id_len )
7505{
7506 ((void) ctx);
7507 ((void) cookie);
7508 ((void) cookie_len);
7509 ((void) cli_id);
7510 ((void) cli_id_len);
7511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007513}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007514#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007515
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007516/* Once ssl->out_hdr as the address of the beginning of the
7517 * next outgoing record is set, deduce the other pointers.
7518 *
7519 * Note: For TLS, we save the implicit record sequence number
7520 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7521 * and the caller has to make sure there's space for this.
7522 */
7523
7524static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7525 mbedtls_ssl_transform *transform )
7526{
7527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007528 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007529 {
7530 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007531#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007532 ssl->out_cid = ssl->out_ctr + 8;
7533 ssl->out_len = ssl->out_cid;
7534 if( transform != NULL )
7535 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007536#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007537 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007538#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007539 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007540 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007541 MBEDTLS_SSL_TRANSPORT_ELSE
7542#endif /* MBEDTLS_SSL_PROTO_DTLS */
7543#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007544 {
7545 ssl->out_ctr = ssl->out_hdr - 8;
7546 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007547#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007548 ssl->out_cid = ssl->out_len;
7549#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007550 ssl->out_iv = ssl->out_hdr + 5;
7551 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007552#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007553
7554 /* Adjust out_msg to make space for explicit IV, if used. */
7555 if( transform != NULL &&
7556 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7557 {
7558 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7559 }
7560 else
7561 ssl->out_msg = ssl->out_iv;
7562}
7563
7564/* Once ssl->in_hdr as the address of the beginning of the
7565 * next incoming record is set, deduce the other pointers.
7566 *
7567 * Note: For TLS, we save the implicit record sequence number
7568 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7569 * and the caller has to make sure there's space for this.
7570 */
7571
Hanno Beckerf5970a02019-05-08 09:38:41 +01007572static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007573{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007574 /* This function sets the pointers to match the case
7575 * of unprotected TLS/DTLS records, with both ssl->in_iv
7576 * and ssl->in_msg pointing to the beginning of the record
7577 * content.
7578 *
7579 * When decrypting a protected record, ssl->in_msg
7580 * will be shifted to point to the beginning of the
7581 * record plaintext.
7582 */
7583
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007584#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007585 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007586 {
Hanno Becker70e79282019-05-03 14:34:53 +01007587 /* This sets the header pointers to match records
7588 * without CID. When we receive a record containing
7589 * a CID, the fields are shifted accordingly in
7590 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007591 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007592#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007593 ssl->in_cid = ssl->in_ctr + 8;
7594 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007595#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007596 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007597#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007598 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007599 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007600 MBEDTLS_SSL_TRANSPORT_ELSE
7601#endif /* MBEDTLS_SSL_PROTO_DTLS */
7602#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007603 {
7604 ssl->in_ctr = ssl->in_hdr - 8;
7605 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007606#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007607 ssl->in_cid = ssl->in_len;
7608#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007609 ssl->in_iv = ssl->in_hdr + 5;
7610 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007611#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007612
Hanno Beckerf5970a02019-05-08 09:38:41 +01007613 /* This will be adjusted at record decryption time. */
7614 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007615}
7616
Paul Bakker5121ce52009-01-03 21:22:43 +00007617/*
7618 * Initialize an SSL context
7619 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007620void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7621{
7622 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7623}
7624
7625/*
7626 * Setup an SSL context
7627 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007628
7629static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7630{
7631 /* Set the incoming and outgoing record pointers. */
7632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007633 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007634 {
7635 ssl->out_hdr = ssl->out_buf;
7636 ssl->in_hdr = ssl->in_buf;
7637 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007638 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007639#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007640#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007641 {
7642 ssl->out_hdr = ssl->out_buf + 8;
7643 ssl->in_hdr = ssl->in_buf + 8;
7644 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007645#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007646
7647 /* Derive other internal pointers. */
7648 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007649 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007650}
7651
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007652int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007653 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007654{
Paul Bakker48916f92012-09-16 19:57:18 +00007655 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007656
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007657 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007658
7659 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007660 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007661 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007662
7663 /* Set to NULL in case of an error condition */
7664 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007665
Angus Grattond8213d02016-05-25 20:56:48 +10007666 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7667 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007668 {
Angus Grattond8213d02016-05-25 20:56:48 +10007669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007670 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007671 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007672 }
7673
7674 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7675 if( ssl->out_buf == NULL )
7676 {
7677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007678 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007679 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007680 }
7681
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007682 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007683
Paul Bakker48916f92012-09-16 19:57:18 +00007684 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007685 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007686
7687 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007688
7689error:
7690 mbedtls_free( ssl->in_buf );
7691 mbedtls_free( ssl->out_buf );
7692
7693 ssl->conf = NULL;
7694
7695 ssl->in_buf = NULL;
7696 ssl->out_buf = NULL;
7697
7698 ssl->in_hdr = NULL;
7699 ssl->in_ctr = NULL;
7700 ssl->in_len = NULL;
7701 ssl->in_iv = NULL;
7702 ssl->in_msg = NULL;
7703
7704 ssl->out_hdr = NULL;
7705 ssl->out_ctr = NULL;
7706 ssl->out_len = NULL;
7707 ssl->out_iv = NULL;
7708 ssl->out_msg = NULL;
7709
k-stachowiak9f7798e2018-07-31 16:52:32 +02007710 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007711}
7712
7713/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007714 * Reset an initialized and used SSL context for re-use while retaining
7715 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007716 *
7717 * If partial is non-zero, keep data in the input buffer and client ID.
7718 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007719 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007720static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007721{
Paul Bakker48916f92012-09-16 19:57:18 +00007722 int ret;
7723
Hanno Becker7e772132018-08-10 12:38:21 +01007724#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7725 !defined(MBEDTLS_SSL_SRV_C)
7726 ((void) partial);
7727#endif
7728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007730
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007731 /* Cancel any possibly running timer */
7732 ssl_set_timer( ssl, 0 );
7733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734#if defined(MBEDTLS_SSL_RENEGOTIATION)
7735 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007736 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007737
7738 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7740 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007743
Paul Bakker7eb013f2011-10-06 12:37:39 +00007744 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007745 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007746
7747 ssl->in_msgtype = 0;
7748 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007750 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007751 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007754 ssl_dtls_replay_reset( ssl );
7755#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007756
7757 ssl->in_hslen = 0;
7758 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007759
7760 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007761
7762 ssl->out_msgtype = 0;
7763 ssl->out_msglen = 0;
7764 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7766 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007767 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007768#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007769
Hanno Becker19859472018-08-06 09:40:20 +01007770 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7771
Paul Bakker48916f92012-09-16 19:57:18 +00007772 ssl->transform_in = NULL;
7773 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007774
Hanno Becker78640902018-08-13 16:35:15 +01007775 ssl->session_in = NULL;
7776 ssl->session_out = NULL;
7777
Angus Grattond8213d02016-05-25 20:56:48 +10007778 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007779
7780#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007781 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007782#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7783 {
7784 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007785 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007786 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7789 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7792 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7795 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007796 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007797 }
7798#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007799
Paul Bakker48916f92012-09-16 19:57:18 +00007800 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 mbedtls_ssl_transform_free( ssl->transform );
7803 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007804 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007805 }
Paul Bakker48916f92012-09-16 19:57:18 +00007806
Paul Bakkerc0463502013-02-14 11:19:38 +01007807 if( ssl->session )
7808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 mbedtls_ssl_session_free( ssl->session );
7810 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007811 ssl->session = NULL;
7812 }
7813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007815 ssl->alpn_chosen = NULL;
7816#endif
7817
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007818#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007819#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007820 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007821#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007822 {
7823 mbedtls_free( ssl->cli_id );
7824 ssl->cli_id = NULL;
7825 ssl->cli_id_len = 0;
7826 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007827#endif
7828
Paul Bakker48916f92012-09-16 19:57:18 +00007829 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7830 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007831
7832 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007833}
7834
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007835/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007836 * Reset an initialized and used SSL context for re-use while retaining
7837 * all application-set variables, function pointers and data.
7838 */
7839int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7840{
7841 return( ssl_session_reset_int( ssl, 0 ) );
7842}
7843
7844/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007845 * SSL set accessors
7846 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007847void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007848{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007849 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007850}
7851
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007852void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007853{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007854 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007855}
7856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007858void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007859{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007860 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007861}
7862#endif
7863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007865void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007866{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007867 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007868}
7869#endif
7870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007872
Hanno Becker1841b0a2018-08-24 11:13:57 +01007873void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7874 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007875{
7876 ssl->disable_datagram_packing = !allow_packing;
7877}
7878
7879void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7880 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007881{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007882 conf->hs_timeout_min = min;
7883 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007884}
7885#endif
7886
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007887void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007888{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007889 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007890}
7891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007893void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007894 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007895 void *p_vrfy )
7896{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007897 conf->f_vrfy = f_vrfy;
7898 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007901
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007902void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007903 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007904 void *p_rng )
7905{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007906 conf->f_rng = f_rng;
7907 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007908}
7909
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007910void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007911 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007912 void *p_dbg )
7913{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007914 conf->f_dbg = f_dbg;
7915 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007916}
7917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007919 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007920 mbedtls_ssl_send_t *f_send,
7921 mbedtls_ssl_recv_t *f_recv,
7922 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007923{
7924 ssl->p_bio = p_bio;
7925 ssl->f_send = f_send;
7926 ssl->f_recv = f_recv;
7927 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007928}
7929
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007930#if defined(MBEDTLS_SSL_PROTO_DTLS)
7931void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7932{
7933 ssl->mtu = mtu;
7934}
7935#endif
7936
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007937void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007938{
7939 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007940}
7941
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007942void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7943 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007944 mbedtls_ssl_set_timer_t *f_set_timer,
7945 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007946{
7947 ssl->p_timer = p_timer;
7948 ssl->f_set_timer = f_set_timer;
7949 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007950
7951 /* Make sure we start with no timer running */
7952 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007953}
7954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007956void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007957 void *p_cache,
7958 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7959 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007960{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007961 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007962 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007963 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007964}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967#if defined(MBEDTLS_SSL_CLI_C)
7968int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007969{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007970 int ret;
7971
7972 if( ssl == NULL ||
7973 session == NULL ||
7974 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007975 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007978 }
7979
7980 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7981 return( ret );
7982
Paul Bakker0a597072012-09-25 21:55:46 +00007983 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007984
7985 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007988
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007989void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007990 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007991{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007992 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7993 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7994 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7995 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007996}
7997
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007998void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007999 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008000 int major, int minor )
8001{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008003 return;
8004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008006 return;
8007
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008008 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008009}
8010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008012void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008013 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008014{
8015 conf->cert_profile = profile;
8016}
8017
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008018/* Append a new keycert entry to a (possibly empty) list */
8019static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8020 mbedtls_x509_crt *cert,
8021 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008022{
niisato8ee24222018-06-25 19:05:48 +09008023 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008024
niisato8ee24222018-06-25 19:05:48 +09008025 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8026 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008027 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008028
niisato8ee24222018-06-25 19:05:48 +09008029 new_cert->cert = cert;
8030 new_cert->key = key;
8031 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008032
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008033 /* Update head is the list was null, else add to the end */
8034 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008035 {
niisato8ee24222018-06-25 19:05:48 +09008036 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008037 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008038 else
8039 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008040 mbedtls_ssl_key_cert *cur = *head;
8041 while( cur->next != NULL )
8042 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008043 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008044 }
8045
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008046 return( 0 );
8047}
8048
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008049int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008050 mbedtls_x509_crt *own_cert,
8051 mbedtls_pk_context *pk_key )
8052{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008053 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008054}
8055
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008056void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008057 mbedtls_x509_crt *ca_chain,
8058 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008059{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008060 conf->ca_chain = ca_chain;
8061 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008064
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008065#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8066int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8067 mbedtls_x509_crt *own_cert,
8068 mbedtls_pk_context *pk_key )
8069{
8070 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8071 own_cert, pk_key ) );
8072}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008073
8074void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8075 mbedtls_x509_crt *ca_chain,
8076 mbedtls_x509_crl *ca_crl )
8077{
8078 ssl->handshake->sni_ca_chain = ca_chain;
8079 ssl->handshake->sni_ca_crl = ca_crl;
8080}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008081
8082void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8083 int authmode )
8084{
8085 ssl->handshake->sni_authmode = authmode;
8086}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008087#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8088
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008089#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008090/*
8091 * Set EC J-PAKE password for current handshake
8092 */
8093int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8094 const unsigned char *pw,
8095 size_t pw_len )
8096{
8097 mbedtls_ecjpake_role role;
8098
Janos Follath8eb64132016-06-03 15:40:57 +01008099 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8101
8102 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8103 role = MBEDTLS_ECJPAKE_SERVER;
8104 else
8105 role = MBEDTLS_ECJPAKE_CLIENT;
8106
8107 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8108 role,
8109 MBEDTLS_MD_SHA256,
8110 MBEDTLS_ECP_DP_SECP256R1,
8111 pw, pw_len ) );
8112}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008113#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008116int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008117 const unsigned char *psk, size_t psk_len,
8118 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008119{
Paul Bakker6db455e2013-09-18 17:29:31 +02008120 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008125
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008126 /* Identity len will be encoded on two bytes */
8127 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008128 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008129 {
8130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8131 }
8132
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008133 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008134 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008135 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008136
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008137 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008138 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008139 conf->psk_len = 0;
8140 }
8141 if( conf->psk_identity != NULL )
8142 {
8143 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008144 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008145 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008146 }
8147
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008148 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8149 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008150 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008151 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008152 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008153 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008154 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008155 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008156 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008157
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008158 conf->psk_len = psk_len;
8159 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008160
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008161 memcpy( conf->psk, psk, conf->psk_len );
8162 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008163
8164 return( 0 );
8165}
8166
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008167int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8168 const unsigned char *psk, size_t psk_len )
8169{
8170 if( psk == NULL || ssl->handshake == NULL )
8171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8172
8173 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8174 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8175
8176 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008177 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008178 mbedtls_platform_zeroize( ssl->handshake->psk,
8179 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008180 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008181 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008182 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008183
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008184 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008185 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008186
8187 ssl->handshake->psk_len = psk_len;
8188 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8189
8190 return( 0 );
8191}
8192
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008193void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008195 size_t),
8196 void *p_psk )
8197{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008198 conf->f_psk = f_psk;
8199 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008202
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008203#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008204
8205#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008206int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008207{
8208 int ret;
8209
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008210 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8211 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8212 {
8213 mbedtls_mpi_free( &conf->dhm_P );
8214 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008215 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008216 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008217
8218 return( 0 );
8219}
Hanno Becker470a8c42017-10-04 15:28:46 +01008220#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008221
Hanno Beckera90658f2017-10-04 15:29:08 +01008222int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8223 const unsigned char *dhm_P, size_t P_len,
8224 const unsigned char *dhm_G, size_t G_len )
8225{
8226 int ret;
8227
8228 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8229 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8230 {
8231 mbedtls_mpi_free( &conf->dhm_P );
8232 mbedtls_mpi_free( &conf->dhm_G );
8233 return( ret );
8234 }
8235
8236 return( 0 );
8237}
Paul Bakker5121ce52009-01-03 21:22:43 +00008238
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008239int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008240{
8241 int ret;
8242
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008243 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8244 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8245 {
8246 mbedtls_mpi_free( &conf->dhm_P );
8247 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008248 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008249 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008250
8251 return( 0 );
8252}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008253#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008254
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008255#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8256/*
8257 * Set the minimum length for Diffie-Hellman parameters
8258 */
8259void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8260 unsigned int bitlen )
8261{
8262 conf->dhm_min_bitlen = bitlen;
8263}
8264#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8265
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008266#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008267/*
8268 * Set allowed/preferred hashes for handshake signatures
8269 */
8270void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8271 const int *hashes )
8272{
8273 conf->sig_hashes = hashes;
8274}
Hanno Becker947194e2017-04-07 13:25:49 +01008275#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008276
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008277#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008278/*
8279 * Set the allowed elliptic curves
8280 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008281void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008282 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008283{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008284 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008285}
Hanno Becker947194e2017-04-07 13:25:49 +01008286#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008287
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008290{
Hanno Becker947194e2017-04-07 13:25:49 +01008291 /* Initialize to suppress unnecessary compiler warning */
8292 size_t hostname_len = 0;
8293
8294 /* Check if new hostname is valid before
8295 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008296 if( hostname != NULL )
8297 {
8298 hostname_len = strlen( hostname );
8299
8300 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8302 }
8303
8304 /* Now it's clear that we will overwrite the old hostname,
8305 * so we can free it safely */
8306
8307 if( ssl->hostname != NULL )
8308 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008309 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008310 mbedtls_free( ssl->hostname );
8311 }
8312
8313 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008314
Paul Bakker5121ce52009-01-03 21:22:43 +00008315 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008316 {
8317 ssl->hostname = NULL;
8318 }
8319 else
8320 {
8321 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008322 if( ssl->hostname == NULL )
8323 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008324
Hanno Becker947194e2017-04-07 13:25:49 +01008325 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008326
Hanno Becker947194e2017-04-07 13:25:49 +01008327 ssl->hostname[hostname_len] = '\0';
8328 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008329
8330 return( 0 );
8331}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008332#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008333
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008334#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008335void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008337 const unsigned char *, size_t),
8338 void *p_sni )
8339{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008340 conf->f_sni = f_sni;
8341 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008346int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008347{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008348 size_t cur_len, tot_len;
8349 const char **p;
8350
8351 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008352 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8353 * MUST NOT be truncated."
8354 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008355 */
8356 tot_len = 0;
8357 for( p = protos; *p != NULL; p++ )
8358 {
8359 cur_len = strlen( *p );
8360 tot_len += cur_len;
8361
8362 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008364 }
8365
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008366 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008367
8368 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008369}
8370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008372{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008373 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008376
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008377void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008378{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008379 conf->max_major_ver = major;
8380 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008381}
8382
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008383void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008384{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008385 conf->min_major_ver = major;
8386 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008387}
8388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008390void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008391{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008392 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008393}
8394#endif
8395
Janos Follath088ce432017-04-10 12:42:31 +01008396#if defined(MBEDTLS_SSL_SRV_C)
8397void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8398 char cert_req_ca_list )
8399{
8400 conf->cert_req_ca_list = cert_req_ca_list;
8401}
8402#endif
8403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008405void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008406{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008407 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008408}
8409#endif
8410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008412void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008413{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008414 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008415}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008416
8417void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008418 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008419{
8420 conf->enforce_extended_master_secret = ems_enf;
8421}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008422#endif
8423
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008424#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008425void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008426{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008427 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008428}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008429#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008432int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008433{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008435 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008438 }
8439
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008440 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008441
8442 return( 0 );
8443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008447void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008448{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008449 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008454void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008455{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008456 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008457}
8458#endif
8459
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008460void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008461{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008462 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008463}
8464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008466void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008467{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008468 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008469}
8470
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008471void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008472{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008473 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008474}
8475
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008476void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008477 const unsigned char period[8] )
8478{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008479 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008484#if defined(MBEDTLS_SSL_CLI_C)
8485void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008486{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008487 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008488}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008489#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008490
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008491#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008492void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8493 mbedtls_ssl_ticket_write_t *f_ticket_write,
8494 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8495 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008496{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008497 conf->f_ticket_write = f_ticket_write;
8498 conf->f_ticket_parse = f_ticket_parse;
8499 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008500}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008503
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008504#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8505void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8506 mbedtls_ssl_export_keys_t *f_export_keys,
8507 void *p_export_keys )
8508{
8509 conf->f_export_keys = f_export_keys;
8510 conf->p_export_keys = p_export_keys;
8511}
8512#endif
8513
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008514#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008515void mbedtls_ssl_conf_async_private_cb(
8516 mbedtls_ssl_config *conf,
8517 mbedtls_ssl_async_sign_t *f_async_sign,
8518 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8519 mbedtls_ssl_async_resume_t *f_async_resume,
8520 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008521 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008522{
8523 conf->f_async_sign_start = f_async_sign;
8524 conf->f_async_decrypt_start = f_async_decrypt;
8525 conf->f_async_resume = f_async_resume;
8526 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008527 conf->p_async_config_data = async_config_data;
8528}
8529
Gilles Peskine8f97af72018-04-26 11:46:10 +02008530void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8531{
8532 return( conf->p_async_config_data );
8533}
8534
Gilles Peskine1febfef2018-04-30 11:54:39 +02008535void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008536{
8537 if( ssl->handshake == NULL )
8538 return( NULL );
8539 else
8540 return( ssl->handshake->user_async_ctx );
8541}
8542
Gilles Peskine1febfef2018-04-30 11:54:39 +02008543void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008544 void *ctx )
8545{
8546 if( ssl->handshake != NULL )
8547 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008548}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008549#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008550
Paul Bakker5121ce52009-01-03 21:22:43 +00008551/*
8552 * SSL get accessors
8553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008555{
8556 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8557}
8558
Hanno Becker8b170a02017-10-10 11:51:19 +01008559int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8560{
8561 /*
8562 * Case A: We're currently holding back
8563 * a message for further processing.
8564 */
8565
8566 if( ssl->keep_current_message == 1 )
8567 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008569 return( 1 );
8570 }
8571
8572 /*
8573 * Case B: Further records are pending in the current datagram.
8574 */
8575
8576#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008577 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008578 ssl->in_left > ssl->next_record_offset )
8579 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008581 return( 1 );
8582 }
8583#endif /* MBEDTLS_SSL_PROTO_DTLS */
8584
8585 /*
8586 * Case C: A handshake message is being processed.
8587 */
8588
Hanno Becker8b170a02017-10-10 11:51:19 +01008589 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8590 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008591 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008592 return( 1 );
8593 }
8594
8595 /*
8596 * Case D: An application data message is being processed
8597 */
8598 if( ssl->in_offt != NULL )
8599 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008601 return( 1 );
8602 }
8603
8604 /*
8605 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008606 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008607 * we implement support for multiple alerts in single records.
8608 */
8609
8610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8611 return( 0 );
8612}
8613
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008614uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008615{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008616 if( ssl->session != NULL )
8617 return( ssl->session->verify_result );
8618
8619 if( ssl->session_negotiate != NULL )
8620 return( ssl->session_negotiate->verify_result );
8621
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008622 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008623}
8624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008626{
Paul Bakker926c8e42013-03-06 10:23:34 +01008627 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008628 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008631}
8632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008636 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008637 {
8638 switch( ssl->minor_ver )
8639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008641 return( "DTLSv1.0" );
8642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008644 return( "DTLSv1.2" );
8645
8646 default:
8647 return( "unknown (DTLS)" );
8648 }
8649 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008650 MBEDTLS_SSL_TRANSPORT_ELSE
8651#endif /* MBEDTLS_SSL_PROTO_DTLS */
8652#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008653 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008654 switch( ssl->minor_ver )
8655 {
8656 case MBEDTLS_SSL_MINOR_VERSION_0:
8657 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008658
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008659 case MBEDTLS_SSL_MINOR_VERSION_1:
8660 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008661
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008662 case MBEDTLS_SSL_MINOR_VERSION_2:
8663 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008664
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008665 case MBEDTLS_SSL_MINOR_VERSION_3:
8666 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008667
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008668 default:
8669 return( "unknown" );
8670 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008671 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008672#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008673}
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008676{
Hanno Becker3136ede2018-08-17 15:28:19 +01008677 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008679 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008680
Hanno Becker43395762019-05-03 14:46:38 +01008681 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8682
Hanno Becker78640902018-08-13 16:35:15 +01008683 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008684 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#if defined(MBEDTLS_ZLIB_SUPPORT)
8687 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8688 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008689#endif
8690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693 case MBEDTLS_MODE_GCM:
8694 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008695 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008697 transform_expansion = transform->minlen;
8698 break;
8699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008701
8702 block_size = mbedtls_cipher_get_block_size(
8703 &transform->cipher_ctx_enc );
8704
Hanno Becker3136ede2018-08-17 15:28:19 +01008705 /* Expansion due to the addition of the MAC. */
8706 transform_expansion += transform->maclen;
8707
8708 /* Expansion due to the addition of CBC padding;
8709 * Theoretically up to 256 bytes, but we never use
8710 * more than the block size of the underlying cipher. */
8711 transform_expansion += block_size;
8712
8713 /* For TLS 1.1 or higher, an explicit IV is added
8714 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008715#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8716 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008717 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008718#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008719
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008720 break;
8721
8722 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008725 }
8726
Hanno Beckera5a2b082019-05-15 14:03:01 +01008727#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008728 if( transform->out_cid_len != 0 )
8729 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008730#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008731
Hanno Becker43395762019-05-03 14:46:38 +01008732 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008733}
8734
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008735#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8736size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8737{
8738 size_t max_len;
8739
8740 /*
8741 * Assume mfl_code is correct since it was checked when set
8742 */
Angus Grattond8213d02016-05-25 20:56:48 +10008743 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008744
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008745 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008746 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008747 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008748 {
Angus Grattond8213d02016-05-25 20:56:48 +10008749 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008750 }
8751
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008752 /* During a handshake, use the value being negotiated */
8753 if( ssl->session_negotiate != NULL &&
8754 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8755 {
8756 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8757 }
8758
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008759 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008760}
8761#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8762
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008763#if defined(MBEDTLS_SSL_PROTO_DTLS)
8764static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8765{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008766 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8767 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8768 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8769 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8770 return ( 0 );
8771
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008772 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8773 return( ssl->mtu );
8774
8775 if( ssl->mtu == 0 )
8776 return( ssl->handshake->mtu );
8777
8778 return( ssl->mtu < ssl->handshake->mtu ?
8779 ssl->mtu : ssl->handshake->mtu );
8780}
8781#endif /* MBEDTLS_SSL_PROTO_DTLS */
8782
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008783int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8784{
8785 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8786
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008787#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8788 !defined(MBEDTLS_SSL_PROTO_DTLS)
8789 (void) ssl;
8790#endif
8791
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008792#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8793 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8794
8795 if( max_len > mfl )
8796 max_len = mfl;
8797#endif
8798
8799#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008800 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008801 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008802 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008803 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8804 const size_t overhead = (size_t) ret;
8805
8806 if( ret < 0 )
8807 return( ret );
8808
8809 if( mtu <= overhead )
8810 {
8811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8812 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8813 }
8814
8815 if( max_len > mtu - overhead )
8816 max_len = mtu - overhead;
8817 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008818#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008819
Hanno Becker0defedb2018-08-10 12:35:02 +01008820#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8821 !defined(MBEDTLS_SSL_PROTO_DTLS)
8822 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008823#endif
8824
8825 return( (int) max_len );
8826}
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828#if defined(MBEDTLS_X509_CRT_PARSE_C)
8829const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008830{
8831 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008832 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008833
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008834 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00008839int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8840 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008841{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008842 if( ssl == NULL ||
8843 dst == NULL ||
8844 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008845 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008848 }
8849
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008850 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008853
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008854const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8855{
8856 if( ssl == NULL )
8857 return( NULL );
8858
8859 return( ssl->session );
8860}
8861
Paul Bakker5121ce52009-01-03 21:22:43 +00008862/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008863 * Define ticket header determining Mbed TLS version
8864 * and structure of the ticket.
8865 */
8866
Hanno Becker41527622019-05-16 12:50:45 +01008867/*
Hanno Becker26829e92019-05-28 14:30:45 +01008868 * Define bitflag determining compile-time settings influencing
8869 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008870 */
8871
Hanno Becker26829e92019-05-28 14:30:45 +01008872#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008873#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008874#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008875#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008876#endif /* MBEDTLS_HAVE_TIME */
8877
8878#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008879#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008880#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008881#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008882#endif /* MBEDTLS_X509_CRT_PARSE_C */
8883
8884#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008885#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008886#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008887#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008888#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8889
8890#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008891#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008892#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008893#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008894#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8895
8896#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008897#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008898#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008899#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008900#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8901
8902#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008903#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008904#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008905#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008906#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8907
Hanno Becker41527622019-05-16 12:50:45 +01008908#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8909#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8910#else
8911#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8912#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8913
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008914#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8915#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8916#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8917#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8918#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8919#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8920#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008921
Hanno Becker26829e92019-05-28 14:30:45 +01008922#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008923 ( (uint16_t) ( \
8924 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8925 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8926 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8927 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8928 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8929 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008930 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008931
Hanno Becker557fe9f2019-05-16 12:41:07 +01008932static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008933 MBEDTLS_VERSION_MAJOR,
8934 MBEDTLS_VERSION_MINOR,
8935 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008936 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8937 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008938};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008939
8940/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008941 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008942 * (in the presentation language of TLS, RFC 8446 section 3)
8943 *
Hanno Becker26829e92019-05-28 14:30:45 +01008944 * opaque mbedtls_version[3]; // major, minor, patch
8945 * opaque session_format[2]; // version-specific 16-bit field determining
8946 * // the format of the remaining
8947 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008948 *
8949 * Note: When updating the format, remember to keep
8950 * these version+format bytes.
8951 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008952 * // In this version, `session_format` determines
8953 * // the setting of those compile-time
8954 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008955 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008956 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008957 * uint8 ciphersuite[2]; // defined by the standard
8958 * uint8 compression; // 0 or 1
8959 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008960 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008961 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008962 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008963 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8964 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008965 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008966 * uint8 mfl_code; // up to 255 according to standard
8967 * uint8 trunc_hmac; // 0 or 1
8968 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008969 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008970 * The order is the same as in the definition of the structure, except
8971 * verify_result is put before peer_cert so that all mandatory fields come
8972 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008973 */
8974int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8975 unsigned char *buf,
8976 size_t buf_len,
8977 size_t *olen )
8978{
8979 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008980 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008981#if defined(MBEDTLS_HAVE_TIME)
8982 uint64_t start;
8983#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008984#if defined(MBEDTLS_X509_CRT_PARSE_C)
8985 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008986#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008987
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008988 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008989 * Add version identifier
8990 */
8991
8992 used += sizeof( ssl_serialized_session_header );
8993
8994 if( used <= buf_len )
8995 {
8996 memcpy( p, ssl_serialized_session_header,
8997 sizeof( ssl_serialized_session_header ) );
8998 p += sizeof( ssl_serialized_session_header );
8999 }
9000
9001 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009002 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009003 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009004#if defined(MBEDTLS_HAVE_TIME)
9005 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009006
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009007 if( used <= buf_len )
9008 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009009 start = (uint64_t) session->start;
9010
9011 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9012 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9013 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9014 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9015 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9016 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9017 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9018 *p++ = (unsigned char)( ( start ) & 0xFF );
9019 }
9020#endif /* MBEDTLS_HAVE_TIME */
9021
9022 /*
9023 * Basic mandatory fields
9024 */
9025 used += 2 /* ciphersuite */
9026 + 1 /* compression */
9027 + 1 /* id_len */
9028 + sizeof( session->id )
9029 + sizeof( session->master )
9030 + 4; /* verify_result */
9031
9032 if( used <= buf_len )
9033 {
9034 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9035 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9036
9037 *p++ = (unsigned char)( session->compression & 0xFF );
9038
9039 *p++ = (unsigned char)( session->id_len & 0xFF );
9040 memcpy( p, session->id, 32 );
9041 p += 32;
9042
9043 memcpy( p, session->master, 48 );
9044 p += 48;
9045
9046 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9047 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9048 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9049 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009050 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009051
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009052 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009053 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009054 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009055#if defined(MBEDTLS_X509_CRT_PARSE_C)
9056 if( session->peer_cert == NULL )
9057 cert_len = 0;
9058 else
9059 cert_len = session->peer_cert->raw.len;
9060
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009061 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009062
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009063 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009064 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009065 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9066 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9067 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9068
9069 if( session->peer_cert != NULL )
9070 {
9071 memcpy( p, session->peer_cert->raw.p, cert_len );
9072 p += cert_len;
9073 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009074 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009075#endif /* MBEDTLS_X509_CRT_PARSE_C */
9076
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009077 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009078 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009079 */
9080#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009081 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009082
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009083 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009084 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009085 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9086 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9087 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9088
9089 if( session->ticket != NULL )
9090 {
9091 memcpy( p, session->ticket, session->ticket_len );
9092 p += session->ticket_len;
9093 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009094
9095 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9096 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9097 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9098 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009099 }
9100#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9101
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009102 /*
9103 * Misc extension-related info
9104 */
9105#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9106 used += 1;
9107
9108 if( used <= buf_len )
9109 *p++ = session->mfl_code;
9110#endif
9111
9112#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9113 used += 1;
9114
9115 if( used <= buf_len )
9116 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9117#endif
9118
9119#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9120 used += 1;
9121
9122 if( used <= buf_len )
9123 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9124#endif
9125
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009126 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009127 *olen = used;
9128
9129 if( used > buf_len )
9130 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009131
9132 return( 0 );
9133}
9134
9135/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009136 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009137 *
9138 * This internal version is wrapped by a public function that cleans up in
9139 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009140 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009141static int ssl_session_load( mbedtls_ssl_session *session,
9142 const unsigned char *buf,
9143 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009144{
9145 const unsigned char *p = buf;
9146 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009147#if defined(MBEDTLS_HAVE_TIME)
9148 uint64_t start;
9149#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009150#if defined(MBEDTLS_X509_CRT_PARSE_C)
9151 size_t cert_len;
9152#endif /* MBEDTLS_X509_CRT_PARSE_C */
9153
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009154 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009155 * Check version identifier
9156 */
9157
9158 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009160
9161 if( memcmp( p, ssl_serialized_session_header,
9162 sizeof( ssl_serialized_session_header ) ) != 0 )
9163 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009164 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009165 }
9166 p += sizeof( ssl_serialized_session_header );
9167
9168 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009169 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009170 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009171#if defined(MBEDTLS_HAVE_TIME)
9172 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9174
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009175 start = ( (uint64_t) p[0] << 56 ) |
9176 ( (uint64_t) p[1] << 48 ) |
9177 ( (uint64_t) p[2] << 40 ) |
9178 ( (uint64_t) p[3] << 32 ) |
9179 ( (uint64_t) p[4] << 24 ) |
9180 ( (uint64_t) p[5] << 16 ) |
9181 ( (uint64_t) p[6] << 8 ) |
9182 ( (uint64_t) p[7] );
9183 p += 8;
9184
9185 session->start = (time_t) start;
9186#endif /* MBEDTLS_HAVE_TIME */
9187
9188 /*
9189 * Basic mandatory fields
9190 */
9191 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9193
9194 session->ciphersuite = ( p[0] << 8 ) | p[1];
9195 p += 2;
9196
9197 session->compression = *p++;
9198
9199 session->id_len = *p++;
9200 memcpy( session->id, p, 32 );
9201 p += 32;
9202
9203 memcpy( session->master, p, 48 );
9204 p += 48;
9205
9206 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9207 ( (uint32_t) p[1] << 16 ) |
9208 ( (uint32_t) p[2] << 8 ) |
9209 ( (uint32_t) p[3] );
9210 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009211
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009212 /* Immediately clear invalid pointer values that have been read, in case
9213 * we exit early before we replaced them with valid ones. */
9214#if defined(MBEDTLS_X509_CRT_PARSE_C)
9215 session->peer_cert = NULL;
9216#endif
9217#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9218 session->ticket = NULL;
9219#endif
9220
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009221 /*
9222 * Peer certificate
9223 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009224#if defined(MBEDTLS_X509_CRT_PARSE_C)
9225 if( 3 > (size_t)( end - p ) )
9226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9227
9228 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9229 p += 3;
9230
9231 if( cert_len == 0 )
9232 {
9233 session->peer_cert = NULL;
9234 }
9235 else
9236 {
9237 int ret;
9238
9239 if( cert_len > (size_t)( end - p ) )
9240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9241
9242 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9243
9244 if( session->peer_cert == NULL )
9245 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9246
9247 mbedtls_x509_crt_init( session->peer_cert );
9248
9249 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9250 p, cert_len ) ) != 0 )
9251 {
9252 mbedtls_x509_crt_free( session->peer_cert );
9253 mbedtls_free( session->peer_cert );
9254 session->peer_cert = NULL;
9255 return( ret );
9256 }
9257
9258 p += cert_len;
9259 }
9260#endif /* MBEDTLS_X509_CRT_PARSE_C */
9261
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009262 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009263 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009264 */
9265#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9266 if( 3 > (size_t)( end - p ) )
9267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9268
9269 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9270 p += 3;
9271
9272 if( session->ticket_len != 0 )
9273 {
9274 if( session->ticket_len > (size_t)( end - p ) )
9275 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9276
9277 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9278 if( session->ticket == NULL )
9279 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9280
9281 memcpy( session->ticket, p, session->ticket_len );
9282 p += session->ticket_len;
9283 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009284
9285 if( 4 > (size_t)( end - p ) )
9286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9287
9288 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9289 ( (uint32_t) p[1] << 16 ) |
9290 ( (uint32_t) p[2] << 8 ) |
9291 ( (uint32_t) p[3] );
9292 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009293#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9294
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009295 /*
9296 * Misc extension-related info
9297 */
9298#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9299 if( 1 > (size_t)( end - p ) )
9300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9301
9302 session->mfl_code = *p++;
9303#endif
9304
9305#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9306 if( 1 > (size_t)( end - p ) )
9307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9308
9309 session->trunc_hmac = *p++;
9310#endif
9311
9312#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9313 if( 1 > (size_t)( end - p ) )
9314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9315
9316 session->encrypt_then_mac = *p++;
9317#endif
9318
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009319 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009320 if( p != end )
9321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9322
9323 return( 0 );
9324}
9325
9326/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009327 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009328 */
9329int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9330 const unsigned char *buf,
9331 size_t len )
9332{
9333 int ret = ssl_session_load( session, buf, len );
9334
9335 if( ret != 0 )
9336 mbedtls_ssl_session_free( session );
9337
9338 return( ret );
9339}
9340
9341/*
Paul Bakker1961b702013-01-25 14:49:24 +01009342 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009345{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009347
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009348 if( ssl == NULL || ssl->conf == NULL )
9349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009352 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009354#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009356 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009358#endif
9359
Paul Bakker1961b702013-01-25 14:49:24 +01009360 return( ret );
9361}
9362
9363/*
9364 * Perform the SSL handshake
9365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009367{
9368 int ret = 0;
9369
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009370 if( ssl == NULL || ssl->conf == NULL )
9371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009378
9379 if( ret != 0 )
9380 break;
9381 }
9382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009384
9385 return( ret );
9386}
9387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388#if defined(MBEDTLS_SSL_RENEGOTIATION)
9389#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009390/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009391 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009394{
9395 int ret;
9396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009398
9399 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9401 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009402
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009403 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009404 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009406 return( ret );
9407 }
9408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009410
9411 return( 0 );
9412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009414
9415/*
9416 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417 * - any side: calling mbedtls_ssl_renegotiate(),
9418 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9419 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009420 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009421 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009425{
9426 int ret;
9427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009429
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009430 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9431 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009432
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009433 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9434 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009436 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009437 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009438 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009439 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009440 ssl->handshake->out_msg_seq = 1;
9441 else
9442 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009443 }
9444#endif
9445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9447 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009452 return( ret );
9453 }
9454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009456
9457 return( 0 );
9458}
9459
9460/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009461 * Renegotiate current connection on client,
9462 * or request renegotiation on server
9463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009467
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009468 if( ssl == NULL || ssl->conf == NULL )
9469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009472 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009473 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009479
9480 /* Did we already try/start sending HelloRequest? */
9481 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009482 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009483
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009484 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009489 /*
9490 * On client, either start the renegotiation process or,
9491 * if already in progress, continue the handshake
9492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009497
9498 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009501 return( ret );
9502 }
9503 }
9504 else
9505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009509 return( ret );
9510 }
9511 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009513
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009514 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009515}
9516
9517/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009518 * Check record counters and renegotiate if they're above the limit.
9519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009521{
Andres AG2196c7f2016-12-15 17:01:16 +00009522 size_t ep_len = ssl_ep_len( ssl );
9523 int in_ctr_cmp;
9524 int out_ctr_cmp;
9525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9527 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009528 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009529 {
9530 return( 0 );
9531 }
9532
Andres AG2196c7f2016-12-15 17:01:16 +00009533 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9534 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009535 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009536 ssl->conf->renego_period + ep_len, 8 - ep_len );
9537
9538 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009539 {
9540 return( 0 );
9541 }
9542
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009547
9548/*
9549 * Receive application data decrypted from the SSL layer
9550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009552{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009553 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009554 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009555
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009556 if( ssl == NULL || ssl->conf == NULL )
9557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009562 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009565 return( ret );
9566
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009567 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009569 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009570 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009571 return( ret );
9572 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009573 }
9574#endif
9575
Hanno Becker4a810fb2017-05-24 16:27:30 +01009576 /*
9577 * Check if renegotiation is necessary and/or handshake is
9578 * in process. If yes, perform/continue, and fall through
9579 * if an unexpected packet is received while the client
9580 * is waiting for the ServerHello.
9581 *
9582 * (There is no equivalent to the last condition on
9583 * the server-side as it is not treated as within
9584 * a handshake while waiting for the ClientHello
9585 * after a renegotiation request.)
9586 */
9587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009589 ret = ssl_check_ctr_renegotiate( ssl );
9590 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9591 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009594 return( ret );
9595 }
9596#endif
9597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009601 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9602 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009605 return( ret );
9606 }
9607 }
9608
Hanno Beckere41158b2017-10-23 13:30:32 +01009609 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009610 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009611 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009612 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009613 if( ssl->f_get_timer != NULL &&
9614 ssl->f_get_timer( ssl->p_timer ) == -1 )
9615 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009616 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009617 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009618
Hanno Becker327c93b2018-08-15 13:56:18 +01009619 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009620 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009621 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9622 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009623
Hanno Becker4a810fb2017-05-24 16:27:30 +01009624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9625 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009626 }
9627
9628 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009630 {
9631 /*
9632 * OpenSSL sends empty messages to randomize the IV
9633 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009634 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009637 return( 0 );
9638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009640 return( ret );
9641 }
9642 }
9643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009647
Hanno Becker4a810fb2017-05-24 16:27:30 +01009648 /*
9649 * - For client-side, expect SERVER_HELLO_REQUEST.
9650 * - For server-side, expect CLIENT_HELLO.
9651 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9652 */
9653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009655 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009657 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009660
9661 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009663 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009664 {
9665 continue;
9666 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009667 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009668#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009669#if defined(MBEDTLS_SSL_PROTO_TLS)
9670 {
9671 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9672 }
9673#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009674 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009675#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009676
Hanno Becker4a810fb2017-05-24 16:27:30 +01009677#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009678 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009682
9683 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009685 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009686 {
9687 continue;
9688 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009689 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009690#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009691#if defined(MBEDTLS_SSL_PROTO_TLS)
9692 {
9693 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9694 }
9695#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009696 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009697#endif /* MBEDTLS_SSL_SRV_C */
9698
Hanno Becker21df7f92017-10-17 11:03:26 +01009699#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009700 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009701 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9702 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9703 ssl->conf->allow_legacy_renegotiation ==
9704 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9705 {
9706 /*
9707 * Accept renegotiation request
9708 */
Paul Bakker48916f92012-09-16 19:57:18 +00009709
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009710 /* DTLS clients need to know renego is server-initiated */
9711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009712 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009713 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9714 {
9715 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9716 }
9717#endif
9718 ret = ssl_start_renegotiation( ssl );
9719 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9720 ret != 0 )
9721 {
9722 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9723 return( ret );
9724 }
9725 }
9726 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009727#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009728 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009729 /*
9730 * Refuse renegotiation
9731 */
9732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009735#if defined(MBEDTLS_SSL_PROTO_SSL3)
9736 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009737 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009738 /* SSLv3 does not have a "no_renegotiation" warning, so
9739 we send a fatal alert and abort the connection. */
9740 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9741 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9742 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009743 }
9744 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9746#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9747 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9748 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9751 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9752 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009753 {
9754 return( ret );
9755 }
Paul Bakker48916f92012-09-16 19:57:18 +00009756 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009757 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9759 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009763 }
Paul Bakker48916f92012-09-16 19:57:18 +00009764 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009765
Hanno Becker90333da2017-10-10 11:27:13 +01009766 /* At this point, we don't know whether the renegotiation has been
9767 * completed or not. The cases to consider are the following:
9768 * 1) The renegotiation is complete. In this case, no new record
9769 * has been read yet.
9770 * 2) The renegotiation is incomplete because the client received
9771 * an application data record while awaiting the ServerHello.
9772 * 3) The renegotiation is incomplete because the client received
9773 * a non-handshake, non-application data message while awaiting
9774 * the ServerHello.
9775 * In each of these case, looping will be the proper action:
9776 * - For 1), the next iteration will read a new record and check
9777 * if it's application data.
9778 * - For 2), the loop condition isn't satisfied as application data
9779 * is present, hence continue is the same as break
9780 * - For 3), the loop condition is satisfied and read_record
9781 * will re-deliver the message that was held back by the client
9782 * when expecting the ServerHello.
9783 */
9784 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009785 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009786#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009788 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009789 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009790 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009791 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009794 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009796 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009797 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009798 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9802 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009805 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009806 }
9807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9811 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009812 }
9813
9814 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009815
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009816 /* We're going to return something now, cancel timer,
9817 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009819 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009820
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009821#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009822 /* If we requested renego but received AppData, resend HelloRequest.
9823 * Do it now, after setting in_offt, to avoid taking this branch
9824 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009826 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009828 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009829 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009832 return( ret );
9833 }
9834 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009836#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009837 }
9838
9839 n = ( len < ssl->in_msglen )
9840 ? len : ssl->in_msglen;
9841
9842 memcpy( buf, ssl->in_offt, n );
9843 ssl->in_msglen -= n;
9844
9845 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009846 {
9847 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009848 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009849 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009851 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009852 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009853 /* more data available */
9854 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009858
Paul Bakker23986e52011-04-24 08:57:21 +00009859 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009860}
9861
9862/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009863 * Send application data to be encrypted by the SSL layer, taking care of max
9864 * fragment length and buffer size.
9865 *
9866 * According to RFC 5246 Section 6.2.1:
9867 *
9868 * Zero-length fragments of Application data MAY be sent as they are
9869 * potentially useful as a traffic analysis countermeasure.
9870 *
9871 * Therefore, it is possible that the input message length is 0 and the
9872 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009873 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009874static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009875 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009876{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009877 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9878 const size_t max_len = (size_t) ret;
9879
9880 if( ret < 0 )
9881 {
9882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9883 return( ret );
9884 }
9885
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009886 if( len > max_len )
9887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009889 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009892 "maximum fragment length: %d > %d",
9893 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009895 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009896 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009897#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009898#if defined(MBEDTLS_SSL_PROTO_TLS)
9899 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009900 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009901 }
9902#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009903 }
Paul Bakker887bd502011-06-08 13:10:54 +00009904
Paul Bakker5121ce52009-01-03 21:22:43 +00009905 if( ssl->out_left != 0 )
9906 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009907 /*
9908 * The user has previously tried to send the data and
9909 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9910 * written. In this case, we expect the high-level write function
9911 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009916 return( ret );
9917 }
9918 }
Paul Bakker887bd502011-06-08 13:10:54 +00009919 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009920 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009921 /*
9922 * The user is trying to send a message the first time, so we need to
9923 * copy the data into the internal buffers and setup the data structure
9924 * to keep track of partial writes
9925 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009926 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009927 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009928 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009929
Hanno Becker67bc7c32018-08-06 11:33:50 +01009930 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009933 return( ret );
9934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009935 }
9936
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009937 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009938}
9939
9940/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009941 * Write application data, doing 1/n-1 splitting if necessary.
9942 *
9943 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009944 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009945 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009948static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009949 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009950{
9951 int ret;
9952
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009953 if( ssl->conf->cbc_record_splitting ==
9954 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009955 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9957 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9958 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009959 {
9960 return( ssl_write_real( ssl, buf, len ) );
9961 }
9962
9963 if( ssl->split_done == 0 )
9964 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009965 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009966 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009967 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009968 }
9969
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009970 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9971 return( ret );
9972 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009973
9974 return( ret + 1 );
9975}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009977
9978/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009979 * Write application data (public-facing wrapper)
9980 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009981int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009982{
9983 int ret;
9984
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009986
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009987 if( ssl == NULL || ssl->conf == NULL )
9988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9989
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009990#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009991 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9992 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009993 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009994 return( ret );
9995 }
9996#endif
9997
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009998 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009999 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010000 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010001 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010003 return( ret );
10004 }
10005 }
10006
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010007#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010008 ret = ssl_write_split( ssl, buf, len );
10009#else
10010 ret = ssl_write_real( ssl, buf, len );
10011#endif
10012
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010014
10015 return( ret );
10016}
10017
10018/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010019 * Notify the peer that the connection is being closed
10020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010022{
10023 int ret;
10024
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010025 if( ssl == NULL || ssl->conf == NULL )
10026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010029
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010030 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010031 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10036 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10037 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010040 return( ret );
10041 }
10042 }
10043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010045
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010046 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010047}
10048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010050{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010051 if( transform == NULL )
10052 return;
10053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010055 deflateEnd( &transform->ctx_deflate );
10056 inflateEnd( &transform->ctx_inflate );
10057#endif
10058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10060 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010061
Hanno Becker92231322018-01-03 15:32:51 +000010062#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 mbedtls_md_free( &transform->md_ctx_enc );
10064 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010065#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010066
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010067 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010068}
10069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070#if defined(MBEDTLS_X509_CRT_PARSE_C)
10071static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010072{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010074
10075 while( cur != NULL )
10076 {
10077 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010079 cur = next;
10080 }
10081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010083
Hanno Becker0271f962018-08-16 13:23:47 +010010084#if defined(MBEDTLS_SSL_PROTO_DTLS)
10085
10086static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10087{
10088 unsigned offset;
10089 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10090
10091 if( hs == NULL )
10092 return;
10093
Hanno Becker283f5ef2018-08-24 09:34:47 +010010094 ssl_free_buffered_record( ssl );
10095
Hanno Becker0271f962018-08-16 13:23:47 +010010096 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010097 ssl_buffering_free_slot( ssl, offset );
10098}
10099
10100static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10101 uint8_t slot )
10102{
10103 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10104 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010105
10106 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10107 return;
10108
Hanno Beckere605b192018-08-21 15:59:07 +010010109 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010110 {
Hanno Beckere605b192018-08-21 15:59:07 +010010111 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010112 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010113 mbedtls_free( hs_buf->data );
10114 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010115 }
10116}
10117
10118#endif /* MBEDTLS_SSL_PROTO_DTLS */
10119
Gilles Peskine9b562d52018-04-25 20:32:43 +020010120void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010121{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010122 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10123
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010124 if( handshake == NULL )
10125 return;
10126
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010127#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10128 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10129 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010130 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010131 handshake->async_in_progress = 0;
10132 }
10133#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10134
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010135#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10136 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10137 mbedtls_md5_free( &handshake->fin_md5 );
10138 mbedtls_sha1_free( &handshake->fin_sha1 );
10139#endif
10140#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10141#if defined(MBEDTLS_SHA256_C)
10142 mbedtls_sha256_free( &handshake->fin_sha256 );
10143#endif
10144#if defined(MBEDTLS_SHA512_C)
10145 mbedtls_sha512_free( &handshake->fin_sha512 );
10146#endif
10147#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149#if defined(MBEDTLS_DHM_C)
10150 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010152#if defined(MBEDTLS_ECDH_C)
10153 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010154#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010155#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010156 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010157#if defined(MBEDTLS_SSL_CLI_C)
10158 mbedtls_free( handshake->ecjpake_cache );
10159 handshake->ecjpake_cache = NULL;
10160 handshake->ecjpake_cache_len = 0;
10161#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010162#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010163
Janos Follath4ae5c292016-02-10 11:27:43 +000010164#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10165 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010166 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010167 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010168#endif
10169
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010170#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10171 if( handshake->psk != NULL )
10172 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010173 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010174 mbedtls_free( handshake->psk );
10175 }
10176#endif
10177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10179 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010180 /*
10181 * Free only the linked list wrapper, not the keys themselves
10182 * since the belong to the SNI callback
10183 */
10184 if( handshake->sni_key_cert != NULL )
10185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010187
10188 while( cur != NULL )
10189 {
10190 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010191 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010192 cur = next;
10193 }
10194 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010196
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010197#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010198 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010199#endif
10200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201#if defined(MBEDTLS_SSL_PROTO_DTLS)
10202 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010203 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010204 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010205#endif
10206
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010207 mbedtls_platform_zeroize( handshake,
10208 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010209}
10210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010212{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010213 if( session == NULL )
10214 return;
10215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010217 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010218#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010219
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010220#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010222#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010223
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010224 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010225}
10226
Paul Bakker5121ce52009-01-03 21:22:43 +000010227/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010228 * Serialize a full SSL context
10229 */
10230int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10231 unsigned char *buf,
10232 size_t buf_len,
10233 size_t *olen )
10234{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010235 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010236 (void) ssl;
10237
10238 if( buf != NULL )
10239 memset( buf, 0, buf_len );
10240
10241 *olen = 0;
10242
10243 return( 0 );
10244}
10245
10246/*
10247 * Deserialize a full SSL context
10248 */
10249int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10250 const unsigned char *buf,
10251 size_t len )
10252{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010253 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010254 (void) ssl;
10255 (void) buf;
10256 (void) len;
10257
10258 return( 0 );
10259}
10260
10261/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010262 * Free an SSL context
10263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010265{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010266 if( ssl == NULL )
10267 return;
10268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010270
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010271 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010272 {
Angus Grattond8213d02016-05-25 20:56:48 +100010273 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010274 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010275 }
10276
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010277 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010278 {
Angus Grattond8213d02016-05-25 20:56:48 +100010279 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010281 }
10282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010283#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010284 if( ssl->compress_buf != NULL )
10285 {
Angus Grattond8213d02016-05-25 20:56:48 +100010286 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010288 }
10289#endif
10290
Paul Bakker48916f92012-09-16 19:57:18 +000010291 if( ssl->transform )
10292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293 mbedtls_ssl_transform_free( ssl->transform );
10294 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010295 }
10296
10297 if( ssl->handshake )
10298 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010299 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010300 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10301 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303 mbedtls_free( ssl->handshake );
10304 mbedtls_free( ssl->transform_negotiate );
10305 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010306 }
10307
Paul Bakkerc0463502013-02-14 11:19:38 +010010308 if( ssl->session )
10309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310 mbedtls_ssl_session_free( ssl->session );
10311 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010312 }
10313
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010314#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010315 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010316 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010317 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010319 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010320#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10323 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10326 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010327 }
10328#endif
10329
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010330#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010332#endif
10333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010335
Paul Bakker86f04f42013-02-14 11:20:09 +010010336 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010337 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010338}
10339
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010340/*
10341 * Initialze mbedtls_ssl_config
10342 */
10343void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10344{
10345 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010346
10347#if !defined(MBEDTLS_SSL_PROTO_TLS)
10348 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10349#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010350}
10351
Simon Butcherc97b6972015-12-27 23:48:17 +000010352#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010353static int ssl_preset_default_hashes[] = {
10354#if defined(MBEDTLS_SHA512_C)
10355 MBEDTLS_MD_SHA512,
10356 MBEDTLS_MD_SHA384,
10357#endif
10358#if defined(MBEDTLS_SHA256_C)
10359 MBEDTLS_MD_SHA256,
10360 MBEDTLS_MD_SHA224,
10361#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010362#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010363 MBEDTLS_MD_SHA1,
10364#endif
10365 MBEDTLS_MD_NONE
10366};
Simon Butcherc97b6972015-12-27 23:48:17 +000010367#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010368
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010369static int ssl_preset_suiteb_ciphersuites[] = {
10370 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10371 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10372 0
10373};
10374
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010375#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010376static int ssl_preset_suiteb_hashes[] = {
10377 MBEDTLS_MD_SHA256,
10378 MBEDTLS_MD_SHA384,
10379 MBEDTLS_MD_NONE
10380};
10381#endif
10382
10383#if defined(MBEDTLS_ECP_C)
10384static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10385 MBEDTLS_ECP_DP_SECP256R1,
10386 MBEDTLS_ECP_DP_SECP384R1,
10387 MBEDTLS_ECP_DP_NONE
10388};
10389#endif
10390
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010391/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010392 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010393 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010394int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010395 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010396{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010397#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010398 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010399#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010400
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010401 /* Use the functions here so that they are covered in tests,
10402 * but otherwise access member directly for efficiency */
10403 mbedtls_ssl_conf_endpoint( conf, endpoint );
10404 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010405
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010406 /*
10407 * Things that are common to all presets
10408 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010409#if defined(MBEDTLS_SSL_CLI_C)
10410 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10411 {
10412 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10413#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10414 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10415#endif
10416 }
10417#endif
10418
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010419#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010420 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010421#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010422
10423#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10424 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10425#endif
10426
10427#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10428 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010429 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010430 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010431#endif
10432
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010433#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10434 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10435#endif
10436
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010437#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010438 conf->f_cookie_write = ssl_cookie_write_dummy;
10439 conf->f_cookie_check = ssl_cookie_check_dummy;
10440#endif
10441
10442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10443 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10444#endif
10445
Janos Follath088ce432017-04-10 12:42:31 +010010446#if defined(MBEDTLS_SSL_SRV_C)
10447 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10448#endif
10449
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010450#if defined(MBEDTLS_SSL_PROTO_DTLS)
10451 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10452 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10453#endif
10454
10455#if defined(MBEDTLS_SSL_RENEGOTIATION)
10456 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010457 memset( conf->renego_period, 0x00, 2 );
10458 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010459#endif
10460
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010461#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10462 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10463 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010464 const unsigned char dhm_p[] =
10465 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10466 const unsigned char dhm_g[] =
10467 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10468
Hanno Beckera90658f2017-10-04 15:29:08 +010010469 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10470 dhm_p, sizeof( dhm_p ),
10471 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010472 {
10473 return( ret );
10474 }
10475 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010476#endif
10477
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010478 /*
10479 * Preset-specific defaults
10480 */
10481 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010482 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010483 /*
10484 * NSA Suite B
10485 */
10486 case MBEDTLS_SSL_PRESET_SUITEB:
10487 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10488 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10489 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10490 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10491
10492 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10493 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10494 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10495 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10496 ssl_preset_suiteb_ciphersuites;
10497
10498#if defined(MBEDTLS_X509_CRT_PARSE_C)
10499 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010500#endif
10501
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010502#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010503 conf->sig_hashes = ssl_preset_suiteb_hashes;
10504#endif
10505
10506#if defined(MBEDTLS_ECP_C)
10507 conf->curve_list = ssl_preset_suiteb_curves;
10508#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010509 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010510
10511 /*
10512 * Default
10513 */
10514 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010515 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10516 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10517 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10518 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10519 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10520 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10521 MBEDTLS_SSL_MIN_MINOR_VERSION :
10522 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010523 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10524 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10525
10526#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010527 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010528 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10529#endif
10530
10531 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10532 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10533 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10534 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10535 mbedtls_ssl_list_ciphersuites();
10536
10537#if defined(MBEDTLS_X509_CRT_PARSE_C)
10538 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10539#endif
10540
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010541#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010542 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010543#endif
10544
10545#if defined(MBEDTLS_ECP_C)
10546 conf->curve_list = mbedtls_ecp_grp_id_list();
10547#endif
10548
10549#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10550 conf->dhm_min_bitlen = 1024;
10551#endif
10552 }
10553
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010554 return( 0 );
10555}
10556
10557/*
10558 * Free mbedtls_ssl_config
10559 */
10560void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10561{
10562#if defined(MBEDTLS_DHM_C)
10563 mbedtls_mpi_free( &conf->dhm_P );
10564 mbedtls_mpi_free( &conf->dhm_G );
10565#endif
10566
10567#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10568 if( conf->psk != NULL )
10569 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010570 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010571 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010572 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010573 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010574 }
10575
10576 if( conf->psk_identity != NULL )
10577 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010578 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010579 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010580 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010581 conf->psk_identity_len = 0;
10582 }
10583#endif
10584
10585#if defined(MBEDTLS_X509_CRT_PARSE_C)
10586 ssl_key_cert_free( conf->key_cert );
10587#endif
10588
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010589 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010590}
10591
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010592#if defined(MBEDTLS_PK_C) && \
10593 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010594/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599#if defined(MBEDTLS_RSA_C)
10600 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10601 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010602#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603#if defined(MBEDTLS_ECDSA_C)
10604 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10605 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010608}
10609
Hanno Becker7e5437a2017-04-28 17:15:26 +010010610unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10611{
10612 switch( type ) {
10613 case MBEDTLS_PK_RSA:
10614 return( MBEDTLS_SSL_SIG_RSA );
10615 case MBEDTLS_PK_ECDSA:
10616 case MBEDTLS_PK_ECKEY:
10617 return( MBEDTLS_SSL_SIG_ECDSA );
10618 default:
10619 return( MBEDTLS_SSL_SIG_ANON );
10620 }
10621}
10622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010624{
10625 switch( sig )
10626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627#if defined(MBEDTLS_RSA_C)
10628 case MBEDTLS_SSL_SIG_RSA:
10629 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010630#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631#if defined(MBEDTLS_ECDSA_C)
10632 case MBEDTLS_SSL_SIG_ECDSA:
10633 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010634#endif
10635 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010637 }
10638}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010639#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010640
Hanno Becker7e5437a2017-04-28 17:15:26 +010010641#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10642 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10643
10644/* Find an entry in a signature-hash set matching a given hash algorithm. */
10645mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10646 mbedtls_pk_type_t sig_alg )
10647{
10648 switch( sig_alg )
10649 {
10650 case MBEDTLS_PK_RSA:
10651 return( set->rsa );
10652 case MBEDTLS_PK_ECDSA:
10653 return( set->ecdsa );
10654 default:
10655 return( MBEDTLS_MD_NONE );
10656 }
10657}
10658
10659/* Add a signature-hash-pair to a signature-hash set */
10660void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10661 mbedtls_pk_type_t sig_alg,
10662 mbedtls_md_type_t md_alg )
10663{
10664 switch( sig_alg )
10665 {
10666 case MBEDTLS_PK_RSA:
10667 if( set->rsa == MBEDTLS_MD_NONE )
10668 set->rsa = md_alg;
10669 break;
10670
10671 case MBEDTLS_PK_ECDSA:
10672 if( set->ecdsa == MBEDTLS_MD_NONE )
10673 set->ecdsa = md_alg;
10674 break;
10675
10676 default:
10677 break;
10678 }
10679}
10680
10681/* Allow exactly one hash algorithm for each signature. */
10682void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10683 mbedtls_md_type_t md_alg )
10684{
10685 set->rsa = md_alg;
10686 set->ecdsa = md_alg;
10687}
10688
10689#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10690 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10691
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010692/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010693 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010695mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010696{
10697 switch( hash )
10698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699#if defined(MBEDTLS_MD5_C)
10700 case MBEDTLS_SSL_HASH_MD5:
10701 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703#if defined(MBEDTLS_SHA1_C)
10704 case MBEDTLS_SSL_HASH_SHA1:
10705 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010706#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707#if defined(MBEDTLS_SHA256_C)
10708 case MBEDTLS_SSL_HASH_SHA224:
10709 return( MBEDTLS_MD_SHA224 );
10710 case MBEDTLS_SSL_HASH_SHA256:
10711 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713#if defined(MBEDTLS_SHA512_C)
10714 case MBEDTLS_SSL_HASH_SHA384:
10715 return( MBEDTLS_MD_SHA384 );
10716 case MBEDTLS_SSL_HASH_SHA512:
10717 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010718#endif
10719 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010721 }
10722}
10723
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010724/*
10725 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10726 */
10727unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10728{
10729 switch( md )
10730 {
10731#if defined(MBEDTLS_MD5_C)
10732 case MBEDTLS_MD_MD5:
10733 return( MBEDTLS_SSL_HASH_MD5 );
10734#endif
10735#if defined(MBEDTLS_SHA1_C)
10736 case MBEDTLS_MD_SHA1:
10737 return( MBEDTLS_SSL_HASH_SHA1 );
10738#endif
10739#if defined(MBEDTLS_SHA256_C)
10740 case MBEDTLS_MD_SHA224:
10741 return( MBEDTLS_SSL_HASH_SHA224 );
10742 case MBEDTLS_MD_SHA256:
10743 return( MBEDTLS_SSL_HASH_SHA256 );
10744#endif
10745#if defined(MBEDTLS_SHA512_C)
10746 case MBEDTLS_MD_SHA384:
10747 return( MBEDTLS_SSL_HASH_SHA384 );
10748 case MBEDTLS_MD_SHA512:
10749 return( MBEDTLS_SSL_HASH_SHA512 );
10750#endif
10751 default:
10752 return( MBEDTLS_SSL_HASH_NONE );
10753 }
10754}
10755
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010756#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010757/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010758 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010759 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010760 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010761int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010764
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010765 if( ssl->conf->curve_list == NULL )
10766 return( -1 );
10767
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010768 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010769 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010770 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010771
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010772 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010773}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010774#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010775
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010776#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010777/*
10778 * Check if a hash proposed by the peer is in our list.
10779 * Return 0 if we're willing to use it, -1 otherwise.
10780 */
10781int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10782 mbedtls_md_type_t md )
10783{
10784 const int *cur;
10785
10786 if( ssl->conf->sig_hashes == NULL )
10787 return( -1 );
10788
10789 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10790 if( *cur == (int) md )
10791 return( 0 );
10792
10793 return( -1 );
10794}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010795#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010797#if defined(MBEDTLS_X509_CRT_PARSE_C)
10798int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10799 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010800 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010801 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010802{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010803 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010804#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010805 int usage = 0;
10806#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010807#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010808 const char *ext_oid;
10809 size_t ext_len;
10810#endif
10811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010812#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10813 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010814 ((void) cert);
10815 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010816 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010817#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010819#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10820 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010821 {
10822 /* Server part of the key exchange */
10823 switch( ciphersuite->key_exchange )
10824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010825 case MBEDTLS_KEY_EXCHANGE_RSA:
10826 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010827 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010828 break;
10829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010830 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10831 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10832 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10833 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010834 break;
10835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10837 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010838 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010839 break;
10840
10841 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010842 case MBEDTLS_KEY_EXCHANGE_NONE:
10843 case MBEDTLS_KEY_EXCHANGE_PSK:
10844 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10845 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010846 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010847 usage = 0;
10848 }
10849 }
10850 else
10851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10853 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010854 }
10855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010857 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010858 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010859 ret = -1;
10860 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010861#else
10862 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10866 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10869 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010870 }
10871 else
10872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010873 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10874 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010875 }
10876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010877 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010878 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010879 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010880 ret = -1;
10881 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010882#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010883
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010884 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010886#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010887
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010888/*
10889 * Convert version numbers to/from wire format
10890 * and, for DTLS, to/from TLS equivalent.
10891 *
10892 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010893 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010894 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10895 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010897void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010898 unsigned char ver[2] )
10899{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010900#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10901 ((void) transport);
10902#endif
10903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010904#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010905 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010908 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10909
10910 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10911 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10912 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010913 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010914#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010915#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010916 {
10917 ver[0] = (unsigned char) major;
10918 ver[1] = (unsigned char) minor;
10919 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010920#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010921}
10922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010923void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010924 const unsigned char ver[2] )
10925{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010926#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10927 ((void) transport);
10928#endif
10929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010931 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010932 {
10933 *major = 255 - ver[0] + 2;
10934 *minor = 255 - ver[1] + 1;
10935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010937 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10938 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010939 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020010940#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010941#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010942 {
10943 *major = ver[0];
10944 *minor = ver[1];
10945 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020010946#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010947}
10948
Simon Butcher99000142016-10-13 17:21:01 +010010949int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10950{
10951#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10952 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10953 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10954
10955 switch( md )
10956 {
10957#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10958#if defined(MBEDTLS_MD5_C)
10959 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010960 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010961#endif
10962#if defined(MBEDTLS_SHA1_C)
10963 case MBEDTLS_SSL_HASH_SHA1:
10964 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10965 break;
10966#endif
10967#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10968#if defined(MBEDTLS_SHA512_C)
10969 case MBEDTLS_SSL_HASH_SHA384:
10970 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10971 break;
10972#endif
10973#if defined(MBEDTLS_SHA256_C)
10974 case MBEDTLS_SSL_HASH_SHA256:
10975 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10976 break;
10977#endif
10978 default:
10979 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10980 }
10981
10982 return 0;
10983#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10984 (void) ssl;
10985 (void) md;
10986
10987 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10988#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10989}
10990
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010991#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10992 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10993int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10994 unsigned char *output,
10995 unsigned char *data, size_t data_len )
10996{
10997 int ret = 0;
10998 mbedtls_md5_context mbedtls_md5;
10999 mbedtls_sha1_context mbedtls_sha1;
11000
11001 mbedtls_md5_init( &mbedtls_md5 );
11002 mbedtls_sha1_init( &mbedtls_sha1 );
11003
11004 /*
11005 * digitally-signed struct {
11006 * opaque md5_hash[16];
11007 * opaque sha_hash[20];
11008 * };
11009 *
11010 * md5_hash
11011 * MD5(ClientHello.random + ServerHello.random
11012 * + ServerParams);
11013 * sha_hash
11014 * SHA(ClientHello.random + ServerHello.random
11015 * + ServerParams);
11016 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011017 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011018 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011020 goto exit;
11021 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011022 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011023 ssl->handshake->randbytes, 64 ) ) != 0 )
11024 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011026 goto exit;
11027 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011028 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011029 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011031 goto exit;
11032 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011033 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011034 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011036 goto exit;
11037 }
11038
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011039 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011040 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011042 goto exit;
11043 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011044 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011045 ssl->handshake->randbytes, 64 ) ) != 0 )
11046 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011048 goto exit;
11049 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011050 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011051 data_len ) ) != 0 )
11052 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011054 goto exit;
11055 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011056 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011057 output + 16 ) ) != 0 )
11058 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011060 goto exit;
11061 }
11062
11063exit:
11064 mbedtls_md5_free( &mbedtls_md5 );
11065 mbedtls_sha1_free( &mbedtls_sha1 );
11066
11067 if( ret != 0 )
11068 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11069 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11070
11071 return( ret );
11072
11073}
11074#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11075 MBEDTLS_SSL_PROTO_TLS1_1 */
11076
11077#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11078 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11079int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011080 unsigned char *hash, size_t *hashlen,
11081 unsigned char *data, size_t data_len,
11082 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011083{
11084 int ret = 0;
11085 mbedtls_md_context_t ctx;
11086 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011087 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011088
11089 mbedtls_md_init( &ctx );
11090
11091 /*
11092 * digitally-signed struct {
11093 * opaque client_random[32];
11094 * opaque server_random[32];
11095 * ServerDHParams params;
11096 * };
11097 */
11098 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11099 {
11100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11101 goto exit;
11102 }
11103 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11104 {
11105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11106 goto exit;
11107 }
11108 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11109 {
11110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11111 goto exit;
11112 }
11113 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11114 {
11115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11116 goto exit;
11117 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011118 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011119 {
11120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11121 goto exit;
11122 }
11123
11124exit:
11125 mbedtls_md_free( &ctx );
11126
11127 if( ret != 0 )
11128 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11129 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11130
11131 return( ret );
11132}
11133#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11134 MBEDTLS_SSL_PROTO_TLS1_2 */
11135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011136#endif /* MBEDTLS_SSL_TLS_C */