blob: c682166f8caaafe08da1824845c571748fe4bc5b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Hanno Becker58fccf22019-02-06 14:30:46 +0000371int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
372 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
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 }
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000396
397#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
398 if( src->peer_cert_digest != NULL )
399 {
400 dst->peer_cert_digest_len = src->peer_cert_digest_len;
401 dst->peer_cert_digest =
402 mbedtls_calloc( 1, dst->peer_cert_digest_len );
403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
409 }
410#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200413
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200414#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200415 if( src->ticket != NULL )
416 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200417 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200418 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200420
421 memcpy( dst->ticket, src->ticket, src->ticket_len );
422 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200423#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200424
425 return( 0 );
426}
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
429int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200430 const unsigned char *key_enc, const unsigned char *key_dec,
431 size_t keylen,
432 const unsigned char *iv_enc, const unsigned char *iv_dec,
433 size_t ivlen,
434 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200435 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
437int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
438int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
441#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000442
Paul Bakker5121ce52009-01-03 21:22:43 +0000443/*
444 * Key material generation
445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200447static int ssl3_prf( const unsigned char *secret, size_t slen,
448 const char *label,
449 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000450 unsigned char *dstbuf, size_t dlen )
451{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100452 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000453 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200454 mbedtls_md5_context md5;
455 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000456 unsigned char padding[16];
457 unsigned char sha1sum[20];
458 ((void)label);
459
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200460 mbedtls_md5_init( &md5 );
461 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200462
Paul Bakker5f70b252012-09-13 14:23:06 +0000463 /*
464 * SSLv3:
465 * block =
466 * MD5( secret + SHA1( 'A' + secret + random ) ) +
467 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
468 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
469 * ...
470 */
471 for( i = 0; i < dlen / 16; i++ )
472 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200473 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000474
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100481 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100483 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100484 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000485
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100486 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100487 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100488 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100489 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100490 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100491 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100492 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100493 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000494 }
495
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200497 mbedtls_md5_free( &md5 );
498 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000499
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500500 mbedtls_platform_zeroize( padding, sizeof( padding ) );
501 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000502
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200508static int tls1_prf( const unsigned char *secret, size_t slen,
509 const char *label,
510 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000511 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000512{
Paul Bakker23986e52011-04-24 08:57:21 +0000513 size_t nb, hs;
514 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200515 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 unsigned char tmp[128];
517 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 const mbedtls_md_info_t *md_info;
519 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100520 int ret;
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 hs = ( slen + 1 ) / 2;
528 S1 = secret;
529 S2 = secret + slen - hs;
530
531 nb = strlen( label );
532 memcpy( tmp + 20, label, nb );
533 memcpy( tmp + 20 + nb, random, rlen );
534 nb += rlen;
535
536 /*
537 * First compute P_md5(secret,label+random)[0..dlen]
538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100543 return( ret );
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
546 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
547 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 for( i = 0; i < dlen; i += 16 )
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_md_hmac_reset ( &md_ctx );
552 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
553 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_reset ( &md_ctx );
556 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
557 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
560
561 for( j = 0; j < k; j++ )
562 dstbuf[i + j] = h_i[j];
563 }
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100566
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 /*
568 * XOR out with P_sha1(secret,label+random)[0..dlen]
569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100574 return( ret );
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
577 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
578 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 for( i = 0; i < dlen; i += 20 )
581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_hmac_reset ( &md_ctx );
583 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
584 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_md_hmac_reset ( &md_ctx );
587 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
588 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
591
592 for( j = 0; j < k; j++ )
593 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
594 }
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100597
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500598 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
599 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 return( 0 );
602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
606static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607 const unsigned char *secret, size_t slen,
608 const char *label,
609 const unsigned char *random, size_t rlen,
610 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611{
612 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
616 const mbedtls_md_info_t *md_info;
617 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100618 int ret;
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100626
627 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000629
630 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100631 memcpy( tmp + md_len, label, nb );
632 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000633 nb += rlen;
634
635 /*
636 * Compute P_<hash>(secret, label + random)[0..dlen]
637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100639 return( ret );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
642 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
643 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100644
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100645 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_md_hmac_reset ( &md_ctx );
648 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
649 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_md_hmac_reset ( &md_ctx );
652 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
653 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000654
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100655 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000656
657 for( j = 0; j < k; j++ )
658 dstbuf[i + j] = h_i[j];
659 }
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100662
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500663 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
664 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000665
666 return( 0 );
667}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100670static int tls_prf_sha256( const unsigned char *secret, size_t slen,
671 const char *label,
672 const unsigned char *random, size_t rlen,
673 unsigned char *dstbuf, size_t dlen )
674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100676 label, random, rlen, dstbuf, dlen ) );
677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200681static int tls_prf_sha384( const unsigned char *secret, size_t slen,
682 const char *label,
683 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000684 unsigned char *dstbuf, size_t dlen )
685{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100687 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_SHA512_C */
690#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
695 defined(MBEDTLS_SSL_PROTO_TLS1_1)
696static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200697#endif
Paul Bakker380da532012-04-18 16:10:25 +0000698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200700static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702#endif
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200705static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200707#endif
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
710#if defined(MBEDTLS_SHA256_C)
711static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200712static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200714#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_SHA512_C)
717static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200718static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000722
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200723/* Type for the TLS PRF */
724typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
725 const unsigned char *, size_t,
726 unsigned char *, size_t);
727
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200728/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200729 * Populate a transform structure with session keys and all the other
730 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200731 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200732 * Parameters:
733 * - [in/out]: transform: structure to populate
734 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200735 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200736 * - [in] ciphersuite
737 * - [in] master
738 * - [in] encrypt_then_mac
739 * - [in] trunc_hmac
740 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200741 * - [in] tls_prf: pointer to PRF to use for key derivation
742 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200743 * - [in] minor_ver: SSL/TLS minor version
744 * - [in] endpoint: client or server
745 * - [in] ssl: optionally used for:
746 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
747 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
748 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200749 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200750static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200751 int ciphersuite,
752 const unsigned char master[48],
753#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
754 int encrypt_then_mac,
755#endif
756#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
757 int trunc_hmac,
758#endif
759#if defined(MBEDTLS_ZLIB_SUPPORT)
760 int compression,
761#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200762 ssl_tls_prf_t tls_prf,
763 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200764 int minor_ver,
765 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200766 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000767{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200768 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 unsigned char keyblk[256];
770 unsigned char *key1;
771 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100772 unsigned char *mac_enc;
773 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000774 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200775 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000776 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000777 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 const mbedtls_cipher_info_t *cipher_info;
779 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100780
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200781#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
782 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
783 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200784 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200785 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000786#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000787
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200788 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200790 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000791#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200792 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200794 /*
795 * Get various info structures
796 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200797 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 if( ciphersuite_info == NULL )
799 {
800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
803 }
804
Hanno Becker8759e162017-12-27 21:34:08 +0000805 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100806 if( cipher_info == NULL )
807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000809 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100811 }
812
Hanno Becker8759e162017-12-27 21:34:08 +0000813 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100814 if( md_info == NULL )
815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000817 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100819 }
820
Hanno Beckera5a2b082019-05-15 14:03:01 +0100821#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100822 /* Copy own and peer's CID if the use of the CID
823 * extension has been negotiated. */
824 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
825 {
826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100827
Hanno Becker4932f9f2019-05-03 15:23:51 +0100828 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100829 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100830 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100831 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100832
833 transform->out_cid_len = ssl->handshake->peer_cid_len;
834 memcpy( transform->out_cid, ssl->handshake->peer_cid,
835 ssl->handshake->peer_cid_len );
836 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
837 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100838 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100839#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200842 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000843 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200844 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100845 if( ret != 0 )
846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100848 return( ret );
849 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200852 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200853 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200854 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 /*
858 * Determine the appropriate key, IV and MAC length.
859 */
Paul Bakker68884e32013-01-07 18:20:04 +0100860
Hanno Beckere7f2df02017-12-27 08:17:40 +0000861 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200862
Hanno Beckerf1229442018-01-03 15:32:31 +0000863#if defined(MBEDTLS_GCM_C) || \
864 defined(MBEDTLS_CCM_C) || \
865 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 cipher_info->mode == MBEDTLS_MODE_CCM ||
868 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 {
Hanno Becker8759e162017-12-27 21:34:08 +0000870 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200872 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000873 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000874 transform->taglen =
875 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200877 /* All modes haves 96-bit IVs;
878 * GCM and CCM has 4 implicit and 8 explicit bytes
879 * ChachaPoly has all 12 bytes implicit
880 */
Paul Bakker68884e32013-01-07 18:20:04 +0100881 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200882 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
883 transform->fixed_ivlen = 12;
884 else
885 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200886
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200887 /* Minimum length of encrypted record */
888 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000889 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100890 }
891 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000892#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
893#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
894 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
895 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100896 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200897 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
899 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200902 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100903 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200905 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000906 mac_key_len = mbedtls_md_get_size( md_info );
907 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200910 /*
911 * If HMAC is to be truncated, we shall keep the leftmost bytes,
912 * (rfc 6066 page 13 or rfc 2104 section 4),
913 * so we only need to adjust the length here.
914 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200915 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000918
919#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
920 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000921 * HMAC implementation which also truncates the key
922 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000923 mac_key_len = transform->maclen;
924#endif
925 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200927
928 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100929 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200931 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200933 transform->minlen = transform->maclen;
934 else
Paul Bakker68884e32013-01-07 18:20:04 +0100935 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200936 /*
937 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100938 * 1. if EtM is in use: one block plus MAC
939 * otherwise: * first multiple of blocklen greater than maclen
940 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200943 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100944 {
945 transform->minlen = transform->maclen
946 + cipher_info->block_size;
947 }
948 else
949#endif
950 {
951 transform->minlen = transform->maclen
952 + cipher_info->block_size
953 - transform->maclen % cipher_info->block_size;
954 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200957 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
958 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100960 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200963 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
964 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965 {
966 transform->minlen += transform->ivlen;
967 }
968 else
969#endif
970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200973 }
Paul Bakker68884e32013-01-07 18:20:04 +0100974 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000976 else
977#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
978 {
979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
981 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Hanno Beckere7f2df02017-12-27 08:17:40 +0000983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
984 (unsigned) keylen,
985 (unsigned) transform->minlen,
986 (unsigned) transform->ivlen,
987 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989 /*
990 * Finally setup the cipher contexts, IVs and MAC secrets.
991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200993 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000995 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000996 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Paul Bakker68884e32013-01-07 18:20:04 +0100998 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001001 /*
1002 * This is not used in TLS v1.1.
1003 */
Paul Bakker48916f92012-09-16 19:57:18 +00001004 iv_copy_len = ( transform->fixed_ivlen ) ?
1005 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001006 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1007 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001008 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 }
1010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_CLI_C */
1012#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001013 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001015 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001016 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Hanno Becker81c7b182017-11-09 18:39:33 +00001018 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001019 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001021 /*
1022 * This is not used in TLS v1.1.
1023 */
Paul Bakker48916f92012-09-16 19:57:18 +00001024 iv_copy_len = ( transform->fixed_ivlen ) ?
1025 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001026 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1027 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001028 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Hanno Becker92231322018-01-03 15:32:51 +00001037#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001039 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001040 {
Hanno Becker92231322018-01-03 15:32:51 +00001041 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001045 }
1046
Hanno Becker81c7b182017-11-09 18:39:33 +00001047 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1048 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001049 }
1050 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1052#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1053 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001054 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001055 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001056 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1057 For AEAD-based ciphersuites, there is nothing to do here. */
1058 if( mac_key_len != 0 )
1059 {
1060 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1061 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1062 }
Paul Bakker68884e32013-01-07 18:20:04 +01001063 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001064 else
1065#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001069 }
Hanno Becker92231322018-01-03 15:32:51 +00001070#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1073 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001074 {
1075 int ret = 0;
1076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001078
Hanno Beckere7f2df02017-12-27 08:17:40 +00001079 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001080 transform->iv_enc, transform->iv_dec,
1081 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001082 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001083 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1086 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001087 }
1088 }
Hanno Becker92231322018-01-03 15:32:51 +00001089#else
1090 ((void) mac_dec);
1091 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001093
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001094#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1095 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001096 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001097 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001098 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001099 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 iv_copy_len );
1101 }
1102#endif
1103
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001104 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001105 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001106 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001108 return( ret );
1109 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001110
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 cipher_info ) ) != 0 )
1113 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001115 return( ret );
1116 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001119 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001123 return( ret );
1124 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001127 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131 return( ret );
1132 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_CIPHER_MODE_CBC)
1135 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1138 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001141 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001142 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1145 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001148 return( ret );
1149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001153 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001155 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001157 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001160
Paul Bakker48916f92012-09-16 19:57:18 +00001161 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1162 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001163
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001164 if( deflateInit( &transform->ctx_deflate,
1165 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001166 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1169 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001170 }
1171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 return( 0 );
1175}
1176
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001177/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001178 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001179 *
1180 * Inputs:
1181 * - SSL/TLS minor version
1182 * - hash associated with the ciphersuite (only used by TLS 1.2)
1183 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001184 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001185 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1186 */
1187static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1188 int minor_ver,
1189 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001190{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001191#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1192 (void) hash;
1193#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001195#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001196 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001197 {
1198 handshake->tls_prf = ssl3_prf;
1199 handshake->calc_verify = ssl_calc_verify_ssl;
1200 handshake->calc_finished = ssl_calc_finished_ssl;
1201 }
1202 else
1203#endif
1204#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001205 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001206 {
1207 handshake->tls_prf = tls1_prf;
1208 handshake->calc_verify = ssl_calc_verify_tls;
1209 handshake->calc_finished = ssl_calc_finished_tls;
1210 }
1211 else
1212#endif
1213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1214#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001215 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1216 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001217 {
1218 handshake->tls_prf = tls_prf_sha384;
1219 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1220 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1221 }
1222 else
1223#endif
1224#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001225 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001226 {
1227 handshake->tls_prf = tls_prf_sha256;
1228 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1229 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1230 }
1231 else
1232#endif
1233#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1234 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1236 }
1237
1238 return( 0 );
1239}
1240
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001241/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001242 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001243 *
1244 * Parameters:
1245 * [in/out] handshake
1246 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1247 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001248 * [out] master
1249 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001250 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001251static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001253 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254{
1255 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256
1257#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001258 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001259 (void) ssl;
1260#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001261
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001262 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001263 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1265 return( 0 );
1266 }
1267
1268 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1269 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001270
1271#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001272 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001273 {
1274 unsigned char session_hash[48];
1275 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001276
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001277 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001278
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001279 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1280 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001281
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001282 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001283 "extended master secret",
1284 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001285 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001286 }
1287 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001288#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001289 {
1290 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1291 "master secret",
1292 handshake->randbytes, 64,
1293 master, 48 );
1294 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001295 if( ret != 0 )
1296 {
1297 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1298 return( ret );
1299 }
1300
1301 mbedtls_platform_zeroize( handshake->premaster,
1302 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001303
1304 return( 0 );
1305}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001306
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001307int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1308{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001309 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001310 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1311 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001312
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1314
1315 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001316 ret = ssl_set_handshake_prfs( ssl->handshake,
1317 ssl->minor_ver,
1318 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001319 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001320 {
1321 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001322 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001323 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001324
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001325 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001326 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001327 ssl->session_negotiate->master,
1328 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001329 if( ret != 0 )
1330 {
1331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1332 return( ret );
1333 }
1334
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001335 /* Swap the client and server random values:
1336 * - MS derivation wanted client+server (RFC 5246 8.1)
1337 * - key derivation wants server+client (RFC 5246 6.3) */
1338 {
1339 unsigned char tmp[64];
1340 memcpy( tmp, ssl->handshake->randbytes, 64 );
1341 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1342 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1343 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1344 }
1345
1346 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001347 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001348 ssl->session_negotiate->ciphersuite,
1349 ssl->session_negotiate->master,
1350#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1351 ssl->session_negotiate->encrypt_then_mac,
1352#endif
1353#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1354 ssl->session_negotiate->trunc_hmac,
1355#endif
1356#if defined(MBEDTLS_ZLIB_SUPPORT)
1357 ssl->session_negotiate->compression,
1358#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001359 ssl->handshake->tls_prf,
1360 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001361 ssl->minor_ver,
1362 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001363 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001364 if( ret != 0 )
1365 {
1366 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1367 return( ret );
1368 }
1369
1370 /* We no longer need Server/ClientHello.random values */
1371 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1372 sizeof( ssl->handshake->randbytes ) );
1373
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001374 /* Allocate compression buffer */
1375#if defined(MBEDTLS_ZLIB_SUPPORT)
1376 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1377 ssl->compress_buf == NULL )
1378 {
1379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1380 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1381 if( ssl->compress_buf == NULL )
1382 {
1383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001384 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001385 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1386 }
1387 }
1388#endif
1389
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1391
1392 return( 0 );
1393}
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001396void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1397 unsigned char hash[36],
1398 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001399{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001400 mbedtls_md5_context md5;
1401 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 unsigned char pad_1[48];
1403 unsigned char pad_2[48];
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001407 mbedtls_md5_init( &md5 );
1408 mbedtls_sha1_init( &sha1 );
1409
1410 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1411 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Paul Bakker380da532012-04-18 16:10:25 +00001413 memset( pad_1, 0x36, 48 );
1414 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001416 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1417 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1418 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001420 mbedtls_md5_starts_ret( &md5 );
1421 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1422 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1423 mbedtls_md5_update_ret( &md5, hash, 16 );
1424 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001426 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1427 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1428 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001429
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001430 mbedtls_sha1_starts_ret( &sha1 );
1431 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1432 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1433 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1434 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001436 *hlen = 36;
1437
1438 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001441 mbedtls_md5_free( &md5 );
1442 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001443
Paul Bakker380da532012-04-18 16:10:25 +00001444 return;
1445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001449void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1450 unsigned char hash[36],
1451 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001452{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001453 mbedtls_md5_context md5;
1454 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001457
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001458 mbedtls_md5_init( &md5 );
1459 mbedtls_sha1_init( &sha1 );
1460
1461 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1462 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001463
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001464 mbedtls_md5_finish_ret( &md5, hash );
1465 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001467 *hlen = 36;
1468
1469 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001471
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001472 mbedtls_md5_free( &md5 );
1473 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001474
Paul Bakker380da532012-04-18 16:10:25 +00001475 return;
1476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1480#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001481void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1482 unsigned char hash[32],
1483 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001484{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001485 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001486
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001487 mbedtls_sha256_init( &sha256 );
1488
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001490
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001491 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001492 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001494 *hlen = 32;
1495
1496 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001498
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001499 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001500
Paul Bakker380da532012-04-18 16:10:25 +00001501 return;
1502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001506void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1507 unsigned char hash[48],
1508 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001509{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001510 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001512 mbedtls_sha512_init( &sha512 );
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001515
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001516 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001517 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001519 *hlen = 48;
1520
1521 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001524 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001525
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 return;
1527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_SHA512_C */
1529#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1532int 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 +02001533{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001534 unsigned char *p = ssl->handshake->premaster;
1535 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001536 const unsigned char *psk = ssl->conf->psk;
1537 size_t psk_len = ssl->conf->psk_len;
1538
1539 /* If the psk callback was called, use its result */
1540 if( ssl->handshake->psk != NULL )
1541 {
1542 psk = ssl->handshake->psk;
1543 psk_len = ssl->handshake->psk_len;
1544 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001545
1546 /*
1547 * PMS = struct {
1548 * opaque other_secret<0..2^16-1>;
1549 * opaque psk<0..2^16-1>;
1550 * };
1551 * with "other_secret" depending on the particular key exchange
1552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1554 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001555 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001556 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001558
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001559 *(p++) = (unsigned char)( psk_len >> 8 );
1560 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001561
1562 if( end < p || (size_t)( end - p ) < psk_len )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
1565 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001566 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001567 }
1568 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1570#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1571 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001572 {
1573 /*
1574 * other_secret already set by the ClientKeyExchange message,
1575 * and is 48 bytes long
1576 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001577 if( end - p < 2 )
1578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1579
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001580 *p++ = 0;
1581 *p++ = 48;
1582 p += 48;
1583 }
1584 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1586#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1587 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001588 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001589 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001590 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001591
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001592 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001594 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001595 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001598 return( ret );
1599 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001600 *(p++) = (unsigned char)( len >> 8 );
1601 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 p += len;
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605 }
1606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1608#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1609 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001611 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 size_t zlen;
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001615 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001616 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 return( ret );
1620 }
1621
1622 *(p++) = (unsigned char)( zlen >> 8 );
1623 *(p++) = (unsigned char)( zlen );
1624 p += zlen;
1625
Janos Follath3fbdada2018-08-15 10:26:53 +01001626 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1627 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628 }
1629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634 }
1635
1636 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001637 if( end - p < 2 )
1638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001639
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001640 *(p++) = (unsigned char)( psk_len >> 8 );
1641 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001642
1643 if( end < p || (size_t)( end - p ) < psk_len )
1644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1645
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001646 memcpy( p, psk, psk_len );
1647 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648
1649 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1650
1651 return( 0 );
1652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656/*
1657 * SSLv3.0 MAC functions
1658 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001659#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001660static void ssl_mac( mbedtls_md_context_t *md_ctx,
1661 const unsigned char *secret,
1662 const unsigned char *buf, size_t len,
1663 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001664 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001665{
1666 unsigned char header[11];
1667 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001668 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1670 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001671
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001672 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001674 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001675 else
Paul Bakker68884e32013-01-07 18:20:04 +01001676 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
1678 memcpy( header, ctr, 8 );
1679 header[ 8] = (unsigned char) type;
1680 header[ 9] = (unsigned char)( len >> 8 );
1681 header[10] = (unsigned char)( len );
1682
Paul Bakker68884e32013-01-07 18:20:04 +01001683 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 mbedtls_md_starts( md_ctx );
1685 mbedtls_md_update( md_ctx, secret, md_size );
1686 mbedtls_md_update( md_ctx, padding, padlen );
1687 mbedtls_md_update( md_ctx, header, 11 );
1688 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001689 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Paul Bakker68884e32013-01-07 18:20:04 +01001691 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 mbedtls_md_starts( md_ctx );
1693 mbedtls_md_update( md_ctx, secret, md_size );
1694 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001695 mbedtls_md_update( md_ctx, out, md_size );
1696 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001699
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001700/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001701 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001702#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001703 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1704 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1705 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1706/* This function makes sure every byte in the memory region is accessed
1707 * (in ascending addresses order) */
1708static void ssl_read_memory( unsigned char *p, size_t len )
1709{
1710 unsigned char acc = 0;
1711 volatile unsigned char force;
1712
1713 for( ; len != 0; p++, len-- )
1714 acc ^= *p;
1715
1716 force = acc;
1717 (void) force;
1718}
1719#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1720
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001721/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001723 */
Hanno Becker3307b532017-12-27 21:37:21 +00001724
Hanno Beckera5a2b082019-05-15 14:03:01 +01001725#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001726/* This functions transforms a DTLS plaintext fragment and a record content
1727 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001728 *
1729 * struct {
1730 * opaque content[DTLSPlaintext.length];
1731 * ContentType real_type;
1732 * uint8 zeros[length_of_padding];
1733 * } DTLSInnerPlaintext;
1734 *
1735 * Input:
1736 * - `content`: The beginning of the buffer holding the
1737 * plaintext to be wrapped.
1738 * - `*content_size`: The length of the plaintext in Bytes.
1739 * - `max_len`: The number of Bytes available starting from
1740 * `content`. This must be `>= *content_size`.
1741 * - `rec_type`: The desired record content type.
1742 *
1743 * Output:
1744 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1745 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1746 *
1747 * Returns:
1748 * - `0` on success.
1749 * - A negative error code if `max_len` didn't offer enough space
1750 * for the expansion.
1751 */
1752static int ssl_cid_build_inner_plaintext( unsigned char *content,
1753 size_t *content_size,
1754 size_t remaining,
1755 uint8_t rec_type )
1756{
1757 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001758 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1759 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1760 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001761
1762 /* Write real content type */
1763 if( remaining == 0 )
1764 return( -1 );
1765 content[ len ] = rec_type;
1766 len++;
1767 remaining--;
1768
1769 if( remaining < pad )
1770 return( -1 );
1771 memset( content + len, 0, pad );
1772 len += pad;
1773 remaining -= pad;
1774
1775 *content_size = len;
1776 return( 0 );
1777}
1778
Hanno Becker7dc25772019-05-20 15:08:01 +01001779/* This function parses a DTLSInnerPlaintext structure.
1780 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001781static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1782 size_t *content_size,
1783 uint8_t *rec_type )
1784{
1785 size_t remaining = *content_size;
1786
1787 /* Determine length of padding by skipping zeroes from the back. */
1788 do
1789 {
1790 if( remaining == 0 )
1791 return( -1 );
1792 remaining--;
1793 } while( content[ remaining ] == 0 );
1794
1795 *content_size = remaining;
1796 *rec_type = content[ remaining ];
1797
1798 return( 0 );
1799}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001800#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001801
Hanno Becker99abf512019-05-20 14:50:53 +01001802/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001803 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001804static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001805 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001806 mbedtls_record *rec )
1807{
Hanno Becker99abf512019-05-20 14:50:53 +01001808 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001809 *
1810 * additional_data = seq_num + TLSCompressed.type +
1811 * TLSCompressed.version + TLSCompressed.length;
1812 *
Hanno Becker99abf512019-05-20 14:50:53 +01001813 * For the CID extension, this is extended as follows
1814 * (quoting draft-ietf-tls-dtls-connection-id-05,
1815 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001816 *
1817 * additional_data = seq_num + DTLSPlaintext.type +
1818 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001819 * cid +
1820 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001821 * length_of_DTLSInnerPlaintext;
1822 */
1823
Hanno Becker3307b532017-12-27 21:37:21 +00001824 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1825 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001826 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001827
Hanno Beckera5a2b082019-05-15 14:03:01 +01001828#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001829 if( rec->cid_len != 0 )
1830 {
1831 memcpy( add_data + 11, rec->cid, rec->cid_len );
1832 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1833 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1834 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1835 *add_data_len = 13 + 1 + rec->cid_len;
1836 }
1837 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001838#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001839 {
1840 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1841 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1842 *add_data_len = 13;
1843 }
Hanno Becker3307b532017-12-27 21:37:21 +00001844}
1845
Hanno Becker611a83b2018-01-03 14:27:32 +00001846int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1847 mbedtls_ssl_transform *transform,
1848 mbedtls_record *rec,
1849 int (*f_rng)(void *, unsigned char *, size_t),
1850 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001853 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001854 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001855 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001856 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001857 size_t post_avail;
1858
1859 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001860#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001861 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001862 ((void) ssl);
1863#endif
1864
1865 /* The PRNG is used for dynamic IV generation that's used
1866 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1867#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1868 ( defined(MBEDTLS_AES_C) || \
1869 defined(MBEDTLS_ARIA_C) || \
1870 defined(MBEDTLS_CAMELLIA_C) ) && \
1871 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1872 ((void) f_rng);
1873 ((void) p_rng);
1874#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Hanno Becker3307b532017-12-27 21:37:21 +00001878 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001879 {
Hanno Becker3307b532017-12-27 21:37:21 +00001880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1881 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1882 }
Hanno Becker505089d2019-05-01 09:45:57 +01001883 if( rec == NULL
1884 || rec->buf == NULL
1885 || rec->buf_len < rec->data_offset
1886 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001887#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001888 || rec->cid_len != 0
1889#endif
1890 )
Hanno Becker3307b532017-12-27 21:37:21 +00001891 {
1892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001894 }
1895
Hanno Becker3307b532017-12-27 21:37:21 +00001896 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001897 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001899 data, rec->data_len );
1900
1901 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1902
1903 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1904 {
1905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1906 (unsigned) rec->data_len,
1907 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1909 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001910
Hanno Beckera5a2b082019-05-15 14:03:01 +01001911#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001912 /*
1913 * Add CID information
1914 */
1915 rec->cid_len = transform->out_cid_len;
1916 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1917 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001918
1919 if( rec->cid_len != 0 )
1920 {
1921 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001922 * Wrap plaintext into DTLSInnerPlaintext structure.
1923 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001924 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001925 * Note that this changes `rec->data_len`, and hence
1926 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001927 */
1928 if( ssl_cid_build_inner_plaintext( data,
1929 &rec->data_len,
1930 post_avail,
1931 rec->type ) != 0 )
1932 {
1933 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1934 }
1935
1936 rec->type = MBEDTLS_SSL_MSG_CID;
1937 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001938#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001939
Hanno Becker92c930f2019-04-29 17:31:37 +01001940 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1941
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001943 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001945#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 if( mode == MBEDTLS_MODE_STREAM ||
1947 ( mode == MBEDTLS_MODE_CBC
1948#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001949 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001950#endif
1951 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 {
Hanno Becker3307b532017-12-27 21:37:21 +00001953 if( post_avail < transform->maclen )
1954 {
1955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1956 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1957 }
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001960 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001961 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001962 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001963 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1964 data, rec->data_len, rec->ctr, rec->type, mac );
1965 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001966 }
1967 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001968#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1970 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001971 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001972 {
Hanno Becker992b6872017-11-09 18:57:39 +00001973 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1974
Hanno Beckere83efe62019-04-29 13:52:53 +01001975 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001976
Hanno Becker3307b532017-12-27 21:37:21 +00001977 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001978 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001979 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1980 data, rec->data_len );
1981 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1982 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1983
1984 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001985 }
1986 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001987#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001991 }
1992
Hanno Becker3307b532017-12-27 21:37:21 +00001993 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1994 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001995
Hanno Becker3307b532017-12-27 21:37:21 +00001996 rec->data_len += transform->maclen;
1997 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001998 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001999 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002000#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002002 /*
2003 * Encrypt
2004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2006 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002008 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002009 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002011 "including %d bytes of padding",
2012 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Hanno Becker3307b532017-12-27 21:37:21 +00002014 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2015 transform->iv_enc, transform->ivlen,
2016 data, rec->data_len,
2017 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002020 return( ret );
2021 }
2022
Hanno Becker3307b532017-12-27 21:37:21 +00002023 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002027 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
Paul Bakker68884e32013-01-07 18:20:04 +01002029 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002031
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002032#if defined(MBEDTLS_GCM_C) || \
2033 defined(MBEDTLS_CCM_C) || \
2034 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002036 mode == MBEDTLS_MODE_CCM ||
2037 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002038 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002039 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002041 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002042
Hanno Becker3307b532017-12-27 21:37:21 +00002043 /* Check that there's space for both the authentication tag
2044 * and the explicit IV before and after the record content. */
2045 if( post_avail < transform->taglen ||
2046 rec->data_offset < explicit_iv_len )
2047 {
2048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2049 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2050 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002051
Paul Bakker68884e32013-01-07 18:20:04 +01002052 /*
2053 * Generate IV
2054 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002055 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2056 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002057 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002059 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2060 explicit_iv_len );
2061 /* Prefix record content with explicit IV. */
2062 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002063 }
2064 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2065 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002066 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 unsigned char i;
2068
2069 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2070
2071 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002072 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002073 }
2074 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002075 {
2076 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002079 }
2080
Hanno Beckere83efe62019-04-29 13:52:53 +01002081 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002082
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002083 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2084 iv, transform->ivlen );
2085 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002086 data - explicit_iv_len, explicit_iv_len );
2087 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002088 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002091 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002092
Paul Bakker68884e32013-01-07 18:20:04 +01002093 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002094 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002095 */
Hanno Becker3307b532017-12-27 21:37:21 +00002096
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002097 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002098 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002099 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002100 data, rec->data_len, /* source */
2101 data, &rec->data_len, /* destination */
2102 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002105 return( ret );
2106 }
2107
Hanno Becker3307b532017-12-27 21:37:21 +00002108 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2109 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002110
Hanno Becker3307b532017-12-27 21:37:21 +00002111 rec->data_len += transform->taglen + explicit_iv_len;
2112 rec->data_offset -= explicit_iv_len;
2113 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002114 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002115 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2118#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002119 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002122 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002123 size_t padlen, i;
2124 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002125
Hanno Becker3307b532017-12-27 21:37:21 +00002126 /* Currently we're always using minimal padding
2127 * (up to 255 bytes would be allowed). */
2128 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2129 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 padlen = 0;
2131
Hanno Becker3307b532017-12-27 21:37:21 +00002132 /* Check there's enough space in the buffer for the padding. */
2133 if( post_avail < padlen + 1 )
2134 {
2135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2136 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2137 }
2138
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002140 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Hanno Becker3307b532017-12-27 21:37:21 +00002142 rec->data_len += padlen + 1;
2143 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002146 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002147 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2148 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149 */
Hanno Becker3307b532017-12-27 21:37:21 +00002150 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002151 {
Hanno Becker3307b532017-12-27 21:37:21 +00002152 if( f_rng == NULL )
2153 {
2154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2156 }
2157
2158 if( rec->data_offset < transform->ivlen )
2159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2161 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2162 }
2163
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002164 /*
2165 * Generate IV
2166 */
Hanno Becker3307b532017-12-27 21:37:21 +00002167 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002168 if( ret != 0 )
2169 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002170
Hanno Becker3307b532017-12-27 21:37:21 +00002171 memcpy( data - transform->ivlen, transform->iv_enc,
2172 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002173
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002174 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002178 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002179 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002180 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181
Hanno Becker3307b532017-12-27 21:37:21 +00002182 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2183 transform->iv_enc,
2184 transform->ivlen,
2185 data, rec->data_len,
2186 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002189 return( ret );
2190 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002191
Hanno Becker3307b532017-12-27 21:37:21 +00002192 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002196 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002199 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002200 {
2201 /*
2202 * Save IV in SSL3 and TLS1
2203 */
Hanno Becker3307b532017-12-27 21:37:21 +00002204 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2205 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
Hanno Becker3307b532017-12-27 21:37:21 +00002207 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002208#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002209 {
2210 data -= transform->ivlen;
2211 rec->data_offset -= transform->ivlen;
2212 rec->data_len += transform->ivlen;
2213 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002216 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002217 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002218 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2219
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002220 /*
2221 * MAC(MAC_write_key, seq_num +
2222 * TLSCipherText.type +
2223 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002224 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002225 * IV + // except for TLS 1.0
2226 * ENC(content + padding + padding_length));
2227 */
Hanno Becker3307b532017-12-27 21:37:21 +00002228
2229 if( post_avail < transform->maclen)
2230 {
2231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2232 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2233 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234
Hanno Beckere83efe62019-04-29 13:52:53 +01002235 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002238 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002239 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002240
Hanno Becker3307b532017-12-27 21:37:21 +00002241 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002242 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002243 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2244 data, rec->data_len );
2245 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2246 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002247
Hanno Becker3307b532017-12-27 21:37:21 +00002248 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002249
Hanno Becker3307b532017-12-27 21:37:21 +00002250 rec->data_len += transform->maclen;
2251 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002252 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002253 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002258 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002264 /* Make extra sure authentication was performed, exactly once */
2265 if( auth_done != 1 )
2266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002269 }
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 return( 0 );
2274}
2275
Hanno Becker611a83b2018-01-03 14:27:32 +00002276int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2277 mbedtls_ssl_transform *transform,
2278 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002280 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002282 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002283#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002284 size_t padlen = 0, correct = 1;
2285#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002286 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002287 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002288 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002289
Hanno Becker611a83b2018-01-03 14:27:32 +00002290#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002291 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002292 ((void) ssl);
2293#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002296 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002297 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2300 }
2301 if( rec == NULL ||
2302 rec->buf == NULL ||
2303 rec->buf_len < rec->data_offset ||
2304 rec->buf_len - rec->data_offset < rec->data_len )
2305 {
2306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002308 }
2309
Hanno Becker4c6876b2017-12-27 21:28:58 +00002310 data = rec->buf + rec->data_offset;
2311 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Hanno Beckera5a2b082019-05-15 14:03:01 +01002313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002314 /*
2315 * Match record's CID with incoming CID.
2316 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002317 if( rec->cid_len != transform->in_cid_len ||
2318 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2319 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002320 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002321 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002322#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2325 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002326 {
2327 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002328 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2329 transform->iv_dec,
2330 transform->ivlen,
2331 data, rec->data_len,
2332 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002335 return( ret );
2336 }
2337
Hanno Becker4c6876b2017-12-27 21:28:58 +00002338 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 }
Paul Bakker68884e32013-01-07 18:20:04 +01002344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002346#if defined(MBEDTLS_GCM_C) || \
2347 defined(MBEDTLS_CCM_C) || \
2348 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002350 mode == MBEDTLS_MODE_CCM ||
2351 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002352 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002355
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 /*
2357 * Compute and update sizes
2358 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002359 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002362 "+ taglen (%d)", rec->data_len,
2363 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002365 }
Paul Bakker68884e32013-01-07 18:20:04 +01002366
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002367 /*
2368 * Prepare IV
2369 */
2370 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2371 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002372 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002373 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002374 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002375
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002376 }
2377 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2378 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002379 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002380 unsigned char i;
2381
2382 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2383
2384 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002385 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002386 }
2387 else
2388 {
2389 /* Reminder if we ever add an AEAD mode with a different size */
2390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2392 }
2393
Hanno Becker4c6876b2017-12-27 21:28:58 +00002394 data += explicit_iv_len;
2395 rec->data_offset += explicit_iv_len;
2396 rec->data_len -= explicit_iv_len + transform->taglen;
2397
Hanno Beckere83efe62019-04-29 13:52:53 +01002398 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002399 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401
2402 memcpy( transform->iv_dec + transform->fixed_ivlen,
2403 data - explicit_iv_len, explicit_iv_len );
2404
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002405 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002406 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002407 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002408
Paul Bakker68884e32013-01-07 18:20:04 +01002409
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002410 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002411 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002412 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002413 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2414 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002415 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002416 data, rec->data_len,
2417 data, &olen,
2418 data + rec->data_len,
2419 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2424 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002425
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002426 return( ret );
2427 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002428 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002429
Hanno Becker4c6876b2017-12-27 21:28:58 +00002430 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2433 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002434 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2438#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002439 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002442 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 /*
Paul Bakker45829992013-01-03 14:52:21 +01002445 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002448 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2449 {
2450 /* The ciphertext is prefixed with the CBC IV. */
2451 minlen += transform->ivlen;
2452 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002453#endif
Paul Bakker45829992013-01-03 14:52:21 +01002454
Hanno Becker4c6876b2017-12-27 21:28:58 +00002455 /* Size considerations:
2456 *
2457 * - The CBC cipher text must not be empty and hence
2458 * at least of size transform->ivlen.
2459 *
2460 * Together with the potential IV-prefix, this explains
2461 * the first of the two checks below.
2462 *
2463 * - The record must contain a MAC, either in plain or
2464 * encrypted, depending on whether Encrypt-then-MAC
2465 * is used or not.
2466 * - If it is, the message contains the IV-prefix,
2467 * the CBC ciphertext, and the MAC.
2468 * - If it is not, the padded plaintext, and hence
2469 * the CBC ciphertext, has at least length maclen + 1
2470 * because there is at least the padding length byte.
2471 *
2472 * As the CBC ciphertext is not empty, both cases give the
2473 * lower bound minlen + maclen + 1 on the record size, which
2474 * we test for in the second check below.
2475 */
2476 if( rec->data_len < minlen + transform->ivlen ||
2477 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002480 "+ 1 ) ( + expl IV )", rec->data_len,
2481 transform->ivlen,
2482 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002484 }
2485
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002486 /*
2487 * Authenticate before decrypt if enabled
2488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002491 {
Hanno Becker992b6872017-11-09 18:57:39 +00002492 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002495
Hanno Becker4c6876b2017-12-27 21:28:58 +00002496 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2497 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498
Hanno Beckere83efe62019-04-29 13:52:53 +01002499 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002500
Hanno Beckere83efe62019-04-29 13:52:53 +01002501 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2502 add_data_len );
2503 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2504 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002505 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2506 data, rec->data_len );
2507 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2508 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002509
Hanno Becker4c6876b2017-12-27 21:28:58 +00002510 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2511 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002512 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002513 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
Hanno Becker4c6876b2017-12-27 21:28:58 +00002515 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2516 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002521 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002522 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002524
2525 /*
2526 * Check length sanity
2527 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002528 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002533 }
2534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002536 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002537 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002538 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002539 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002540 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002541 /* This is safe because data_len >= minlen + maclen + 1 initially,
2542 * and at this point we have at most subtracted maclen (note that
2543 * minlen == transform->ivlen here). */
2544 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545
Hanno Becker4c6876b2017-12-27 21:28:58 +00002546 data += transform->ivlen;
2547 rec->data_offset += transform->ivlen;
2548 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002549 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002551
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2553 transform->iv_dec, transform->ivlen,
2554 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002557 return( ret );
2558 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002559
Hanno Becker4c6876b2017-12-27 21:28:58 +00002560 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002568 {
2569 /*
2570 * Save IV in SSL3 and TLS1
2571 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002572 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2573 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002575#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Hanno Becker4c6876b2017-12-27 21:28:58 +00002577 /* Safe since data_len >= minlen + maclen + 1, so after having
2578 * subtracted at most minlen and maclen up to this point,
2579 * data_len > 0. */
2580 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002581
Hanno Becker4c6876b2017-12-27 21:28:58 +00002582 if( auth_done == 1 )
2583 {
2584 correct *= ( rec->data_len >= padlen + 1 );
2585 padlen *= ( rec->data_len >= padlen + 1 );
2586 }
2587 else
Paul Bakker45829992013-01-03 14:52:21 +01002588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002590 if( rec->data_len < transform->maclen + padlen + 1 )
2591 {
2592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2593 rec->data_len,
2594 transform->maclen,
2595 padlen + 1 ) );
2596 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002597#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002598
2599 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2600 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Hanno Becker4c6876b2017-12-27 21:28:58 +00002603 padlen++;
2604
2605 /* Regardless of the validity of the padding,
2606 * we have data_len >= padlen here. */
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002609 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002611 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_DEBUG_ALL)
2614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002615 "should be no more than %d",
2616 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002617#endif
Paul Bakker45829992013-01-03 14:52:21 +01002618 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620 }
2621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2623#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2624 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002625 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002627 /* The padding check involves a series of up to 256
2628 * consecutive memory reads at the end of the record
2629 * plaintext buffer. In order to hide the length and
2630 * validity of the padding, always perform exactly
2631 * `min(256,plaintext_len)` reads (but take into account
2632 * only the last `padlen` bytes for the padding check). */
2633 size_t pad_count = 0;
2634 size_t real_count = 0;
2635 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002636
Hanno Becker4c6876b2017-12-27 21:28:58 +00002637 /* Index of first padding byte; it has been ensured above
2638 * that the subtraction is safe. */
2639 size_t const padding_idx = rec->data_len - padlen;
2640 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2641 size_t const start_idx = rec->data_len - num_checks;
2642 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002643
Hanno Becker4c6876b2017-12-27 21:28:58 +00002644 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002645 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002646 real_count |= ( idx >= padding_idx );
2647 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002648 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002649 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002652 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002654#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002655 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002657 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2659 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002663 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002664
Hanno Becker4c6876b2017-12-27 21:28:58 +00002665 /* If the padding was found to be invalid, padlen == 0
2666 * and the subtraction is safe. If the padding was found valid,
2667 * padlen hasn't been changed and the previous assertion
2668 * data_len >= padlen still holds. */
2669 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002671 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002673 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002679#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002681 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002682#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
2684 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002685 * Authenticate if not done yet.
2686 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002688#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002689 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002690 {
Hanno Becker992b6872017-11-09 18:57:39 +00002691 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002692
Hanno Becker4c6876b2017-12-27 21:28:58 +00002693 /* If the initial value of padlen was such that
2694 * data_len < maclen + padlen + 1, then padlen
2695 * got reset to 1, and the initial check
2696 * data_len >= minlen + maclen + 1
2697 * guarantees that at this point we still
2698 * have at least data_len >= maclen.
2699 *
2700 * If the initial value of padlen was such that
2701 * data_len >= maclen + padlen + 1, then we have
2702 * subtracted either padlen + 1 (if the padding was correct)
2703 * or 0 (if the padding was incorrect) since then,
2704 * hence data_len >= maclen in any case.
2705 */
2706 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Hanno Beckere83efe62019-04-29 13:52:53 +01002708 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002711 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002712 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002713 ssl_mac( &transform->md_ctx_dec,
2714 transform->mac_dec,
2715 data, rec->data_len,
2716 rec->ctr, rec->type,
2717 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002718 }
2719 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2721#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2722 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002723 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002724 {
2725 /*
2726 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002727 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002728 *
2729 * Known timing attacks:
2730 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2731 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002732 * To compensate for different timings for the MAC calculation
2733 * depending on how much padding was removed (which is determined
2734 * by padlen), process extra_run more blocks through the hash
2735 * function.
2736 *
2737 * The formula in the paper is
2738 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2739 * where L1 is the size of the header plus the decrypted message
2740 * plus CBC padding and L2 is the size of the header plus the
2741 * decrypted message. This is for an underlying hash function
2742 * with 64-byte blocks.
2743 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2744 * correctly. We round down instead of up, so -56 is the correct
2745 * value for our calculations instead of -55.
2746 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002747 * Repeat the formula rather than defining a block_size variable.
2748 * This avoids requiring division by a variable at runtime
2749 * (which would be marginally less efficient and would require
2750 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002751 */
2752 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002753 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002754
2755 /*
2756 * The next two sizes are the minimum and maximum values of
2757 * in_msglen over all padlen values.
2758 *
2759 * They're independent of padlen, since we previously did
2760 * in_msglen -= padlen.
2761 *
2762 * Note that max_len + maclen is never more than the buffer
2763 * length, as we previously did in_msglen -= maclen too.
2764 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002765 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002766 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2767
Hanno Becker4c6876b2017-12-27 21:28:58 +00002768 memset( tmp, 0, sizeof( tmp ) );
2769
2770 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002771 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002772#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2773 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002774 case MBEDTLS_MD_MD5:
2775 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002776 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002778 extra_run =
2779 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2780 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002781 break;
2782#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002783#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002784 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002785 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002786 extra_run =
2787 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2788 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002789 break;
2790#endif
2791 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2794 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002795
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002796 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002797
Hanno Beckere83efe62019-04-29 13:52:53 +01002798 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2799 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002800 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2801 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002802 /* Make sure we access everything even when padlen > 0. This
2803 * makes the synchronisation requirements for just-in-time
2804 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002805 ssl_read_memory( data + rec->data_len, padlen );
2806 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002807
2808 /* Call mbedtls_md_process at least once due to cache attacks
2809 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002810 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002811 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002812
Hanno Becker4c6876b2017-12-27 21:28:58 +00002813 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002814
2815 /* Make sure we access all the memory that could contain the MAC,
2816 * before we check it in the next code block. This makes the
2817 * synchronisation requirements for just-in-time Prime+Probe
2818 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002819 ssl_read_memory( data + min_len,
2820 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002821 }
2822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2824 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2827 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002828 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002830#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002831 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2832 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Hanno Becker4c6876b2017-12-27 21:28:58 +00002835 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2836 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838#if defined(MBEDTLS_SSL_DEBUG_ALL)
2839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002840#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002841 correct = 0;
2842 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002843 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002844 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002845
2846 /*
2847 * Finally check the correct flag
2848 */
2849 if( correct == 0 )
2850 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002851#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002852
2853 /* Make extra sure authentication was performed, exactly once */
2854 if( auth_done != 1 )
2855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002858 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Hanno Beckera5a2b082019-05-15 14:03:01 +01002860#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002861 if( rec->cid_len != 0 )
2862 {
2863 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2864 &rec->type );
2865 if( ret != 0 )
2866 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2867 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002868#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
2872 return( 0 );
2873}
2874
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002875#undef MAC_NONE
2876#undef MAC_PLAINTEXT
2877#undef MAC_CIPHERTEXT
2878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002880/*
2881 * Compression/decompression functions
2882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002884{
2885 int ret;
2886 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002887 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002888 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002889 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002892
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002893 if( len_pre == 0 )
2894 return( 0 );
2895
Paul Bakker2770fbd2012-07-03 13:30:23 +00002896 memcpy( msg_pre, ssl->out_msg, len_pre );
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 ssl->out_msglen ) );
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902 ssl->out_msg, ssl->out_msglen );
2903
Paul Bakker48916f92012-09-16 19:57:18 +00002904 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2905 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2906 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002907 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908
Paul Bakker48916f92012-09-16 19:57:18 +00002909 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002910 if( ret != Z_OK )
2911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2913 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914 }
2915
Angus Grattond8213d02016-05-25 20:56:48 +10002916 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002917 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 ssl->out_msglen ) );
2921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 ssl->out_msg, ssl->out_msglen );
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926
2927 return( 0 );
2928}
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931{
2932 int ret;
2933 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002934 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002935 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002936 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002939
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002940 if( len_pre == 0 )
2941 return( 0 );
2942
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 memcpy( msg_pre, ssl->in_msg, len_pre );
2944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946 ssl->in_msglen ) );
2947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002949 ssl->in_msg, ssl->in_msglen );
2950
Paul Bakker48916f92012-09-16 19:57:18 +00002951 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2952 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2953 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002954 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002955 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956
Paul Bakker48916f92012-09-16 19:57:18 +00002957 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958 if( ret != Z_OK )
2959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2961 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002962 }
2963
Angus Grattond8213d02016-05-25 20:56:48 +10002964 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002965 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002968 ssl->in_msglen ) );
2969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002971 ssl->in_msg, ssl->in_msglen );
2972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002974
2975 return( 0 );
2976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2980static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#if defined(MBEDTLS_SSL_PROTO_DTLS)
2983static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002984{
2985 /* If renegotiation is not enforced, retransmit until we would reach max
2986 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002987 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002988 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002989 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002990 unsigned char doublings = 1;
2991
2992 while( ratio != 0 )
2993 {
2994 ++doublings;
2995 ratio >>= 1;
2996 }
2997
2998 if( ++ssl->renego_records_seen > doublings )
2999 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003001 return( 0 );
3002 }
3003 }
3004
3005 return( ssl_write_hello_request( ssl ) );
3006}
3007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003009
Paul Bakker5121ce52009-01-03 21:22:43 +00003010/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003011 * Fill the input message buffer by appending data to it.
3012 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003013 *
3014 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3015 * available (from this read and/or a previous one). Otherwise, an error code
3016 * is returned (possibly EOF or WANT_READ).
3017 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003018 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3019 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3020 * since we always read a whole datagram at once.
3021 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003022 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003023 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003026{
Paul Bakker23986e52011-04-24 08:57:21 +00003027 int ret;
3028 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003032 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003035 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003037 }
3038
Angus Grattond8213d02016-05-25 20:56:48 +10003039 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003043 }
3044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003046 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003048 uint32_t timeout;
3049
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003050 /* Just to be sure */
3051 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3052 {
3053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3054 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3056 }
3057
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003058 /*
3059 * The point is, we need to always read a full datagram at once, so we
3060 * sometimes read more then requested, and handle the additional data.
3061 * It could be the rest of the current record (while fetching the
3062 * header) and/or some other records in the same datagram.
3063 */
3064
3065 /*
3066 * Move to the next record in the already read datagram if applicable
3067 */
3068 if( ssl->next_record_offset != 0 )
3069 {
3070 if( ssl->in_left < ssl->next_record_offset )
3071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003074 }
3075
3076 ssl->in_left -= ssl->next_record_offset;
3077
3078 if( ssl->in_left != 0 )
3079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003081 ssl->next_record_offset ) );
3082 memmove( ssl->in_hdr,
3083 ssl->in_hdr + ssl->next_record_offset,
3084 ssl->in_left );
3085 }
3086
3087 ssl->next_record_offset = 0;
3088 }
3089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003092
3093 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003094 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003095 */
3096 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003099 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003100 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003101
3102 /*
3103 * A record can't be split accross datagrams. If we need to read but
3104 * are not at the beginning of a new record, the caller did something
3105 * wrong.
3106 */
3107 if( ssl->in_left != 0 )
3108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003111 }
3112
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113 /*
3114 * Don't even try to read if time's out already.
3115 * This avoids by-passing the timer when repeatedly receiving messages
3116 * that will end up being dropped.
3117 */
3118 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003119 {
3120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003121 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003122 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003123 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003124 {
Angus Grattond8213d02016-05-25 20:56:48 +10003125 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003128 timeout = ssl->handshake->retransmit_timeout;
3129 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003130 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003133
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003134 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003135 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3136 timeout );
3137 else
3138 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003141
3142 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003144 }
3145
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003146 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003149 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003152 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003153 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003156 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003157 }
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003162 return( ret );
3163 }
3164
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003165 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003166 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003168 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003170 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003171 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003174 return( ret );
3175 }
3176
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003177 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003180 }
3181
Paul Bakker5121ce52009-01-03 21:22:43 +00003182 if( ret < 0 )
3183 return( ret );
3184
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003185 ssl->in_left = ret;
3186 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003187 MBEDTLS_SSL_TRANSPORT_ELSE
3188#endif /* MBEDTLS_SSL_PROTO_DTLS */
3189#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003192 ssl->in_left, nb_want ) );
3193
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003194 while( ssl->in_left < nb_want )
3195 {
3196 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003197
3198 if( ssl_check_timer( ssl ) != 0 )
3199 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3200 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003201 {
3202 if( ssl->f_recv_timeout != NULL )
3203 {
3204 ret = ssl->f_recv_timeout( ssl->p_bio,
3205 ssl->in_hdr + ssl->in_left, len,
3206 ssl->conf->read_timeout );
3207 }
3208 else
3209 {
3210 ret = ssl->f_recv( ssl->p_bio,
3211 ssl->in_hdr + ssl->in_left, len );
3212 }
3213 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003216 ssl->in_left, nb_want ) );
3217 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003218
3219 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003221
3222 if( ret < 0 )
3223 return( ret );
3224
mohammad160352aecb92018-03-28 23:41:40 -07003225 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003226 {
Darryl Green11999bb2018-03-13 15:22:58 +00003227 MBEDTLS_SSL_DEBUG_MSG( 1,
3228 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003229 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3231 }
3232
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003233 ssl->in_left += ret;
3234 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003236#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
3240 return( 0 );
3241}
3242
3243/*
3244 * Flush any data not yet written
3245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003247{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003248 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003249 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003252
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003253 if( ssl->f_send == NULL )
3254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003256 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003258 }
3259
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003260 /* Avoid incrementing counter if data is flushed */
3261 if( ssl->out_left == 0 )
3262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003264 return( 0 );
3265 }
3266
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 while( ssl->out_left > 0 )
3268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003270 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003271
Hanno Becker2b1e3542018-08-06 11:19:13 +01003272 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003273 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
3277 if( ret <= 0 )
3278 return( ret );
3279
mohammad160352aecb92018-03-28 23:41:40 -07003280 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003281 {
Darryl Green11999bb2018-03-13 15:22:58 +00003282 MBEDTLS_SSL_DEBUG_MSG( 1,
3283 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003284 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3286 }
3287
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 ssl->out_left -= ret;
3289 }
3290
Hanno Becker2b1e3542018-08-06 11:19:13 +01003291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003292 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003293 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003294 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003295 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003296 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003297#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003298#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003299 {
3300 ssl->out_hdr = ssl->out_buf + 8;
3301 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003302#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003303 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
3307 return( 0 );
3308}
3309
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003310/*
3311 * Functions to handle the DTLS retransmission state machine
3312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003314/*
3315 * Append current handshake message to current outgoing flight
3316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3321 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3322 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323
3324 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003325 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003326 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003329 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003330 }
3331
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003332 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003336 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003337 }
3338
3339 /* Copy current handshake message with headers */
3340 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3341 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003342 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343 msg->next = NULL;
3344
3345 /* Append to the current flight */
3346 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003347 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348 else
3349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003351 while( cur->next != NULL )
3352 cur = cur->next;
3353 cur->next = msg;
3354 }
3355
Hanno Becker3b235902018-08-06 09:54:53 +01003356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357 return( 0 );
3358}
3359
3360/*
3361 * Free the current flight of handshake messages
3362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 mbedtls_ssl_flight_item *cur = flight;
3366 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003367
3368 while( cur != NULL )
3369 {
3370 next = cur->next;
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_free( cur->p );
3373 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003374
3375 cur = next;
3376 }
3377}
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3380static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003381#endif
3382
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003383/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003384 * Swap transform_out and out_ctr with the alternative ones
3385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003389 unsigned char tmp_out_ctr[8];
3390
3391 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003394 return;
3395 }
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003398
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003399 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003400 tmp_transform = ssl->transform_out;
3401 ssl->transform_out = ssl->handshake->alt_transform_out;
3402 ssl->handshake->alt_transform_out = tmp_transform;
3403
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003404 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003405 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3406 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003408
3409 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003410 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3413 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3418 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003419 }
3420 }
3421#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003422}
3423
3424/*
3425 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003426 */
3427int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3428{
3429 int ret = 0;
3430
3431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3432
3433 ret = mbedtls_ssl_flight_transmit( ssl );
3434
3435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3436
3437 return( ret );
3438}
3439
3440/*
3441 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003442 *
3443 * Need to remember the current message in case flush_output returns
3444 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003445 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003446 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003447int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003448{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003449 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003453 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003455
3456 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003457 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003458 ssl_swap_epochs( ssl );
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003462
3463 while( ssl->handshake->cur_msg != NULL )
3464 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003466 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003467
Hanno Beckere1dcb032018-08-17 16:47:58 +01003468 int const is_finished =
3469 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3470 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3471
Hanno Becker04da1892018-08-14 13:22:10 +01003472 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3473 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3474
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003475 /* Swap epochs before sending Finished: we can't do it after
3476 * sending ChangeCipherSpec, in case write returns WANT_READ.
3477 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003478 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003479 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003481 ssl_swap_epochs( ssl );
3482 }
3483
Hanno Becker67bc7c32018-08-06 11:33:50 +01003484 ret = ssl_get_remaining_payload_in_datagram( ssl );
3485 if( ret < 0 )
3486 return( ret );
3487 max_frag_len = (size_t) ret;
3488
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003489 /* CCS is copied as is, while HS messages may need fragmentation */
3490 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3491 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003492 if( max_frag_len == 0 )
3493 {
3494 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3495 return( ret );
3496
3497 continue;
3498 }
3499
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003500 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003501 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003502 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003503
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003504 /* Update position inside current message */
3505 ssl->handshake->cur_msg_p += cur->len;
3506 }
3507 else
3508 {
3509 const unsigned char * const p = ssl->handshake->cur_msg_p;
3510 const size_t hs_len = cur->len - 12;
3511 const size_t frag_off = p - ( cur->p + 12 );
3512 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003513 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003514
Hanno Beckere1dcb032018-08-17 16:47:58 +01003515 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003516 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003517 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003518 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003519
Hanno Becker67bc7c32018-08-06 11:33:50 +01003520 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3521 return( ret );
3522
3523 continue;
3524 }
3525 max_hs_frag_len = max_frag_len - 12;
3526
3527 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3528 max_hs_frag_len : rem_len;
3529
3530 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003531 {
3532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003533 (unsigned) cur_hs_frag_len,
3534 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003535 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003536
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003537 /* Messages are stored with handshake headers as if not fragmented,
3538 * copy beginning of headers then fill fragmentation fields.
3539 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3540 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003541
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003542 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3543 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3544 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3545
Hanno Becker67bc7c32018-08-06 11:33:50 +01003546 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3547 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3548 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003549
3550 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3551
Hanno Becker3f7b9732018-08-28 09:53:25 +01003552 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003553 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3554 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003555 ssl->out_msgtype = cur->type;
3556
3557 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003558 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003559 }
3560
3561 /* If done with the current message move to the next one if any */
3562 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3563 {
3564 if( cur->next != NULL )
3565 {
3566 ssl->handshake->cur_msg = cur->next;
3567 ssl->handshake->cur_msg_p = cur->next->p + 12;
3568 }
3569 else
3570 {
3571 ssl->handshake->cur_msg = NULL;
3572 ssl->handshake->cur_msg_p = NULL;
3573 }
3574 }
3575
3576 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003577 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580 return( ret );
3581 }
3582 }
3583
Hanno Becker67bc7c32018-08-06 11:33:50 +01003584 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3585 return( ret );
3586
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003587 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3589 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003590 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003593 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3594 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003595
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003597
3598 return( 0 );
3599}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003600
3601/*
3602 * To be called when the last message of an incoming flight is received.
3603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003605{
3606 /* We won't need to resend that one any more */
3607 ssl_flight_free( ssl->handshake->flight );
3608 ssl->handshake->flight = NULL;
3609 ssl->handshake->cur_msg = NULL;
3610
3611 /* The next incoming flight will start with this msg_seq */
3612 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3613
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003614 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003615 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003616
Hanno Becker0271f962018-08-16 13:23:47 +01003617 /* Clear future message buffering structure. */
3618 ssl_buffering_free( ssl );
3619
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003620 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003621 ssl_set_timer( ssl, 0 );
3622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3624 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003627 }
3628 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003630}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003631
3632/*
3633 * To be called when the last message of an outgoing flight is send.
3634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003636{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003637 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003638 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3641 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003644 }
3645 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003649
Paul Bakker5121ce52009-01-03 21:22:43 +00003650/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003651 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003653
3654/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003655 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003656 *
3657 * - fill in handshake headers
3658 * - update handshake checksum
3659 * - DTLS: save message for resending
3660 * - then pass to the record layer
3661 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003662 * DTLS: except for HelloRequest, messages are only queued, and will only be
3663 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003664 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003665 * Inputs:
3666 * - ssl->out_msglen: 4 + actual handshake message len
3667 * (4 is the size of handshake headers for TLS)
3668 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3669 * - ssl->out_msg + 4: the handshake message body
3670 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003671 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003672 * - ssl->out_msglen: the length of the record contents
3673 * (including handshake headers but excluding record headers)
3674 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003675 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003677{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003678 int ret;
3679 const size_t hs_len = ssl->out_msglen - 4;
3680 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3683
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003684 /*
3685 * Sanity checks
3686 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003687 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003688 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3689 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003690 /* In SSLv3, the client might send a NoCertificate alert. */
3691#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3692 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3693 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3694 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3695#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3696 {
3697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3699 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003702 /* Whenever we send anything different from a
3703 * HelloRequest we should be in a handshake - double check. */
3704 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3705 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003706 ssl->handshake == NULL )
3707 {
3708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3710 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003713 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003714 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003719 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003721
Hanno Beckerb50a2532018-08-06 11:52:54 +01003722 /* Double-check that we did not exceed the bounds
3723 * of the outgoing record buffer.
3724 * This should never fail as the various message
3725 * writing functions must obey the bounds of the
3726 * outgoing record buffer, but better be safe.
3727 *
3728 * Note: We deliberately do not check for the MTU or MFL here.
3729 */
3730 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3731 {
3732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3733 "size %u, maximum %u",
3734 (unsigned) ssl->out_msglen,
3735 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3737 }
3738
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003739 /*
3740 * Fill handshake headers
3741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003744 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3745 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3746 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003748 /*
3749 * DTLS has additional fields in the Handshake layer,
3750 * between the length field and the actual payload:
3751 * uint16 message_seq;
3752 * uint24 fragment_offset;
3753 * uint24 fragment_length;
3754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003756 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003757 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003758 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003759 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003760 {
3761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3762 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003763 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003764 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003765 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3766 }
3767
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003768 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003769 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003770
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003771 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003772 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003773 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003774 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3775 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3776 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003777 }
3778 else
3779 {
3780 ssl->out_msg[4] = 0;
3781 ssl->out_msg[5] = 0;
3782 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003783
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003784 /* Handshake hashes are computed without fragmentation,
3785 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003786 memset( ssl->out_msg + 6, 0x00, 3 );
3787 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003790
Hanno Becker0207e532018-08-28 10:28:28 +01003791 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003792 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3793 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 }
3795
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003796 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003798 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003799 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3800 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003801 {
3802 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003805 return( ret );
3806 }
3807 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003808 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003809#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003810 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003811 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003812 {
3813 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3814 return( ret );
3815 }
3816 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003817
3818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3819
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003820 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003821}
3822
3823/*
3824 * Record layer functions
3825 */
3826
3827/*
3828 * Write current record.
3829 *
3830 * Uses:
3831 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3832 * - ssl->out_msglen: length of the record content (excl headers)
3833 * - ssl->out_msg: record content
3834 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003836{
3837 int ret, done = 0;
3838 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003840
3841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003844 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003846 {
3847 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003850 return( ret );
3851 }
3852
3853 len = ssl->out_msglen;
3854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3858 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 ret = mbedtls_ssl_hw_record_write( ssl );
3863 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3866 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003867 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003868
3869 if( ret == 0 )
3870 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003871 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003873 if( !done )
3874 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003875 unsigned i;
3876 size_t protected_record_size;
3877
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003878 /* Skip writing the record content type to after the encryption,
3879 * as it may change when using the CID extension. */
3880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003882 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003883
Hanno Becker19859472018-08-06 09:40:20 +01003884 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003885 ssl->out_len[0] = (unsigned char)( len >> 8 );
3886 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003887
Paul Bakker48916f92012-09-16 19:57:18 +00003888 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003889 {
Hanno Becker3307b532017-12-27 21:37:21 +00003890 mbedtls_record rec;
3891
3892 rec.buf = ssl->out_iv;
3893 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3894 ( ssl->out_iv - ssl->out_buf );
3895 rec.data_len = ssl->out_msglen;
3896 rec.data_offset = ssl->out_msg - rec.buf;
3897
3898 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3899 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3900 ssl->conf->transport, rec.ver );
3901 rec.type = ssl->out_msgtype;
3902
Hanno Beckera5a2b082019-05-15 14:03:01 +01003903#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003904 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003905 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003906#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003907
Hanno Becker611a83b2018-01-03 14:27:32 +00003908 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003909 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003912 return( ret );
3913 }
3914
Hanno Becker3307b532017-12-27 21:37:21 +00003915 if( rec.data_offset != 0 )
3916 {
3917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3919 }
3920
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003921 /* Update the record content type and CID. */
3922 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003923#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003924 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003925#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003926 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003927 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3928 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003929 }
3930
Hanno Becker43395762019-05-03 14:46:38 +01003931 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003932
3933#if defined(MBEDTLS_SSL_PROTO_DTLS)
3934 /* In case of DTLS, double-check that we don't exceed
3935 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003936 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003937 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003938 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003939 if( ret < 0 )
3940 return( ret );
3941
3942 if( protected_record_size > (size_t) ret )
3943 {
3944 /* Should never happen */
3945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3946 }
3947 }
3948#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003949
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003950 /* Now write the potentially updated record content type. */
3951 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003954 "version = [%d:%d], msglen = %d",
3955 ssl->out_hdr[0], ssl->out_hdr[1],
3956 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003959 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003960
3961 ssl->out_left += protected_record_size;
3962 ssl->out_hdr += protected_record_size;
3963 ssl_update_out_pointers( ssl, ssl->transform_out );
3964
Hanno Becker04484622018-08-06 09:49:38 +01003965 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3966 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3967 break;
3968
3969 /* The loop goes to its end iff the counter is wrapping */
3970 if( i == ssl_ep_len( ssl ) )
3971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3973 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3974 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003975 }
3976
Hanno Becker67bc7c32018-08-06 11:33:50 +01003977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003978 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003979 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003980 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003981 size_t remaining;
3982 ret = ssl_get_remaining_payload_in_datagram( ssl );
3983 if( ret < 0 )
3984 {
3985 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3986 ret );
3987 return( ret );
3988 }
3989
3990 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003991 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003992 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003993 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003994 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003995 else
3996 {
Hanno Becker513815a2018-08-20 11:56:09 +01003997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003998 }
3999 }
4000#endif /* MBEDTLS_SSL_PROTO_DTLS */
4001
4002 if( ( flush == SSL_FORCE_FLUSH ) &&
4003 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 return( ret );
4007 }
4008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
4011 return( 0 );
4012}
4013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004015
4016static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4017{
4018 if( ssl->in_msglen < ssl->in_hslen ||
4019 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4020 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4021 {
4022 return( 1 );
4023 }
4024 return( 0 );
4025}
Hanno Becker44650b72018-08-16 12:51:11 +01004026
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004027static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004028{
4029 return( ( ssl->in_msg[9] << 16 ) |
4030 ( ssl->in_msg[10] << 8 ) |
4031 ssl->in_msg[11] );
4032}
4033
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004034static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004035{
4036 return( ( ssl->in_msg[6] << 16 ) |
4037 ( ssl->in_msg[7] << 8 ) |
4038 ssl->in_msg[8] );
4039}
4040
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004041static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004042{
4043 uint32_t msg_len, frag_off, frag_len;
4044
4045 msg_len = ssl_get_hs_total_len( ssl );
4046 frag_off = ssl_get_hs_frag_off( ssl );
4047 frag_len = ssl_get_hs_frag_len( ssl );
4048
4049 if( frag_off > msg_len )
4050 return( -1 );
4051
4052 if( frag_len > msg_len - frag_off )
4053 return( -1 );
4054
4055 if( frag_len + 12 > ssl->in_msglen )
4056 return( -1 );
4057
4058 return( 0 );
4059}
4060
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004061/*
4062 * Mark bits in bitmask (used for DTLS HS reassembly)
4063 */
4064static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4065{
4066 unsigned int start_bits, end_bits;
4067
4068 start_bits = 8 - ( offset % 8 );
4069 if( start_bits != 8 )
4070 {
4071 size_t first_byte_idx = offset / 8;
4072
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004073 /* Special case */
4074 if( len <= start_bits )
4075 {
4076 for( ; len != 0; len-- )
4077 mask[first_byte_idx] |= 1 << ( start_bits - len );
4078
4079 /* Avoid potential issues with offset or len becoming invalid */
4080 return;
4081 }
4082
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004083 offset += start_bits; /* Now offset % 8 == 0 */
4084 len -= start_bits;
4085
4086 for( ; start_bits != 0; start_bits-- )
4087 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4088 }
4089
4090 end_bits = len % 8;
4091 if( end_bits != 0 )
4092 {
4093 size_t last_byte_idx = ( offset + len ) / 8;
4094
4095 len -= end_bits; /* Now len % 8 == 0 */
4096
4097 for( ; end_bits != 0; end_bits-- )
4098 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4099 }
4100
4101 memset( mask + offset / 8, 0xFF, len / 8 );
4102}
4103
4104/*
4105 * Check that bitmask is full
4106 */
4107static int ssl_bitmask_check( unsigned char *mask, size_t len )
4108{
4109 size_t i;
4110
4111 for( i = 0; i < len / 8; i++ )
4112 if( mask[i] != 0xFF )
4113 return( -1 );
4114
4115 for( i = 0; i < len % 8; i++ )
4116 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4117 return( -1 );
4118
4119 return( 0 );
4120}
4121
Hanno Becker56e205e2018-08-16 09:06:12 +01004122/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004123static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004124 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004125{
Hanno Becker56e205e2018-08-16 09:06:12 +01004126 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004127
Hanno Becker56e205e2018-08-16 09:06:12 +01004128 alloc_len = 12; /* Handshake header */
4129 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004130
Hanno Beckerd07df862018-08-16 09:14:58 +01004131 if( add_bitmap )
4132 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004133
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004134 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004135}
Hanno Becker56e205e2018-08-16 09:06:12 +01004136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004138
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004139static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004140{
4141 return( ( ssl->in_msg[1] << 16 ) |
4142 ( ssl->in_msg[2] << 8 ) |
4143 ssl->in_msg[3] );
4144}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004145
Simon Butcher99000142016-10-13 17:21:01 +01004146int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004151 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004153 }
4154
Hanno Becker12555c62018-08-16 12:47:53 +01004155 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004158 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004159 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004162 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004163 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004164 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004165 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004166
Hanno Becker44650b72018-08-16 12:51:11 +01004167 if( ssl_check_hs_header( ssl ) != 0 )
4168 {
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4170 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4171 }
4172
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004173 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004174 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4175 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4176 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4177 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004178 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004179 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4180 {
4181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4182 recv_msg_seq,
4183 ssl->handshake->in_msg_seq ) );
4184 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4185 }
4186
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004187 /* Retransmit only on last message from previous flight, to avoid
4188 * too many retransmissions.
4189 * Besides, No sane server ever retransmits HelloVerifyRequest */
4190 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004194 "message_seq = %d, start_of_flight = %d",
4195 recv_msg_seq,
4196 ssl->handshake->in_flight_start_seq ) );
4197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004201 return( ret );
4202 }
4203 }
4204 else
4205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004207 "message_seq = %d, expected = %d",
4208 recv_msg_seq,
4209 ssl->handshake->in_msg_seq ) );
4210 }
4211
Hanno Becker90333da2017-10-10 11:27:13 +01004212 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004213 }
4214 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004215
Hanno Becker6d97ef52018-08-16 13:09:04 +01004216 /* Message reassembly is handled alongside buffering of future
4217 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004218 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004219 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004220 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004223 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004224 }
4225 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004226 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004228#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004229 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004230 /* With TLS we don't handle fragmentation (for now) */
4231 if( ssl->in_msglen < ssl->in_hslen )
4232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4234 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4235 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004236 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004237#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004238
Simon Butcher99000142016-10-13 17:21:01 +01004239 return( 0 );
4240}
4241
4242void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4243{
Hanno Becker0271f962018-08-16 13:23:47 +01004244 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004245
Hanno Becker0271f962018-08-16 13:23:47 +01004246 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004247 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004248 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004249 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004250
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004251 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004253 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004254 ssl->handshake != NULL )
4255 {
Hanno Becker0271f962018-08-16 13:23:47 +01004256 unsigned offset;
4257 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004258
Hanno Becker0271f962018-08-16 13:23:47 +01004259 /* Increment handshake sequence number */
4260 hs->in_msg_seq++;
4261
4262 /*
4263 * Clear up handshake buffering and reassembly structure.
4264 */
4265
4266 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004267 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004268
4269 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004270 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4271 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004272 offset++, hs_buf++ )
4273 {
4274 *hs_buf = *(hs_buf + 1);
4275 }
4276
4277 /* Create a fresh last entry */
4278 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004279 }
4280#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004281}
4282
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004283/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004284 * DTLS anti-replay: RFC 6347 4.1.2.6
4285 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004286 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4287 * Bit n is set iff record number in_window_top - n has been seen.
4288 *
4289 * Usually, in_window_top is the last record number seen and the lsb of
4290 * in_window is set. The only exception is the initial state (record number 0
4291 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4294static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004295{
4296 ssl->in_window_top = 0;
4297 ssl->in_window = 0;
4298}
4299
4300static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4301{
4302 return( ( (uint64_t) buf[0] << 40 ) |
4303 ( (uint64_t) buf[1] << 32 ) |
4304 ( (uint64_t) buf[2] << 24 ) |
4305 ( (uint64_t) buf[3] << 16 ) |
4306 ( (uint64_t) buf[4] << 8 ) |
4307 ( (uint64_t) buf[5] ) );
4308}
4309
4310/*
4311 * Return 0 if sequence number is acceptable, -1 otherwise
4312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004314{
4315 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4316 uint64_t bit;
4317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004318 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004319 return( 0 );
4320
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321 if( rec_seqnum > ssl->in_window_top )
4322 return( 0 );
4323
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004324 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004325
4326 if( bit >= 64 )
4327 return( -1 );
4328
4329 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4330 return( -1 );
4331
4332 return( 0 );
4333}
4334
4335/*
4336 * Update replay window on new validated record
4337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339{
4340 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4341
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004342 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004343 return;
4344
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345 if( rec_seqnum > ssl->in_window_top )
4346 {
4347 /* Update window_top and the contents of the window */
4348 uint64_t shift = rec_seqnum - ssl->in_window_top;
4349
4350 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004351 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004352 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004353 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004354 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004355 ssl->in_window |= 1;
4356 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004357
4358 ssl->in_window_top = rec_seqnum;
4359 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004360 else
4361 {
4362 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004363 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004364
4365 if( bit < 64 ) /* Always true, but be extra sure */
4366 ssl->in_window |= (uint64_t) 1 << bit;
4367 }
4368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004370
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004371#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004372/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004373static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4374
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004375/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004376 * Without any SSL context, check if a datagram looks like a ClientHello with
4377 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004378 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004379 *
4380 * - if cookie is valid, return 0
4381 * - if ClientHello looks superficially valid but cookie is not,
4382 * fill obuf and set olen, then
4383 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4384 * - otherwise return a specific error code
4385 */
4386static int ssl_check_dtls_clihlo_cookie(
4387 mbedtls_ssl_cookie_write_t *f_cookie_write,
4388 mbedtls_ssl_cookie_check_t *f_cookie_check,
4389 void *p_cookie,
4390 const unsigned char *cli_id, size_t cli_id_len,
4391 const unsigned char *in, size_t in_len,
4392 unsigned char *obuf, size_t buf_len, size_t *olen )
4393{
4394 size_t sid_len, cookie_len;
4395 unsigned char *p;
4396
4397 if( f_cookie_write == NULL || f_cookie_check == NULL )
4398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4399
4400 /*
4401 * Structure of ClientHello with record and handshake headers,
4402 * and expected values. We don't need to check a lot, more checks will be
4403 * done when actually parsing the ClientHello - skipping those checks
4404 * avoids code duplication and does not make cookie forging any easier.
4405 *
4406 * 0-0 ContentType type; copied, must be handshake
4407 * 1-2 ProtocolVersion version; copied
4408 * 3-4 uint16 epoch; copied, must be 0
4409 * 5-10 uint48 sequence_number; copied
4410 * 11-12 uint16 length; (ignored)
4411 *
4412 * 13-13 HandshakeType msg_type; (ignored)
4413 * 14-16 uint24 length; (ignored)
4414 * 17-18 uint16 message_seq; copied
4415 * 19-21 uint24 fragment_offset; copied, must be 0
4416 * 22-24 uint24 fragment_length; (ignored)
4417 *
4418 * 25-26 ProtocolVersion client_version; (ignored)
4419 * 27-58 Random random; (ignored)
4420 * 59-xx SessionID session_id; 1 byte len + sid_len content
4421 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4422 * ...
4423 *
4424 * Minimum length is 61 bytes.
4425 */
4426 if( in_len < 61 ||
4427 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4428 in[3] != 0 || in[4] != 0 ||
4429 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4430 {
4431 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4432 }
4433
4434 sid_len = in[59];
4435 if( sid_len > in_len - 61 )
4436 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4437
4438 cookie_len = in[60 + sid_len];
4439 if( cookie_len > in_len - 60 )
4440 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4441
4442 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4443 cli_id, cli_id_len ) == 0 )
4444 {
4445 /* Valid cookie */
4446 return( 0 );
4447 }
4448
4449 /*
4450 * If we get here, we've got an invalid cookie, let's prepare HVR.
4451 *
4452 * 0-0 ContentType type; copied
4453 * 1-2 ProtocolVersion version; copied
4454 * 3-4 uint16 epoch; copied
4455 * 5-10 uint48 sequence_number; copied
4456 * 11-12 uint16 length; olen - 13
4457 *
4458 * 13-13 HandshakeType msg_type; hello_verify_request
4459 * 14-16 uint24 length; olen - 25
4460 * 17-18 uint16 message_seq; copied
4461 * 19-21 uint24 fragment_offset; copied
4462 * 22-24 uint24 fragment_length; olen - 25
4463 *
4464 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4465 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4466 *
4467 * Minimum length is 28.
4468 */
4469 if( buf_len < 28 )
4470 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4471
4472 /* Copy most fields and adapt others */
4473 memcpy( obuf, in, 25 );
4474 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4475 obuf[25] = 0xfe;
4476 obuf[26] = 0xff;
4477
4478 /* Generate and write actual cookie */
4479 p = obuf + 28;
4480 if( f_cookie_write( p_cookie,
4481 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4482 {
4483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4484 }
4485
4486 *olen = p - obuf;
4487
4488 /* Go back and fill length fields */
4489 obuf[27] = (unsigned char)( *olen - 28 );
4490
4491 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4492 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4493 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4494
4495 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4496 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4497
4498 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4499}
4500
4501/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004502 * Handle possible client reconnect with the same UDP quadruplet
4503 * (RFC 6347 Section 4.2.8).
4504 *
4505 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4506 * that looks like a ClientHello.
4507 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004508 * - if the input looks like a ClientHello without cookies,
4509 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004511 * - if the input looks like a ClientHello with a valid cookie,
4512 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004513 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004514 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004515 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004516 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004517 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4518 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004519 */
4520static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4521{
4522 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004524
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004525 ret = ssl_check_dtls_clihlo_cookie(
4526 ssl->conf->f_cookie_write,
4527 ssl->conf->f_cookie_check,
4528 ssl->conf->p_cookie,
4529 ssl->cli_id, ssl->cli_id_len,
4530 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004531 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004532
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004533 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4534
4535 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004536 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004537 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004538 * If the error is permanent we'll catch it later,
4539 * if it's not, then hopefully it'll work next time. */
4540 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4541
4542 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004543 }
4544
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004545 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004546 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004547 /* Got a valid cookie, partially reset context */
4548 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4549 {
4550 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4551 return( ret );
4552 }
4553
4554 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004555 }
4556
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004557 return( ret );
4558}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004559#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004560
Hanno Becker46483f12019-05-03 13:25:54 +01004561static int ssl_check_record_type( uint8_t record_type )
4562{
4563 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4564 record_type != MBEDTLS_SSL_MSG_ALERT &&
4565 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4566 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4567 {
4568 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4569 }
4570
4571 return( 0 );
4572}
4573
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004574/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004575 * ContentType type;
4576 * ProtocolVersion version;
4577 * uint16 epoch; // DTLS only
4578 * uint48 sequence_number; // DTLS only
4579 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004580 *
4581 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004582 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004583 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4584 *
4585 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004586 * 1. proceed with the record if this function returns 0
4587 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4588 * 3. return CLIENT_RECONNECT if this function return that value
4589 * 4. drop the whole datagram if this function returns anything else.
4590 * Point 2 is needed when the peer is resending, and we have already received
4591 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004595 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004596 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004597
Hanno Becker8b09b732019-05-08 12:03:28 +01004598 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004599
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004601 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004602
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004603 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004604#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004605 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004606 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4607 ssl->conf->cid_len != 0 )
4608 {
4609 /* Shift pointers to account for record header including CID
4610 * struct {
4611 * ContentType special_type = tls12_cid;
4612 * ProtocolVersion version;
4613 * uint16 epoch;
4614 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004615 * opaque cid[cid_length]; // Additional field compared to
4616 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004617 * uint16 length;
4618 * opaque enc_content[DTLSCiphertext.length];
4619 * } DTLSCiphertext;
4620 */
4621
4622 /* So far, we only support static CID lengths
4623 * fixed in the configuration. */
4624 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4625 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4626 }
4627 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004628#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004629 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004632
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004633#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004634 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004635 * Section 4.1.2.7, that is, send alert only with TLS */
4636 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4637 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004638 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4639 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004640 }
4641#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004644 }
4645
4646 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004647 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 }
4652
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004653 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 }
4658
Hanno Becker8b09b732019-05-08 12:03:28 +01004659 /* Now that the total length of the record header is known, ensure
4660 * that the current datagram is large enough to hold it.
4661 * This would fail, for example, if we received a datagram of
4662 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4663 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4664 if( ret != 0 )
4665 {
4666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4667 return( ret );
4668 }
4669 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4670
4671 /* Parse and validate record length
4672 * This must happen after the CID parsing because
4673 * its position in the record header depends on
4674 * the presence of a CID. */
4675
4676 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004677 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004678 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4681 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004682 }
4683
Hanno Becker8b09b732019-05-08 12:03:28 +01004684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004685 "version = [%d:%d], msglen = %d",
4686 ssl->in_msgtype,
4687 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004688
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004689 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004690 * DTLS-related tests.
4691 * Check epoch before checking length constraint because
4692 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4693 * message gets duplicated before the corresponding Finished message,
4694 * the second ChangeCipherSpec should be discarded because it belongs
4695 * to an old epoch, but not because its length is shorter than
4696 * the minimum record length for packets using the new record transform.
4697 * Note that these two kinds of failures are handled differently,
4698 * as an unexpected record is silently skipped but an invalid
4699 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004700 */
4701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004702 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004703 {
4704 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4705
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004706 /* Check epoch (and sequence number) with DTLS */
4707 if( rec_epoch != ssl->in_epoch )
4708 {
4709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4710 "expected %d, received %d",
4711 ssl->in_epoch, rec_epoch ) );
4712
4713#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4714 /*
4715 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4716 * access the first byte of record content (handshake type), as we
4717 * have an active transform (possibly iv_len != 0), so use the
4718 * fact that the record header len is 13 instead.
4719 */
4720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4721 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4722 rec_epoch == 0 &&
4723 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4724 ssl->in_left > 13 &&
4725 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4726 {
4727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4728 "from the same port" ) );
4729 return( ssl_handle_possible_reconnect( ssl ) );
4730 }
4731 else
4732#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004733 {
4734 /* Consider buffering the record. */
4735 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4736 {
4737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4738 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4739 }
4740
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004741 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004742 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004743 }
4744
4745#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4746 /* Replay detection only works for the current epoch */
4747 if( rec_epoch == ssl->in_epoch &&
4748 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4751 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4752 }
4753#endif
4754 }
4755#endif /* MBEDTLS_SSL_PROTO_DTLS */
4756
Hanno Becker52c6dc62017-05-26 16:07:36 +01004757
4758 /* Check length against bounds of the current transform and version */
4759 if( ssl->transform_in == NULL )
4760 {
4761 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004762 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4765 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4766 }
4767 }
4768 else
4769 {
4770 if( ssl->in_msglen < ssl->transform_in->minlen )
4771 {
4772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4773 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4774 }
4775
4776#if defined(MBEDTLS_SSL_PROTO_SSL3)
4777 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004778 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004779 {
4780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4781 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4782 }
4783#endif
4784#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4785 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4786 /*
4787 * TLS encrypted messages can have up to 256 bytes of padding
4788 */
4789 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4790 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004791 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004792 {
4793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4794 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4795 }
4796#endif
4797 }
4798
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004799 return( 0 );
4800}
Paul Bakker5121ce52009-01-03 21:22:43 +00004801
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004802/*
4803 * If applicable, decrypt (and decompress) record content
4804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004806{
4807 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004810 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4813 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 ret = mbedtls_ssl_hw_record_read( ssl );
4818 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4821 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004822 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004823
4824 if( ret == 0 )
4825 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004828 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004830 mbedtls_record rec;
4831
4832 rec.buf = ssl->in_iv;
4833 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4834 - ( ssl->in_iv - ssl->in_buf );
4835 rec.data_len = ssl->in_msglen;
4836 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004837#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004838 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004839 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004840#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004841
4842 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4843 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4844 ssl->conf->transport, rec.ver );
4845 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004846 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4847 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004850
Hanno Beckera5a2b082019-05-15 14:03:01 +01004851#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004852 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4853 ssl->conf->ignore_unexpected_cid
4854 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4855 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004857 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004858 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004859#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004860
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 return( ret );
4862 }
4863
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004864 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004865 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004866 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4867 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004869
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004870 /* The record content type may change during decryption,
4871 * so re-read it. */
4872 ssl->in_msgtype = rec.type;
4873 /* Also update the input buffer, because unfortunately
4874 * the server-side ssl_parse_client_hello() reparses the
4875 * record header when receiving a ClientHello initiating
4876 * a renegotiation. */
4877 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004878 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004879 ssl->in_msglen = rec.data_len;
4880 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4881 ssl->in_len[1] = (unsigned char)( rec.data_len );
4882
Paul Bakker5121ce52009-01-03 21:22:43 +00004883 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4884 ssl->in_msg, ssl->in_msglen );
4885
Hanno Beckera5a2b082019-05-15 14:03:01 +01004886#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004887 /* We have already checked the record content type
4888 * in ssl_parse_record_header(), failing or silently
4889 * dropping the record in the case of an unknown type.
4890 *
4891 * Since with the use of CIDs, the record content type
4892 * might change during decryption, re-check the record
4893 * content type, but treat a failure as fatal this time. */
4894 if( ssl_check_record_type( ssl->in_msgtype ) )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4897 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4898 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004899#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004900
Angus Grattond8213d02016-05-25 20:56:48 +10004901 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004905 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004906 else if( ssl->in_msglen == 0 )
4907 {
4908#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4909 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4910 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4911 {
4912 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4914 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4915 }
4916#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4917
4918 ssl->nb_zero++;
4919
4920 /*
4921 * Three or more empty messages may be a DoS attack
4922 * (excessive CPU consumption).
4923 */
4924 if( ssl->nb_zero > 3 )
4925 {
4926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004927 "messages, possible DoS attack" ) );
4928 /* Treat the records as if they were not properly authenticated,
4929 * thereby failing the connection if we see more than allowed
4930 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004931 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4932 }
4933 }
4934 else
4935 ssl->nb_zero = 0;
4936
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004937 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4938#if defined(MBEDTLS_SSL_PROTO_TLS)
4939 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004940 {
4941 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004942 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004943 if( ++ssl->in_ctr[i - 1] != 0 )
4944 break;
4945
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004946 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004947 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004948 {
4949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4950 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4951 }
4952 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004953#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004954
Paul Bakker5121ce52009-01-03 21:22:43 +00004955 }
4956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004958 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004960 {
4961 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004964 return( ret );
4965 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004970 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004973 }
4974#endif
4975
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004976 return( 0 );
4977}
4978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004980
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004981/*
4982 * Read a record.
4983 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004984 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4985 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4986 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004987 */
Hanno Becker1097b342018-08-15 14:09:41 +01004988
4989/* Helper functions for mbedtls_ssl_read_record(). */
4990static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004991static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4992static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004993
Hanno Becker327c93b2018-08-15 13:56:18 +01004994int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004995 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004996{
4997 int ret;
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005000
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005001 if( ssl->keep_current_message == 0 )
5002 {
5003 do {
Simon Butcher99000142016-10-13 17:21:01 +01005004
Hanno Becker26994592018-08-15 14:14:59 +01005005 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005006 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005007 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005008
Hanno Beckere74d5562018-08-15 14:26:08 +01005009 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005010 {
Hanno Becker40f50842018-08-15 14:48:01 +01005011#if defined(MBEDTLS_SSL_PROTO_DTLS)
5012 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005013
Hanno Becker40f50842018-08-15 14:48:01 +01005014 /* We only check for buffered messages if the
5015 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005016 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005017 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005018 {
Hanno Becker40f50842018-08-15 14:48:01 +01005019 if( ssl_load_buffered_message( ssl ) == 0 )
5020 have_buffered = 1;
5021 }
5022
5023 if( have_buffered == 0 )
5024#endif /* MBEDTLS_SSL_PROTO_DTLS */
5025 {
5026 ret = ssl_get_next_record( ssl );
5027 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5028 continue;
5029
5030 if( ret != 0 )
5031 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005032 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005033 return( ret );
5034 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005035 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005036 }
5037
5038 ret = mbedtls_ssl_handle_message_type( ssl );
5039
Hanno Becker40f50842018-08-15 14:48:01 +01005040#if defined(MBEDTLS_SSL_PROTO_DTLS)
5041 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5042 {
5043 /* Buffer future message */
5044 ret = ssl_buffer_message( ssl );
5045 if( ret != 0 )
5046 return( ret );
5047
5048 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5049 }
5050#endif /* MBEDTLS_SSL_PROTO_DTLS */
5051
Hanno Becker90333da2017-10-10 11:27:13 +01005052 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5053 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005054
5055 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005056 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005057 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005058 return( ret );
5059 }
5060
Hanno Becker327c93b2018-08-15 13:56:18 +01005061 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005062 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005063 {
5064 mbedtls_ssl_update_handshake_status( ssl );
5065 }
Simon Butcher99000142016-10-13 17:21:01 +01005066 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005067 else
Simon Butcher99000142016-10-13 17:21:01 +01005068 {
Hanno Becker02f59072018-08-15 14:00:24 +01005069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005070 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005071 }
5072
5073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5074
5075 return( 0 );
5076}
5077
Hanno Becker40f50842018-08-15 14:48:01 +01005078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005079static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005080{
Hanno Becker40f50842018-08-15 14:48:01 +01005081 if( ssl->in_left > ssl->next_record_offset )
5082 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005083
Hanno Becker40f50842018-08-15 14:48:01 +01005084 return( 0 );
5085}
5086
5087static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5088{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005089 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005090 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005091 int ret = 0;
5092
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 if( hs == NULL )
5094 return( -1 );
5095
Hanno Beckere00ae372018-08-20 09:39:42 +01005096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5097
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005098 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5099 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5100 {
5101 /* Check if we have seen a ChangeCipherSpec before.
5102 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005103 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005104 {
5105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5106 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005107 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005108 }
5109
Hanno Becker39b8bc92018-08-28 17:17:13 +01005110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005111 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5112 ssl->in_msglen = 1;
5113 ssl->in_msg[0] = 1;
5114
5115 /* As long as they are equal, the exact value doesn't matter. */
5116 ssl->in_left = 0;
5117 ssl->next_record_offset = 0;
5118
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005119 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005120 goto exit;
5121 }
Hanno Becker37f95322018-08-16 13:55:32 +01005122
Hanno Beckerb8f50142018-08-28 10:01:34 +01005123#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005124 /* Debug only */
5125 {
5126 unsigned offset;
5127 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5128 {
5129 hs_buf = &hs->buffering.hs[offset];
5130 if( hs_buf->is_valid == 1 )
5131 {
5132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5133 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005134 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005135 }
5136 }
5137 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005138#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005139
5140 /* Check if we have buffered and/or fully reassembled the
5141 * next handshake message. */
5142 hs_buf = &hs->buffering.hs[0];
5143 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5144 {
5145 /* Synthesize a record containing the buffered HS message. */
5146 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5147 ( hs_buf->data[2] << 8 ) |
5148 hs_buf->data[3];
5149
5150 /* Double-check that we haven't accidentally buffered
5151 * a message that doesn't fit into the input buffer. */
5152 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5153 {
5154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5156 }
5157
5158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5159 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5160 hs_buf->data, msg_len + 12 );
5161
5162 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5163 ssl->in_hslen = msg_len + 12;
5164 ssl->in_msglen = msg_len + 12;
5165 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5166
5167 ret = 0;
5168 goto exit;
5169 }
5170 else
5171 {
5172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5173 hs->in_msg_seq ) );
5174 }
5175
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005176 ret = -1;
5177
5178exit:
5179
5180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5181 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005182}
5183
Hanno Beckera02b0b42018-08-21 17:20:27 +01005184static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5185 size_t desired )
5186{
5187 int offset;
5188 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5190 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191
Hanno Becker01315ea2018-08-21 17:22:17 +01005192 /* Get rid of future records epoch first, if such exist. */
5193 ssl_free_buffered_record( ssl );
5194
5195 /* Check if we have enough space available now. */
5196 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5197 hs->buffering.total_bytes_buffered ) )
5198 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005200 return( 0 );
5201 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005202
Hanno Becker4f432ad2018-08-28 10:02:32 +01005203 /* We don't have enough space to buffer the next expected handshake
5204 * message. Remove buffers used for future messages to gain space,
5205 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005206 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5207 offset >= 0; offset-- )
5208 {
5209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5210 offset ) );
5211
Hanno Beckerb309b922018-08-23 13:18:05 +01005212 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005213
5214 /* Check if we have enough space available now. */
5215 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5216 hs->buffering.total_bytes_buffered ) )
5217 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005219 return( 0 );
5220 }
5221 }
5222
5223 return( -1 );
5224}
5225
Hanno Becker40f50842018-08-15 14:48:01 +01005226static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5227{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005228 int ret = 0;
5229 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5230
5231 if( hs == NULL )
5232 return( 0 );
5233
5234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5235
5236 switch( ssl->in_msgtype )
5237 {
5238 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005240
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005241 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005242 break;
5243
5244 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005245 {
5246 unsigned recv_msg_seq_offset;
5247 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5248 mbedtls_ssl_hs_buffer *hs_buf;
5249 size_t msg_len = ssl->in_hslen - 12;
5250
5251 /* We should never receive an old handshake
5252 * message - double-check nonetheless. */
5253 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5254 {
5255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5257 }
5258
5259 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5260 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5261 {
5262 /* Silently ignore -- message too far in the future */
5263 MBEDTLS_SSL_DEBUG_MSG( 2,
5264 ( "Ignore future HS message with sequence number %u, "
5265 "buffering window %u - %u",
5266 recv_msg_seq, ssl->handshake->in_msg_seq,
5267 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5268
5269 goto exit;
5270 }
5271
5272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5273 recv_msg_seq, recv_msg_seq_offset ) );
5274
5275 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5276
5277 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005278 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005279 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005280 size_t reassembly_buf_sz;
5281
Hanno Becker37f95322018-08-16 13:55:32 +01005282 hs_buf->is_fragmented =
5283 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5284
5285 /* We copy the message back into the input buffer
5286 * after reassembly, so check that it's not too large.
5287 * This is an implementation-specific limitation
5288 * and not one from the standard, hence it is not
5289 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005290 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005291 {
5292 /* Ignore message */
5293 goto exit;
5294 }
5295
Hanno Beckere0b150f2018-08-21 15:51:03 +01005296 /* Check if we have enough space to buffer the message. */
5297 if( hs->buffering.total_bytes_buffered >
5298 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5299 {
5300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5302 }
5303
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005304 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5305 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005306
5307 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5308 hs->buffering.total_bytes_buffered ) )
5309 {
5310 if( recv_msg_seq_offset > 0 )
5311 {
5312 /* If we can't buffer a future message because
5313 * of space limitations -- ignore. */
5314 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",
5315 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5316 (unsigned) hs->buffering.total_bytes_buffered ) );
5317 goto exit;
5318 }
Hanno Beckere1801392018-08-21 16:51:05 +01005319 else
5320 {
5321 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",
5322 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5323 (unsigned) hs->buffering.total_bytes_buffered ) );
5324 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005325
Hanno Beckera02b0b42018-08-21 17:20:27 +01005326 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005327 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005328 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",
5329 (unsigned) msg_len,
5330 (unsigned) reassembly_buf_sz,
5331 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005332 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005333 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5334 goto exit;
5335 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005336 }
5337
5338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5339 msg_len ) );
5340
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005341 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5342 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005343 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005344 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005345 goto exit;
5346 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005347 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005348
5349 /* Prepare final header: copy msg_type, length and message_seq,
5350 * then add standardised fragment_offset and fragment_length */
5351 memcpy( hs_buf->data, ssl->in_msg, 6 );
5352 memset( hs_buf->data + 6, 0, 3 );
5353 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5354
5355 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005356
5357 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005358 }
5359 else
5360 {
5361 /* Make sure msg_type and length are consistent */
5362 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5363 {
5364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5365 /* Ignore */
5366 goto exit;
5367 }
5368 }
5369
Hanno Becker4422bbb2018-08-20 09:40:19 +01005370 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005371 {
5372 size_t frag_len, frag_off;
5373 unsigned char * const msg = hs_buf->data + 12;
5374
5375 /*
5376 * Check and copy current fragment
5377 */
5378
5379 /* Validation of header fields already done in
5380 * mbedtls_ssl_prepare_handshake_record(). */
5381 frag_off = ssl_get_hs_frag_off( ssl );
5382 frag_len = ssl_get_hs_frag_len( ssl );
5383
5384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5385 frag_off, frag_len ) );
5386 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5387
5388 if( hs_buf->is_fragmented )
5389 {
5390 unsigned char * const bitmask = msg + msg_len;
5391 ssl_bitmask_set( bitmask, frag_off, frag_len );
5392 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5393 msg_len ) == 0 );
5394 }
5395 else
5396 {
5397 hs_buf->is_complete = 1;
5398 }
5399
5400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5401 hs_buf->is_complete ? "" : "not yet " ) );
5402 }
5403
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005404 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005405 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005406
5407 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005408 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005409 break;
5410 }
5411
5412exit:
5413
5414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5415 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005416}
5417#endif /* MBEDTLS_SSL_PROTO_DTLS */
5418
Hanno Becker1097b342018-08-15 14:09:41 +01005419static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005420{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005421 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005422 * Consume last content-layer message and potentially
5423 * update in_msglen which keeps track of the contents'
5424 * consumption state.
5425 *
5426 * (1) Handshake messages:
5427 * Remove last handshake message, move content
5428 * and adapt in_msglen.
5429 *
5430 * (2) Alert messages:
5431 * Consume whole record content, in_msglen = 0.
5432 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005433 * (3) Change cipher spec:
5434 * Consume whole record content, in_msglen = 0.
5435 *
5436 * (4) Application data:
5437 * Don't do anything - the record layer provides
5438 * the application data as a stream transport
5439 * and consumes through mbedtls_ssl_read only.
5440 *
5441 */
5442
5443 /* Case (1): Handshake messages */
5444 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005445 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005446 /* Hard assertion to be sure that no application data
5447 * is in flight, as corrupting ssl->in_msglen during
5448 * ssl->in_offt != NULL is fatal. */
5449 if( ssl->in_offt != NULL )
5450 {
5451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5453 }
5454
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005455 /*
5456 * Get next Handshake message in the current record
5457 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005458
Hanno Becker4a810fb2017-05-24 16:27:30 +01005459 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005460 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005461 * current handshake content: If DTLS handshake
5462 * fragmentation is used, that's the fragment
5463 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005464 * size here is faulty and should be changed at
5465 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005466 * (2) While it doesn't seem to cause problems, one
5467 * has to be very careful not to assume that in_hslen
5468 * is always <= in_msglen in a sensible communication.
5469 * Again, it's wrong for DTLS handshake fragmentation.
5470 * The following check is therefore mandatory, and
5471 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005472 * Additionally, ssl->in_hslen might be arbitrarily out of
5473 * bounds after handling a DTLS message with an unexpected
5474 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005475 */
5476 if( ssl->in_hslen < ssl->in_msglen )
5477 {
5478 ssl->in_msglen -= ssl->in_hslen;
5479 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5480 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005481
Hanno Becker4a810fb2017-05-24 16:27:30 +01005482 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5483 ssl->in_msg, ssl->in_msglen );
5484 }
5485 else
5486 {
5487 ssl->in_msglen = 0;
5488 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005489
Hanno Becker4a810fb2017-05-24 16:27:30 +01005490 ssl->in_hslen = 0;
5491 }
5492 /* Case (4): Application data */
5493 else if( ssl->in_offt != NULL )
5494 {
5495 return( 0 );
5496 }
5497 /* Everything else (CCS & Alerts) */
5498 else
5499 {
5500 ssl->in_msglen = 0;
5501 }
5502
Hanno Becker1097b342018-08-15 14:09:41 +01005503 return( 0 );
5504}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005505
Hanno Beckere74d5562018-08-15 14:26:08 +01005506static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5507{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005508 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005509 return( 1 );
5510
5511 return( 0 );
5512}
5513
Hanno Becker5f066e72018-08-16 14:56:31 +01005514#if defined(MBEDTLS_SSL_PROTO_DTLS)
5515
5516static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5517{
5518 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5519 if( hs == NULL )
5520 return;
5521
Hanno Becker01315ea2018-08-21 17:22:17 +01005522 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005523 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005524 hs->buffering.total_bytes_buffered -=
5525 hs->buffering.future_record.len;
5526
5527 mbedtls_free( hs->buffering.future_record.data );
5528 hs->buffering.future_record.data = NULL;
5529 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005530}
5531
5532static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5533{
5534 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5535 unsigned char * rec;
5536 size_t rec_len;
5537 unsigned rec_epoch;
5538
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005539 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005540 return( 0 );
5541
5542 if( hs == NULL )
5543 return( 0 );
5544
Hanno Becker5f066e72018-08-16 14:56:31 +01005545 rec = hs->buffering.future_record.data;
5546 rec_len = hs->buffering.future_record.len;
5547 rec_epoch = hs->buffering.future_record.epoch;
5548
5549 if( rec == NULL )
5550 return( 0 );
5551
Hanno Becker4cb782d2018-08-20 11:19:05 +01005552 /* Only consider loading future records if the
5553 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005554 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005555 return( 0 );
5556
Hanno Becker5f066e72018-08-16 14:56:31 +01005557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5558
5559 if( rec_epoch != ssl->in_epoch )
5560 {
5561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5562 goto exit;
5563 }
5564
5565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5566
5567 /* Double-check that the record is not too large */
5568 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5569 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5573 }
5574
5575 memcpy( ssl->in_hdr, rec, rec_len );
5576 ssl->in_left = rec_len;
5577 ssl->next_record_offset = 0;
5578
5579 ssl_free_buffered_record( ssl );
5580
5581exit:
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5583 return( 0 );
5584}
5585
5586static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5587{
5588 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5589 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005590 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005591
5592 /* Don't buffer future records outside handshakes. */
5593 if( hs == NULL )
5594 return( 0 );
5595
5596 /* Only buffer handshake records (we are only interested
5597 * in Finished messages). */
5598 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5599 return( 0 );
5600
5601 /* Don't buffer more than one future epoch record. */
5602 if( hs->buffering.future_record.data != NULL )
5603 return( 0 );
5604
Hanno Becker01315ea2018-08-21 17:22:17 +01005605 /* Don't buffer record if there's not enough buffering space remaining. */
5606 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5607 hs->buffering.total_bytes_buffered ) )
5608 {
5609 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",
5610 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5611 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005612 return( 0 );
5613 }
5614
Hanno Becker5f066e72018-08-16 14:56:31 +01005615 /* Buffer record */
5616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5617 ssl->in_epoch + 1 ) );
5618 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5619 rec_hdr_len + ssl->in_msglen );
5620
5621 /* ssl_parse_record_header() only considers records
5622 * of the next epoch as candidates for buffering. */
5623 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005624 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005625
5626 hs->buffering.future_record.data =
5627 mbedtls_calloc( 1, hs->buffering.future_record.len );
5628 if( hs->buffering.future_record.data == NULL )
5629 {
5630 /* If we run out of RAM trying to buffer a
5631 * record from the next epoch, just ignore. */
5632 return( 0 );
5633 }
5634
Hanno Becker01315ea2018-08-21 17:22:17 +01005635 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005636
Hanno Becker01315ea2018-08-21 17:22:17 +01005637 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005638 return( 0 );
5639}
5640
5641#endif /* MBEDTLS_SSL_PROTO_DTLS */
5642
Hanno Beckere74d5562018-08-15 14:26:08 +01005643static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005644{
5645 int ret;
5646
Hanno Becker5f066e72018-08-16 14:56:31 +01005647#if defined(MBEDTLS_SSL_PROTO_DTLS)
5648 /* We might have buffered a future record; if so,
5649 * and if the epoch matches now, load it.
5650 * On success, this call will set ssl->in_left to
5651 * the length of the buffered record, so that
5652 * the calls to ssl_fetch_input() below will
5653 * essentially be no-ops. */
5654 ret = ssl_load_buffered_record( ssl );
5655 if( ret != 0 )
5656 return( ret );
5657#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005658
Hanno Becker8b09b732019-05-08 12:03:28 +01005659 /* Reset in pointers to default state for TLS/DTLS records,
5660 * assuming no CID and no offset between record content and
5661 * record plaintext. */
5662 ssl_update_in_pointers( ssl );
5663
5664 /* Ensure that we have enough space available for the default form
5665 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5666 * with no space for CIDs counted in). */
5667 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5668 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005671 return( ret );
5672 }
5673
5674 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005677 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005678 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005679 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005680 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5681 {
5682 ret = ssl_buffer_future_record( ssl );
5683 if( ret != 0 )
5684 return( ret );
5685
5686 /* Fall through to handling of unexpected records */
5687 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5688 }
5689
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005690 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5691 {
5692 /* Skip unexpected record (but not whole datagram) */
5693 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005694 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005695
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5697 "(header)" ) );
5698 }
5699 else
5700 {
5701 /* Skip invalid record and the rest of the datagram */
5702 ssl->next_record_offset = 0;
5703 ssl->in_left = 0;
5704
5705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5706 "(header)" ) );
5707 }
5708
5709 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005710 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005711 }
5712#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005713 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005714 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005715
5716 /*
5717 * Read and optionally decrypt the message contents
5718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005720 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005723 return( ret );
5724 }
5725
5726 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005728 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005729 {
Hanno Becker43395762019-05-03 14:46:38 +01005730 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005731 if( ssl->next_record_offset < ssl->in_left )
5732 {
5733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5734 }
5735 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005736 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005737#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005738#if defined(MBEDTLS_SSL_PROTO_TLS)
5739 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005740 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005741 }
5742#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005743
5744 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005747 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005748 {
5749 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005750 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005751 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005752 /* Except when waiting for Finished as a bad mac here
5753 * probably means something went wrong in the handshake
5754 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5755 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5756 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5757 {
5758#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5759 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5760 {
5761 mbedtls_ssl_send_alert_message( ssl,
5762 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5763 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5764 }
5765#endif
5766 return( ret );
5767 }
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005770 if( ssl->conf->badmac_limit != 0 &&
5771 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5774 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005775 }
5776#endif
5777
Hanno Becker4a810fb2017-05-24 16:27:30 +01005778 /* As above, invalid records cause
5779 * dismissal of the whole datagram. */
5780
5781 ssl->next_record_offset = 0;
5782 ssl->in_left = 0;
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005785 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005786 }
5787
5788 return( ret );
5789 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005790 MBEDTLS_SSL_TRANSPORT_ELSE
5791#endif /* MBEDTLS_SSL_PROTO_DTLS */
5792#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005793 {
5794 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5796 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 mbedtls_ssl_send_alert_message( ssl,
5799 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5800 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005801 }
5802#endif
5803 return( ret );
5804 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005805#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005806 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005807
Simon Butcher99000142016-10-13 17:21:01 +01005808 return( 0 );
5809}
5810
5811int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5812{
5813 int ret;
5814
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005815 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005816 * Handle particular types of records
5817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 {
Simon Butcher99000142016-10-13 17:21:01 +01005820 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5821 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005822 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 }
5825
Hanno Beckere678eaa2018-08-21 14:57:46 +01005826 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005827 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005828 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005829 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5831 ssl->in_msglen ) );
5832 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005833 }
5834
Hanno Beckere678eaa2018-08-21 14:57:46 +01005835 if( ssl->in_msg[0] != 1 )
5836 {
5837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5838 ssl->in_msg[0] ) );
5839 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5840 }
5841
5842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005843 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005844 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5845 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5846 {
5847 if( ssl->handshake == NULL )
5848 {
5849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5850 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5851 }
5852
5853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5854 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5855 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005856#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005857 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005861 if( ssl->in_msglen != 2 )
5862 {
5863 /* Note: Standard allows for more than one 2 byte alert
5864 to be packed in a single message, but Mbed TLS doesn't
5865 currently support this. */
5866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5867 ssl->in_msglen ) );
5868 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5869 }
5870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005872 ssl->in_msg[0], ssl->in_msg[1] ) );
5873
5874 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005875 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005880 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 }
5883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5885 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5888 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005890
5891#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5892 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5893 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5894 {
Hanno Becker90333da2017-10-10 11:27:13 +01005895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005896 /* Will be handled when trying to parse ServerHello */
5897 return( 0 );
5898 }
5899#endif
5900
5901#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5902 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5903 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5904 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5905 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5906 {
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5908 /* Will be handled in mbedtls_ssl_parse_certificate() */
5909 return( 0 );
5910 }
5911#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5912
5913 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005914 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005915 }
5916
Hanno Beckerc76c6192017-06-06 10:03:17 +01005917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005918 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005919 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005920 /* Drop unexpected ApplicationData records,
5921 * except at the beginning of renegotiations */
5922 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5923 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5924#if defined(MBEDTLS_SSL_RENEGOTIATION)
5925 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5926 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005927#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005928 )
5929 {
5930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5931 return( MBEDTLS_ERR_SSL_NON_FATAL );
5932 }
5933
5934 if( ssl->handshake != NULL &&
5935 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5936 {
5937 ssl_handshake_wrapup_free_hs_transform( ssl );
5938 }
5939 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005940#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005941
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 return( 0 );
5943}
5944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005946{
5947 int ret;
5948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5950 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5951 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005952 {
5953 return( ret );
5954 }
5955
5956 return( 0 );
5957}
5958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005960 unsigned char level,
5961 unsigned char message )
5962{
5963 int ret;
5964
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005965 if( ssl == NULL || ssl->conf == NULL )
5966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005972 ssl->out_msglen = 2;
5973 ssl->out_msg[0] = level;
5974 ssl->out_msg[1] = message;
5975
Hanno Becker67bc7c32018-08-06 11:33:50 +01005976 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005979 return( ret );
5980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005982
5983 return( 0 );
5984}
5985
Paul Bakker5121ce52009-01-03 21:22:43 +00005986/*
5987 * Handshake functions
5988 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00005989#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005990/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005992{
Hanno Becker8759e162017-12-27 21:34:08 +00005993 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005996
Hanno Becker5097cba2019-02-05 13:36:46 +00005997 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006000 ssl->state++;
6001 return( 0 );
6002 }
6003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6005 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006006}
6007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006009{
Hanno Becker8759e162017-12-27 21:34:08 +00006010 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006013
Hanno Becker5097cba2019-02-05 13:36:46 +00006014 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006017 ssl->state++;
6018 return( 0 );
6019 }
6020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006023}
Gilles Peskinef9828522017-05-03 12:28:43 +02006024
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006025#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006026/* Some certificate support -> implement write and parse */
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006031 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006033 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006036
Hanno Becker5097cba2019-02-05 13:36:46 +00006037 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
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 Beckerdf759382019-02-05 17:02:46 +00006142
6143#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006144static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6145 unsigned char *crt_buf,
6146 size_t crt_buf_len )
6147{
6148 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6149
6150 if( peer_crt == NULL )
6151 return( -1 );
6152
6153 if( peer_crt->raw.len != crt_buf_len )
6154 return( -1 );
6155
Hanno Becker68b856d2019-02-08 14:00:04 +00006156 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006157}
Hanno Beckerdf759382019-02-05 17:02:46 +00006158#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6159static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6160 unsigned char *crt_buf,
6161 size_t crt_buf_len )
6162{
6163 int ret;
6164 unsigned char const * const peer_cert_digest =
6165 ssl->session->peer_cert_digest;
6166 mbedtls_md_type_t const peer_cert_digest_type =
6167 ssl->session->peer_cert_digest_type;
6168 mbedtls_md_info_t const * const digest_info =
6169 mbedtls_md_info_from_type( peer_cert_digest_type );
6170 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6171 size_t digest_len;
6172
6173 if( peer_cert_digest == NULL || digest_info == NULL )
6174 return( -1 );
6175
6176 digest_len = mbedtls_md_get_size( digest_info );
6177 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6178 return( -1 );
6179
6180 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6181 if( ret != 0 )
6182 return( -1 );
6183
6184 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6185}
6186#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006187#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006188
Hanno Becker22141592019-02-05 12:38:15 +00006189static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6190{
6191 if( session->peer_cert != NULL )
6192 {
6193 mbedtls_x509_crt_free( session->peer_cert );
6194 mbedtls_free( session->peer_cert );
6195 session->peer_cert = NULL;
6196 }
Hanno Becker9fb6e2e2019-02-05 17:00:50 +00006197
6198#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6199 if( session->peer_cert_digest != NULL )
6200 {
6201 /* Zeroization is not necessary. */
6202 mbedtls_free( session->peer_cert_digest );
6203 session->peer_cert_digest = NULL;
6204 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6205 session->peer_cert_digest_len = 0;
6206 }
6207#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker22141592019-02-05 12:38:15 +00006208}
6209
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006210/*
6211 * Once the certificate message is read, parse it into a cert chain and
6212 * perform basic checks, but leave actual verification to the caller
6213 */
Hanno Becker35e41772019-02-05 15:37:23 +00006214static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6215 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006216{
Hanno Becker35e41772019-02-05 15:37:23 +00006217 int ret;
6218#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6219 int crt_cnt=0;
6220#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006221 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006222 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006227 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6228 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 }
6231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6233 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006236 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6237 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006239 }
6240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006242
Paul Bakker5121ce52009-01-03 21:22:43 +00006243 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006245 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006246 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006247
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006248 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006252 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6253 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 }
6256
Hanno Becker33c3dc82019-01-30 14:46:46 +00006257 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6258 i += 3;
6259
Hanno Becker33c3dc82019-01-30 14:46:46 +00006260 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006261 while( i < ssl->in_hslen )
6262 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006263 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006264 if ( i + 3 > ssl->in_hslen ) {
6265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006266 mbedtls_ssl_send_alert_message( ssl,
6267 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6268 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006269 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6270 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006271 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6272 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006273 if( ssl->in_msg[i] != 0 )
6274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006276 mbedtls_ssl_send_alert_message( ssl,
6277 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6278 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006280 }
6281
Hanno Becker33c3dc82019-01-30 14:46:46 +00006282 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006283 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6284 | (unsigned int) ssl->in_msg[i + 2];
6285 i += 3;
6286
6287 if( n < 128 || i + n > ssl->in_hslen )
6288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006290 mbedtls_ssl_send_alert_message( ssl,
6291 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6292 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006294 }
6295
Hanno Becker33c3dc82019-01-30 14:46:46 +00006296 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006297#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6298 if( crt_cnt++ == 0 &&
6299 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6300 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006301 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006302 /* During client-side renegotiation, check that the server's
6303 * end-CRTs hasn't changed compared to the initial handshake,
6304 * mitigating the triple handshake attack. On success, reuse
6305 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6307 if( ssl_check_peer_crt_unchanged( ssl,
6308 &ssl->in_msg[i],
6309 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006310 {
Hanno Becker35e41772019-02-05 15:37:23 +00006311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6312 mbedtls_ssl_send_alert_message( ssl,
6313 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6314 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6315 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006316 }
Hanno Becker35e41772019-02-05 15:37:23 +00006317
6318 /* Now we can safely free the original chain. */
6319 ssl_clear_peer_cert( ssl->session );
6320 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006321#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6322
Hanno Becker33c3dc82019-01-30 14:46:46 +00006323 /* Parse the next certificate in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006324 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006325 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006326 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006327 case 0: /*ok*/
6328 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6329 /* Ignore certificate with an unknown algorithm: maybe a
6330 prior certificate was already trusted. */
6331 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006332
Hanno Becker33c3dc82019-01-30 14:46:46 +00006333 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6334 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6335 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006336
Hanno Becker33c3dc82019-01-30 14:46:46 +00006337 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6338 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6339 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006340
Hanno Becker33c3dc82019-01-30 14:46:46 +00006341 default:
6342 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6343 crt_parse_der_failed:
6344 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6345 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6346 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 }
6348
6349 i += n;
6350 }
6351
Hanno Becker35e41772019-02-05 15:37:23 +00006352 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006353 return( 0 );
6354}
6355
Hanno Beckerb8a08572019-02-05 12:49:06 +00006356#if defined(MBEDTLS_SSL_SRV_C)
6357static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6358{
6359 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6360 return( -1 );
6361
6362#if defined(MBEDTLS_SSL_PROTO_SSL3)
6363 /*
6364 * Check if the client sent an empty certificate
6365 */
6366 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6367 {
6368 if( ssl->in_msglen == 2 &&
6369 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6370 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6371 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6372 {
6373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6374 return( 0 );
6375 }
6376
6377 return( -1 );
6378 }
6379#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6380
6381#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6382 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6383 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6384 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6385 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6386 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6387 {
6388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6389 return( 0 );
6390 }
6391
6392 return( -1 );
6393#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6394 MBEDTLS_SSL_PROTO_TLS1_2 */
6395}
6396#endif /* MBEDTLS_SSL_SRV_C */
6397
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006398/* Check if a certificate message is expected.
6399 * Return either
6400 * - SSL_CERTIFICATE_EXPECTED, or
6401 * - SSL_CERTIFICATE_SKIP
6402 * indicating whether a Certificate message is expected or not.
6403 */
6404#define SSL_CERTIFICATE_EXPECTED 0
6405#define SSL_CERTIFICATE_SKIP 1
6406static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6407 int authmode )
6408{
6409 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6410 ssl->handshake->ciphersuite_info;
6411
6412 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6413 return( SSL_CERTIFICATE_SKIP );
6414
6415#if defined(MBEDTLS_SSL_SRV_C)
6416 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6417 {
6418 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6419 return( SSL_CERTIFICATE_SKIP );
6420
6421 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6422 {
6423 /* NOTE: Is it intentional that we set verify_result
6424 * to SKIP_VERIFY on server-side only? */
6425 ssl->session_negotiate->verify_result =
6426 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6427 return( SSL_CERTIFICATE_SKIP );
6428 }
6429 }
6430#endif /* MBEDTLS_SSL_SRV_C */
6431
6432 return( SSL_CERTIFICATE_EXPECTED );
6433}
6434
Hanno Becker3cf50612019-02-05 14:36:34 +00006435static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6436 int authmode,
6437 mbedtls_x509_crt *chain,
6438 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006439{
Hanno Becker613d4902019-02-05 13:11:17 +00006440 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006441 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6442 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006443 mbedtls_x509_crt *ca_chain;
6444 mbedtls_x509_crl *ca_crl;
6445
6446 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6447 return( 0 );
6448
6449#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6450 if( ssl->handshake->sni_ca_chain != NULL )
6451 {
6452 ca_chain = ssl->handshake->sni_ca_chain;
6453 ca_crl = ssl->handshake->sni_ca_crl;
6454 }
6455 else
6456#endif
6457 {
6458 ca_chain = ssl->conf->ca_chain;
6459 ca_crl = ssl->conf->ca_crl;
6460 }
6461
6462 /*
6463 * Main check: verify certificate
6464 */
6465 ret = mbedtls_x509_crt_verify_restartable(
6466 chain,
6467 ca_chain, ca_crl,
6468 ssl->conf->cert_profile,
6469 ssl->hostname,
6470 &ssl->session_negotiate->verify_result,
6471 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6472
6473 if( ret != 0 )
6474 {
6475 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6476 }
6477
6478#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6479 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6480 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6481#endif
6482
6483 /*
6484 * Secondary checks: always done, but change 'ret' only if it was 0
6485 */
6486
6487#if defined(MBEDTLS_ECP_C)
6488 {
6489 const mbedtls_pk_context *pk = &chain->pk;
6490
6491 /* If certificate uses an EC key, make sure the curve is OK */
6492 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6493 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6494 {
6495 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6496
6497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6498 if( ret == 0 )
6499 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6500 }
6501 }
6502#endif /* MBEDTLS_ECP_C */
6503
6504 if( mbedtls_ssl_check_cert_usage( chain,
6505 ciphersuite_info,
6506 ! ssl->conf->endpoint,
6507 &ssl->session_negotiate->verify_result ) != 0 )
6508 {
6509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6510 if( ret == 0 )
6511 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6512 }
6513
6514 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6515 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6516 * with details encoded in the verification flags. All other kinds
6517 * of error codes, including those from the user provided f_vrfy
6518 * functions, are treated as fatal and lead to a failure of
6519 * ssl_parse_certificate even if verification was optional. */
6520 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6521 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6522 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6523 {
6524 ret = 0;
6525 }
6526
6527 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6528 {
6529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6530 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6531 }
6532
6533 if( ret != 0 )
6534 {
6535 uint8_t alert;
6536
6537 /* The certificate may have been rejected for several reasons.
6538 Pick one and send the corresponding alert. Which alert to send
6539 may be a subject of debate in some cases. */
6540 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6541 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6542 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6543 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6544 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6545 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6546 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6547 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6548 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6549 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6550 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6551 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6552 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6553 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6554 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6555 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6556 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6557 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6558 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6559 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6560 else
6561 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6562 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6563 alert );
6564 }
6565
6566#if defined(MBEDTLS_DEBUG_C)
6567 if( ssl->session_negotiate->verify_result != 0 )
6568 {
6569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6570 ssl->session_negotiate->verify_result ) );
6571 }
6572 else
6573 {
6574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6575 }
6576#endif /* MBEDTLS_DEBUG_C */
6577
6578 return( ret );
6579}
6580
6581int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6582{
6583 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006584 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006585#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6586 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6587 ? ssl->handshake->sni_authmode
6588 : ssl->conf->authmode;
6589#else
6590 const int authmode = ssl->conf->authmode;
6591#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006592 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006593 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006594
6595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6596
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006597 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6598 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006599 {
6600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006601 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006602 }
6603
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006604#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6605 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006606 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006607 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006608 chain = ssl->handshake->ecrs_peer_cert;
6609 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006610 goto crt_verify;
6611 }
6612#endif
6613
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006614 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006615 {
6616 /* mbedtls_ssl_read_record may have sent an alert already. We
6617 let it decide whether to alert. */
6618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006619 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006620 }
6621
Hanno Beckerb8a08572019-02-05 12:49:06 +00006622#if defined(MBEDTLS_SSL_SRV_C)
6623 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6624 {
6625 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006626
6627 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006628 ret = 0;
6629 else
6630 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006631
Hanno Becker613d4902019-02-05 13:11:17 +00006632 goto exit;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006633 }
6634#endif /* MBEDTLS_SSL_SRV_C */
6635
Hanno Becker35e41772019-02-05 15:37:23 +00006636 /* Clear existing peer CRT structure in case we tried to
6637 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006638 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006639
6640 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6641 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006642 {
6643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6644 sizeof( mbedtls_x509_crt ) ) );
6645 mbedtls_ssl_send_alert_message( ssl,
6646 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6647 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006648
Hanno Beckere4aeb762019-02-05 17:19:52 +00006649 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6650 goto exit;
6651 }
6652 mbedtls_x509_crt_init( chain );
6653
6654 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006655 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006656 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006657
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006658#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6659 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006660 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006661
6662crt_verify:
6663 if( ssl->handshake->ecrs_enabled)
6664 rs_ctx = &ssl->handshake->ecrs_ctx;
6665#endif
6666
Hanno Becker3cf50612019-02-05 14:36:34 +00006667 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006668 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006669 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006670 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
Hanno Becker3008d282019-02-05 17:02:28 +00006672#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckere4aeb762019-02-05 17:19:52 +00006673
Hanno Becker3008d282019-02-05 17:02:28 +00006674 /* Remember digest of the peer's end-CRT. */
6675 ssl->session_negotiate->peer_cert_digest =
6676 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6677 if( ssl->session_negotiate->peer_cert_digest == NULL )
6678 {
6679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6680 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6681 mbedtls_ssl_send_alert_message( ssl,
6682 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6683 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006684
6685 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6686 goto exit;
Hanno Becker3008d282019-02-05 17:02:28 +00006687 }
6688 ret = mbedtls_md( mbedtls_md_info_from_type(
Hanno Beckere4aeb762019-02-05 17:19:52 +00006689 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6690 chain->raw.p, chain->raw.len,
Hanno Becker3008d282019-02-05 17:02:28 +00006691 ssl->session_negotiate->peer_cert_digest );
6692 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006693 goto exit;
Hanno Becker3008d282019-02-05 17:02:28 +00006694
6695 ssl->session_negotiate->peer_cert_digest_type =
6696 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6697 ssl->session_negotiate->peer_cert_digest_len =
6698 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6699#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6700
Hanno Beckere4aeb762019-02-05 17:19:52 +00006701 ssl->session_negotiate->peer_cert = chain;
6702 chain = NULL;
6703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006705
Hanno Becker613d4902019-02-05 13:11:17 +00006706exit:
6707
Hanno Beckere4aeb762019-02-05 17:19:52 +00006708 if( ret == 0 )
6709 ssl->state++;
6710
6711#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6712 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6713 {
6714 ssl->handshake->ecrs_peer_cert = chain;
6715 chain = NULL;
6716 }
6717#endif
6718
6719 if( chain != NULL )
6720 {
6721 mbedtls_x509_crt_free( chain );
6722 mbedtls_free( chain );
6723 }
6724
Paul Bakker5121ce52009-01-03 21:22:43 +00006725 return( ret );
6726}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006727#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006730{
6731 int ret;
6732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006736 ssl->out_msglen = 1;
6737 ssl->out_msg[0] = 1;
6738
Paul Bakker5121ce52009-01-03 21:22:43 +00006739 ssl->state++;
6740
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006741 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006742 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006744 return( ret );
6745 }
6746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006748
6749 return( 0 );
6750}
6751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006753{
6754 int ret;
6755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006757
Hanno Becker327c93b2018-08-15 13:56:18 +01006758 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006761 return( ret );
6762 }
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006767 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6768 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006770 }
6771
Hanno Beckere678eaa2018-08-21 14:57:46 +01006772 /* CCS records are only accepted if they have length 1 and content '1',
6773 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006774
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006775 /*
6776 * Switch to our negotiated transform and session parameters for inbound
6777 * data.
6778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006780 ssl->transform_in = ssl->transform_negotiate;
6781 ssl->session_in = ssl->session_negotiate;
6782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006784 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006787 ssl_dtls_replay_reset( ssl );
6788#endif
6789
6790 /* Increment epoch */
6791 if( ++ssl->in_epoch == 0 )
6792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006794 /* This is highly unlikely to happen for legitimate reasons, so
6795 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006797 }
6798 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006799 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006801#if defined(MBEDTLS_SSL_PROTO_TLS)
6802 {
6803 memset( ssl->in_ctr, 0, 8 );
6804 }
6805#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006806
Hanno Beckerf5970a02019-05-08 09:38:41 +01006807 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6810 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006815 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6816 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006818 }
6819 }
6820#endif
6821
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 ssl->state++;
6823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006825
6826 return( 0 );
6827}
6828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6830 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006831{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006832 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6835 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6836 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006837 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006838 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006839#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6841#if defined(MBEDTLS_SHA512_C)
6842 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006843 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6844 else
6845#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846#if defined(MBEDTLS_SHA256_C)
6847 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006848 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006849 else
6850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006854 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006855 }
Paul Bakker380da532012-04-18 16:10:25 +00006856}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6861 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006862 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6863 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6866#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006867 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006868#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006870 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006873}
6874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006876 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006877{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6879 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006880 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6881 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6884#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006885 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006888 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006891}
6892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6894 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6895static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006896 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006897{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006898 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6899 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006900}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006901#endif
Paul Bakker380da532012-04-18 16:10:25 +00006902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6904#if defined(MBEDTLS_SHA256_C)
6905static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006906 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006907{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006908 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006909}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006910#endif
Paul Bakker380da532012-04-18 16:10:25 +00006911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if defined(MBEDTLS_SHA512_C)
6913static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006914 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006915{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006916 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006917}
Paul Bakker769075d2012-11-24 11:26:46 +01006918#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006922static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006924{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006925 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006926 mbedtls_md5_context md5;
6927 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 unsigned char padbuf[48];
6930 unsigned char md5sum[16];
6931 unsigned char sha1sum[20];
6932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006934 if( !session )
6935 session = ssl->session;
6936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006938
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006939 mbedtls_md5_init( &md5 );
6940 mbedtls_sha1_init( &sha1 );
6941
6942 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6943 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006944
6945 /*
6946 * SSLv3:
6947 * hash =
6948 * MD5( master + pad2 +
6949 * MD5( handshake + sender + master + pad1 ) )
6950 * + SHA1( master + pad2 +
6951 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006952 */
6953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006955 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6956 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006957#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006960 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6961 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006962#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006965 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006966
Paul Bakker1ef83d62012-04-11 12:09:53 +00006967 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006968
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006969 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6970 mbedtls_md5_update_ret( &md5, session->master, 48 );
6971 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6972 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006973
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006974 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6975 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6976 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6977 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006978
Paul Bakker1ef83d62012-04-11 12:09:53 +00006979 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006980
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006981 mbedtls_md5_starts_ret( &md5 );
6982 mbedtls_md5_update_ret( &md5, session->master, 48 );
6983 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6984 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6985 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006986
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006987 mbedtls_sha1_starts_ret( &sha1 );
6988 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6989 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6990 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6991 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006994
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006995 mbedtls_md5_free( &md5 );
6996 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006997
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006998 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6999 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7000 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007007static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007009{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007010 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007011 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007012 mbedtls_md5_context md5;
7013 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007014 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007017 if( !session )
7018 session = ssl->session;
7019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007021
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007022 mbedtls_md5_init( &md5 );
7023 mbedtls_sha1_init( &sha1 );
7024
7025 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7026 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007027
Paul Bakker1ef83d62012-04-11 12:09:53 +00007028 /*
7029 * TLSv1:
7030 * hash = PRF( master, finished_label,
7031 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7032 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007035 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7036 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007037#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007040 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7041 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007042#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007045 ? "client finished"
7046 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007047
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007048 mbedtls_md5_finish_ret( &md5, padbuf );
7049 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007050
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007051 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007052 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007055
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007056 mbedtls_md5_free( &md5 );
7057 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007058
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007059 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7066#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007067static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007069{
7070 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007071 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007072 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007073 unsigned char padbuf[32];
7074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007076 if( !session )
7077 session = ssl->session;
7078
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007079 mbedtls_sha256_init( &sha256 );
7080
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007082
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007083 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007084
7085 /*
7086 * TLSv1.2:
7087 * hash = PRF( master, finished_label,
7088 * Hash( handshake ) )[0.11]
7089 */
7090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091#if !defined(MBEDTLS_SHA256_ALT)
7092 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007093 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007094#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007097 ? "client finished"
7098 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007099
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007100 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007101
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007102 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007103 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007106
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007107 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007108
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007109 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007116static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007118{
7119 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007120 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007121 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007122 unsigned char padbuf[48];
7123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007125 if( !session )
7126 session = ssl->session;
7127
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007128 mbedtls_sha512_init( &sha512 );
7129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007131
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007132 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007133
7134 /*
7135 * TLSv1.2:
7136 * hash = PRF( master, finished_label,
7137 * Hash( handshake ) )[0.11]
7138 */
7139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007141 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7142 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007143#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007146 ? "client finished"
7147 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007148
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007149 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007150
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007151 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007152 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007155
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007156 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007157
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007158 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#endif /* MBEDTLS_SHA512_C */
7163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007168
7169 /*
7170 * Free our handshake params
7171 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007172 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007174 ssl->handshake = NULL;
7175
7176 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007177 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007178 */
7179 if( ssl->transform )
7180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 mbedtls_ssl_transform_free( ssl->transform );
7182 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007183 }
7184 ssl->transform = ssl->transform_negotiate;
7185 ssl->transform_negotiate = NULL;
7186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007188}
7189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007191{
7192 int resume = ssl->handshake->resume;
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196#if defined(MBEDTLS_SSL_RENEGOTIATION)
7197 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007200 ssl->renego_records_seen = 0;
7201 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007202#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007203
7204 /*
7205 * Free the previous session and switch in the current one
7206 */
Paul Bakker0a597072012-09-25 21:55:46 +00007207 if( ssl->session )
7208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007210 /* RFC 7366 3.1: keep the EtM state */
7211 ssl->session_negotiate->encrypt_then_mac =
7212 ssl->session->encrypt_then_mac;
7213#endif
7214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215 mbedtls_ssl_session_free( ssl->session );
7216 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007217 }
7218 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007219 ssl->session_negotiate = NULL;
7220
Paul Bakker0a597072012-09-25 21:55:46 +00007221 /*
7222 * Add cache entry
7223 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007224 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007225 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007226 resume == 0 )
7227 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007228 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007230 }
Paul Bakker0a597072012-09-25 21:55:46 +00007231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007233 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007234 ssl->handshake->flight != NULL )
7235 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007236 /* Cancel handshake timer */
7237 ssl_set_timer( ssl, 0 );
7238
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007239 /* Keep last flight around in case we need to resend it:
7240 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007242 }
7243 else
7244#endif
7245 ssl_handshake_wrapup_free_hs_transform( ssl );
7246
Paul Bakker48916f92012-09-16 19:57:18 +00007247 ssl->state++;
7248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007250}
7251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007253{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007254 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007257
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007258 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007259
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007260 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007261
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007262 /*
7263 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7264 * may define some other value. Currently (early 2016), no defined
7265 * ciphersuite does this (and this is unlikely to change as activity has
7266 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007271 ssl->verify_data_len = hash_len;
7272 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007273#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007274
Paul Bakker5121ce52009-01-03 21:22:43 +00007275 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7277 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
7279 /*
7280 * In case of session resuming, invert the client and server
7281 * ChangeCipherSpec messages order.
7282 */
Paul Bakker0a597072012-09-25 21:55:46 +00007283 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007286 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007288#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007290 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007292#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007293 }
7294 else
7295 ssl->state++;
7296
Paul Bakker48916f92012-09-16 19:57:18 +00007297 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007298 * Switch to our negotiated transform and session parameters for outbound
7299 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007304 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007305 {
7306 unsigned char i;
7307
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007308 /* Remember current epoch settings for resending */
7309 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007310 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007311
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007312 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007313 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007314
7315 /* Increment epoch */
7316 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007317 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007318 break;
7319
7320 /* The loop goes to its end iff the counter is wrapping */
7321 if( i == 0 )
7322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7324 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007325 }
7326 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007327 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007329#if defined(MBEDTLS_SSL_PROTO_TLS)
7330 {
7331 memset( ssl->cur_out_ctr, 0, 8 );
7332 }
7333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007334
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007335 ssl->transform_out = ssl->transform_negotiate;
7336 ssl->session_out = ssl->session_negotiate;
7337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7339 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7344 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007345 }
7346 }
7347#endif
7348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007350 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007352#endif
7353
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007354 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007355 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007357 return( ret );
7358 }
7359
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007360#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007361 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007362 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7363 {
7364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7365 return( ret );
7366 }
7367#endif
7368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007370
7371 return( 0 );
7372}
7373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007375#define SSL_MAX_HASH_LEN 36
7376#else
7377#define SSL_MAX_HASH_LEN 12
7378#endif
7379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007381{
Paul Bakker23986e52011-04-24 08:57:21 +00007382 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007383 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007384 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007387
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007388 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007389
Hanno Becker327c93b2018-08-15 13:56:18 +01007390 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007393 return( ret );
7394 }
7395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007399 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7400 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007402 }
7403
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007404 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#if defined(MBEDTLS_SSL_PROTO_SSL3)
7406 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007407 hash_len = 36;
7408 else
7409#endif
7410 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7413 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007416 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7417 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007419 }
7420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007422 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007425 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7426 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007428 }
7429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007431 ssl->verify_data_len = hash_len;
7432 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007433#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007434
Paul Bakker0a597072012-09-25 21:55:46 +00007435 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007438 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007440#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007442 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007444#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007445 }
7446 else
7447 ssl->state++;
7448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007450 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007452#endif
7453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007455
7456 return( 0 );
7457}
7458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7464 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7465 mbedtls_md5_init( &handshake->fin_md5 );
7466 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007467 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7468 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007469#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7471#if defined(MBEDTLS_SHA256_C)
7472 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007473 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475#if defined(MBEDTLS_SHA512_C)
7476 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007477 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007480
7481 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007482
7483#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7484 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7485 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7486#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#if defined(MBEDTLS_DHM_C)
7489 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491#if defined(MBEDTLS_ECDH_C)
7492 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007493#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007494#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007495 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007496#if defined(MBEDTLS_SSL_CLI_C)
7497 handshake->ecjpake_cache = NULL;
7498 handshake->ecjpake_cache_len = 0;
7499#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007500#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007501
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007502#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007503 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007504#endif
7505
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007506#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7507 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7508#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007509}
7510
Hanno Becker611a83b2018-01-03 14:27:32 +00007511void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007512{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7516 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007517
Hanno Becker92231322018-01-03 15:32:51 +00007518#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 mbedtls_md_init( &transform->md_ctx_enc );
7520 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007521#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007522}
7523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007527}
7528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007530{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007531 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007532 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007534 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007536 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007537 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007538
7539 /*
7540 * Either the pointers are now NULL or cleared properly and can be freed.
7541 * Now allocate missing structures.
7542 */
7543 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007544 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007545 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007546 }
Paul Bakker48916f92012-09-16 19:57:18 +00007547
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007548 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007549 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007550 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007551 }
Paul Bakker48916f92012-09-16 19:57:18 +00007552
Paul Bakker82788fb2014-10-20 13:59:19 +02007553 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007554 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007555 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007556 }
Paul Bakker48916f92012-09-16 19:57:18 +00007557
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007558 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007559 if( ssl->handshake == NULL ||
7560 ssl->transform_negotiate == NULL ||
7561 ssl->session_negotiate == NULL )
7562 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565 mbedtls_free( ssl->handshake );
7566 mbedtls_free( ssl->transform_negotiate );
7567 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007568
7569 ssl->handshake = NULL;
7570 ssl->transform_negotiate = NULL;
7571 ssl->session_negotiate = NULL;
7572
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007573 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007574 }
7575
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007576 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007578 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007579 ssl_handshake_params_init( ssl->handshake );
7580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007582 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007583 {
7584 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007585
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007586 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7587 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7588 else
7589 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007590
7591 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007592 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007593#endif
7594
Paul Bakker48916f92012-09-16 19:57:18 +00007595 return( 0 );
7596}
7597
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007598#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007599/* Dummy cookie callbacks for defaults */
7600static int ssl_cookie_write_dummy( void *ctx,
7601 unsigned char **p, unsigned char *end,
7602 const unsigned char *cli_id, size_t cli_id_len )
7603{
7604 ((void) ctx);
7605 ((void) p);
7606 ((void) end);
7607 ((void) cli_id);
7608 ((void) cli_id_len);
7609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007611}
7612
7613static int ssl_cookie_check_dummy( void *ctx,
7614 const unsigned char *cookie, size_t cookie_len,
7615 const unsigned char *cli_id, size_t cli_id_len )
7616{
7617 ((void) ctx);
7618 ((void) cookie);
7619 ((void) cookie_len);
7620 ((void) cli_id);
7621 ((void) cli_id_len);
7622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007624}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007625#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007626
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007627/* Once ssl->out_hdr as the address of the beginning of the
7628 * next outgoing record is set, deduce the other pointers.
7629 *
7630 * Note: For TLS, we save the implicit record sequence number
7631 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7632 * and the caller has to make sure there's space for this.
7633 */
7634
7635static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7636 mbedtls_ssl_transform *transform )
7637{
7638#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007639 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007640 {
7641 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007642#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007643 ssl->out_cid = ssl->out_ctr + 8;
7644 ssl->out_len = ssl->out_cid;
7645 if( transform != NULL )
7646 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007647#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007648 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007649#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007650 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007651 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007652 MBEDTLS_SSL_TRANSPORT_ELSE
7653#endif /* MBEDTLS_SSL_PROTO_DTLS */
7654#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007655 {
7656 ssl->out_ctr = ssl->out_hdr - 8;
7657 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007658#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007659 ssl->out_cid = ssl->out_len;
7660#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007661 ssl->out_iv = ssl->out_hdr + 5;
7662 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007663#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007664
7665 /* Adjust out_msg to make space for explicit IV, if used. */
7666 if( transform != NULL &&
7667 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7668 {
7669 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7670 }
7671 else
7672 ssl->out_msg = ssl->out_iv;
7673}
7674
7675/* Once ssl->in_hdr as the address of the beginning of the
7676 * next incoming record is set, deduce the other pointers.
7677 *
7678 * Note: For TLS, we save the implicit record sequence number
7679 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7680 * and the caller has to make sure there's space for this.
7681 */
7682
Hanno Beckerf5970a02019-05-08 09:38:41 +01007683static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007684{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007685 /* This function sets the pointers to match the case
7686 * of unprotected TLS/DTLS records, with both ssl->in_iv
7687 * and ssl->in_msg pointing to the beginning of the record
7688 * content.
7689 *
7690 * When decrypting a protected record, ssl->in_msg
7691 * will be shifted to point to the beginning of the
7692 * record plaintext.
7693 */
7694
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007696 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007697 {
Hanno Becker70e79282019-05-03 14:34:53 +01007698 /* This sets the header pointers to match records
7699 * without CID. When we receive a record containing
7700 * a CID, the fields are shifted accordingly in
7701 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007702 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007703#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007704 ssl->in_cid = ssl->in_ctr + 8;
7705 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007706#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007707 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007708#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007709 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007710 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007711 MBEDTLS_SSL_TRANSPORT_ELSE
7712#endif /* MBEDTLS_SSL_PROTO_DTLS */
7713#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007714 {
7715 ssl->in_ctr = ssl->in_hdr - 8;
7716 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007717#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007718 ssl->in_cid = ssl->in_len;
7719#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007720 ssl->in_iv = ssl->in_hdr + 5;
7721 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007722#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007723
Hanno Beckerf5970a02019-05-08 09:38:41 +01007724 /* This will be adjusted at record decryption time. */
7725 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007726}
7727
Paul Bakker5121ce52009-01-03 21:22:43 +00007728/*
7729 * Initialize an SSL context
7730 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007731void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7732{
7733 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7734}
7735
7736/*
7737 * Setup an SSL context
7738 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007739
7740static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7741{
7742 /* Set the incoming and outgoing record pointers. */
7743#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007744 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007745 {
7746 ssl->out_hdr = ssl->out_buf;
7747 ssl->in_hdr = ssl->in_buf;
7748 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007749 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007750#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007751#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007752 {
7753 ssl->out_hdr = ssl->out_buf + 8;
7754 ssl->in_hdr = ssl->in_buf + 8;
7755 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007756#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007757
7758 /* Derive other internal pointers. */
7759 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007760 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007761}
7762
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007763int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007764 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007765{
Paul Bakker48916f92012-09-16 19:57:18 +00007766 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007767
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007768 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007769
7770 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007771 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007772 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007773
7774 /* Set to NULL in case of an error condition */
7775 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007776
Angus Grattond8213d02016-05-25 20:56:48 +10007777 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7778 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007779 {
Angus Grattond8213d02016-05-25 20:56:48 +10007780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007781 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007782 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007783 }
7784
7785 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7786 if( ssl->out_buf == NULL )
7787 {
7788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007789 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007790 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007791 }
7792
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007793 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007794
Paul Bakker48916f92012-09-16 19:57:18 +00007795 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007796 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007797
7798 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007799
7800error:
7801 mbedtls_free( ssl->in_buf );
7802 mbedtls_free( ssl->out_buf );
7803
7804 ssl->conf = NULL;
7805
7806 ssl->in_buf = NULL;
7807 ssl->out_buf = NULL;
7808
7809 ssl->in_hdr = NULL;
7810 ssl->in_ctr = NULL;
7811 ssl->in_len = NULL;
7812 ssl->in_iv = NULL;
7813 ssl->in_msg = NULL;
7814
7815 ssl->out_hdr = NULL;
7816 ssl->out_ctr = NULL;
7817 ssl->out_len = NULL;
7818 ssl->out_iv = NULL;
7819 ssl->out_msg = NULL;
7820
k-stachowiak9f7798e2018-07-31 16:52:32 +02007821 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007822}
7823
7824/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007825 * Reset an initialized and used SSL context for re-use while retaining
7826 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007827 *
7828 * If partial is non-zero, keep data in the input buffer and client ID.
7829 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007830 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007831static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007832{
Paul Bakker48916f92012-09-16 19:57:18 +00007833 int ret;
7834
Hanno Becker7e772132018-08-10 12:38:21 +01007835#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7836 !defined(MBEDTLS_SSL_SRV_C)
7837 ((void) partial);
7838#endif
7839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007841
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007842 /* Cancel any possibly running timer */
7843 ssl_set_timer( ssl, 0 );
7844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845#if defined(MBEDTLS_SSL_RENEGOTIATION)
7846 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007847 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007848
7849 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7851 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007852#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007854
Paul Bakker7eb013f2011-10-06 12:37:39 +00007855 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007856 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007857
7858 ssl->in_msgtype = 0;
7859 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007861 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007862 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007865 ssl_dtls_replay_reset( ssl );
7866#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007867
7868 ssl->in_hslen = 0;
7869 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007870
7871 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007872
7873 ssl->out_msgtype = 0;
7874 ssl->out_msglen = 0;
7875 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7877 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007878 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007879#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007880
Hanno Becker19859472018-08-06 09:40:20 +01007881 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7882
Paul Bakker48916f92012-09-16 19:57:18 +00007883 ssl->transform_in = NULL;
7884 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007885
Hanno Becker78640902018-08-13 16:35:15 +01007886 ssl->session_in = NULL;
7887 ssl->session_out = NULL;
7888
Angus Grattond8213d02016-05-25 20:56:48 +10007889 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007890
7891#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007892 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007893#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7894 {
7895 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007896 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007897 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7900 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7903 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7906 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007907 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007908 }
7909#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007910
Paul Bakker48916f92012-09-16 19:57:18 +00007911 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 mbedtls_ssl_transform_free( ssl->transform );
7914 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007915 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007916 }
Paul Bakker48916f92012-09-16 19:57:18 +00007917
Paul Bakkerc0463502013-02-14 11:19:38 +01007918 if( ssl->session )
7919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 mbedtls_ssl_session_free( ssl->session );
7921 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007922 ssl->session = NULL;
7923 }
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007926 ssl->alpn_chosen = NULL;
7927#endif
7928
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007929#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007930#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007931 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007932#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007933 {
7934 mbedtls_free( ssl->cli_id );
7935 ssl->cli_id = NULL;
7936 ssl->cli_id_len = 0;
7937 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007938#endif
7939
Paul Bakker48916f92012-09-16 19:57:18 +00007940 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7941 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007942
7943 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007944}
7945
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007946/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007947 * Reset an initialized and used SSL context for re-use while retaining
7948 * all application-set variables, function pointers and data.
7949 */
7950int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7951{
7952 return( ssl_session_reset_int( ssl, 0 ) );
7953}
7954
7955/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007956 * SSL set accessors
7957 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007958void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007959{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007960 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007961}
7962
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007963void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007964{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007965 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007966}
7967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007969void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007970{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007971 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007972}
7973#endif
7974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007976void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007977{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007978 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007979}
7980#endif
7981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007983
Hanno Becker1841b0a2018-08-24 11:13:57 +01007984void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7985 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007986{
7987 ssl->disable_datagram_packing = !allow_packing;
7988}
7989
7990void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7991 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007992{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007993 conf->hs_timeout_min = min;
7994 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007995}
7996#endif
7997
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007998void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007999{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008000 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008001}
8002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008004void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008005 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008006 void *p_vrfy )
8007{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008008 conf->f_vrfy = f_vrfy;
8009 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008012
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008013void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008014 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008015 void *p_rng )
8016{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008017 conf->f_rng = f_rng;
8018 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008019}
8020
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008021void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008022 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008023 void *p_dbg )
8024{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008025 conf->f_dbg = f_dbg;
8026 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008027}
8028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008030 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008031 mbedtls_ssl_send_t *f_send,
8032 mbedtls_ssl_recv_t *f_recv,
8033 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008034{
8035 ssl->p_bio = p_bio;
8036 ssl->f_send = f_send;
8037 ssl->f_recv = f_recv;
8038 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008039}
8040
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008041#if defined(MBEDTLS_SSL_PROTO_DTLS)
8042void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8043{
8044 ssl->mtu = mtu;
8045}
8046#endif
8047
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008048void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008049{
8050 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008051}
8052
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008053void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8054 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008055 mbedtls_ssl_set_timer_t *f_set_timer,
8056 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008057{
8058 ssl->p_timer = p_timer;
8059 ssl->f_set_timer = f_set_timer;
8060 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008061
8062 /* Make sure we start with no timer running */
8063 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008064}
8065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008067void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008068 void *p_cache,
8069 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8070 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008071{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008072 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008073 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008074 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078#if defined(MBEDTLS_SSL_CLI_C)
8079int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008080{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008081 int ret;
8082
8083 if( ssl == NULL ||
8084 session == NULL ||
8085 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008086 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008089 }
8090
Hanno Becker58fccf22019-02-06 14:30:46 +00008091 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8092 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008093 return( ret );
8094
Paul Bakker0a597072012-09-25 21:55:46 +00008095 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008096
8097 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008100
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008101void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008102 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008103{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008104 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8105 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8106 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8107 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008108}
8109
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008110void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008111 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008112 int major, int minor )
8113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008115 return;
8116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008118 return;
8119
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008120 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008121}
8122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008124void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008125 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008126{
8127 conf->cert_profile = profile;
8128}
8129
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008130/* Append a new keycert entry to a (possibly empty) list */
8131static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8132 mbedtls_x509_crt *cert,
8133 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008134{
niisato8ee24222018-06-25 19:05:48 +09008135 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008136
niisato8ee24222018-06-25 19:05:48 +09008137 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8138 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008139 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008140
niisato8ee24222018-06-25 19:05:48 +09008141 new_cert->cert = cert;
8142 new_cert->key = key;
8143 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008144
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008145 /* Update head is the list was null, else add to the end */
8146 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008147 {
niisato8ee24222018-06-25 19:05:48 +09008148 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008149 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008150 else
8151 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008152 mbedtls_ssl_key_cert *cur = *head;
8153 while( cur->next != NULL )
8154 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008155 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008156 }
8157
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008158 return( 0 );
8159}
8160
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008161int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008162 mbedtls_x509_crt *own_cert,
8163 mbedtls_pk_context *pk_key )
8164{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008165 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008166}
8167
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008168void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008169 mbedtls_x509_crt *ca_chain,
8170 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008171{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008172 conf->ca_chain = ca_chain;
8173 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008176
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008177#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8178int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8179 mbedtls_x509_crt *own_cert,
8180 mbedtls_pk_context *pk_key )
8181{
8182 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8183 own_cert, pk_key ) );
8184}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008185
8186void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8187 mbedtls_x509_crt *ca_chain,
8188 mbedtls_x509_crl *ca_crl )
8189{
8190 ssl->handshake->sni_ca_chain = ca_chain;
8191 ssl->handshake->sni_ca_crl = ca_crl;
8192}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008193
8194void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8195 int authmode )
8196{
8197 ssl->handshake->sni_authmode = authmode;
8198}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008199#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8200
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008201#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008202/*
8203 * Set EC J-PAKE password for current handshake
8204 */
8205int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8206 const unsigned char *pw,
8207 size_t pw_len )
8208{
8209 mbedtls_ecjpake_role role;
8210
Janos Follath8eb64132016-06-03 15:40:57 +01008211 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8213
8214 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8215 role = MBEDTLS_ECJPAKE_SERVER;
8216 else
8217 role = MBEDTLS_ECJPAKE_CLIENT;
8218
8219 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8220 role,
8221 MBEDTLS_MD_SHA256,
8222 MBEDTLS_ECP_DP_SECP256R1,
8223 pw, pw_len ) );
8224}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008225#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008228int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008229 const unsigned char *psk, size_t psk_len,
8230 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008231{
Paul Bakker6db455e2013-09-18 17:29:31 +02008232 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008237
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008238 /* Identity len will be encoded on two bytes */
8239 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008240 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008241 {
8242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8243 }
8244
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008245 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008246 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008247 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008248
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008249 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008250 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008251 conf->psk_len = 0;
8252 }
8253 if( conf->psk_identity != NULL )
8254 {
8255 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008256 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008257 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008258 }
8259
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008260 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8261 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008262 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008263 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008264 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008265 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008266 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008267 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008268 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008269
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008270 conf->psk_len = psk_len;
8271 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008272
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008273 memcpy( conf->psk, psk, conf->psk_len );
8274 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008275
8276 return( 0 );
8277}
8278
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008279int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8280 const unsigned char *psk, size_t psk_len )
8281{
8282 if( psk == NULL || ssl->handshake == NULL )
8283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8284
8285 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8287
8288 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008289 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008290 mbedtls_platform_zeroize( ssl->handshake->psk,
8291 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008292 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008293 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008294 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008295
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008296 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008297 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008298
8299 ssl->handshake->psk_len = psk_len;
8300 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8301
8302 return( 0 );
8303}
8304
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008305void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008306 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008307 size_t),
8308 void *p_psk )
8309{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008310 conf->f_psk = f_psk;
8311 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008312}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008314
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008315#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008316
8317#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008318int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008319{
8320 int ret;
8321
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008322 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8323 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8324 {
8325 mbedtls_mpi_free( &conf->dhm_P );
8326 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008327 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008328 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008329
8330 return( 0 );
8331}
Hanno Becker470a8c42017-10-04 15:28:46 +01008332#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008333
Hanno Beckera90658f2017-10-04 15:29:08 +01008334int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8335 const unsigned char *dhm_P, size_t P_len,
8336 const unsigned char *dhm_G, size_t G_len )
8337{
8338 int ret;
8339
8340 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8341 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8342 {
8343 mbedtls_mpi_free( &conf->dhm_P );
8344 mbedtls_mpi_free( &conf->dhm_G );
8345 return( ret );
8346 }
8347
8348 return( 0 );
8349}
Paul Bakker5121ce52009-01-03 21:22:43 +00008350
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008351int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008352{
8353 int ret;
8354
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008355 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8356 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8357 {
8358 mbedtls_mpi_free( &conf->dhm_P );
8359 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008360 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008361 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008362
8363 return( 0 );
8364}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008365#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008366
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008367#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8368/*
8369 * Set the minimum length for Diffie-Hellman parameters
8370 */
8371void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8372 unsigned int bitlen )
8373{
8374 conf->dhm_min_bitlen = bitlen;
8375}
8376#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8377
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008378#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008379/*
8380 * Set allowed/preferred hashes for handshake signatures
8381 */
8382void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8383 const int *hashes )
8384{
8385 conf->sig_hashes = hashes;
8386}
Hanno Becker947194e2017-04-07 13:25:49 +01008387#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008388
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008389#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008390/*
8391 * Set the allowed elliptic curves
8392 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008393void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008394 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008395{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008396 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008397}
Hanno Becker947194e2017-04-07 13:25:49 +01008398#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008399
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008400#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008402{
Hanno Becker947194e2017-04-07 13:25:49 +01008403 /* Initialize to suppress unnecessary compiler warning */
8404 size_t hostname_len = 0;
8405
8406 /* Check if new hostname is valid before
8407 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008408 if( hostname != NULL )
8409 {
8410 hostname_len = strlen( hostname );
8411
8412 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8414 }
8415
8416 /* Now it's clear that we will overwrite the old hostname,
8417 * so we can free it safely */
8418
8419 if( ssl->hostname != NULL )
8420 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008421 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008422 mbedtls_free( ssl->hostname );
8423 }
8424
8425 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008426
Paul Bakker5121ce52009-01-03 21:22:43 +00008427 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008428 {
8429 ssl->hostname = NULL;
8430 }
8431 else
8432 {
8433 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008434 if( ssl->hostname == NULL )
8435 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008436
Hanno Becker947194e2017-04-07 13:25:49 +01008437 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008438
Hanno Becker947194e2017-04-07 13:25:49 +01008439 ssl->hostname[hostname_len] = '\0';
8440 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008441
8442 return( 0 );
8443}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008444#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008445
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008446#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008447void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008449 const unsigned char *, size_t),
8450 void *p_sni )
8451{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008452 conf->f_sni = f_sni;
8453 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008458int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008459{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008460 size_t cur_len, tot_len;
8461 const char **p;
8462
8463 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008464 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8465 * MUST NOT be truncated."
8466 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008467 */
8468 tot_len = 0;
8469 for( p = protos; *p != NULL; p++ )
8470 {
8471 cur_len = strlen( *p );
8472 tot_len += cur_len;
8473
8474 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008476 }
8477
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008478 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008479
8480 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008481}
8482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008484{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008485 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008488
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008489void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008490{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008491 conf->max_major_ver = major;
8492 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008493}
8494
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008495void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008496{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008497 conf->min_major_ver = major;
8498 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008499}
8500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008502void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008503{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008504 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008505}
8506#endif
8507
Janos Follath088ce432017-04-10 12:42:31 +01008508#if defined(MBEDTLS_SSL_SRV_C)
8509void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8510 char cert_req_ca_list )
8511{
8512 conf->cert_req_ca_list = cert_req_ca_list;
8513}
8514#endif
8515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008517void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008518{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008519 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008520}
8521#endif
8522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008524void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008525{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008526 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008527}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008528
8529void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008530 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008531{
8532 conf->enforce_extended_master_secret = ems_enf;
8533}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008534#endif
8535
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008536#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008537void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008539 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008540}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008541#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008544int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008546 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008547 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008550 }
8551
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008552 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008553
8554 return( 0 );
8555}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008559void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008560{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008561 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008566void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008567{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008568 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008569}
8570#endif
8571
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008572void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008573{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008574 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008575}
8576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008578void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008579{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008580 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008581}
8582
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008583void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008585 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008586}
8587
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008588void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008589 const unsigned char period[8] )
8590{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008591 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008592}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008596#if defined(MBEDTLS_SSL_CLI_C)
8597void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008598{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008599 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008600}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008601#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008602
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008603#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008604void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8605 mbedtls_ssl_ticket_write_t *f_ticket_write,
8606 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8607 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008608{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008609 conf->f_ticket_write = f_ticket_write;
8610 conf->f_ticket_parse = f_ticket_parse;
8611 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008612}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008615
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008616#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8617void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8618 mbedtls_ssl_export_keys_t *f_export_keys,
8619 void *p_export_keys )
8620{
8621 conf->f_export_keys = f_export_keys;
8622 conf->p_export_keys = p_export_keys;
8623}
8624#endif
8625
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008626#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008627void mbedtls_ssl_conf_async_private_cb(
8628 mbedtls_ssl_config *conf,
8629 mbedtls_ssl_async_sign_t *f_async_sign,
8630 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8631 mbedtls_ssl_async_resume_t *f_async_resume,
8632 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008633 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008634{
8635 conf->f_async_sign_start = f_async_sign;
8636 conf->f_async_decrypt_start = f_async_decrypt;
8637 conf->f_async_resume = f_async_resume;
8638 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008639 conf->p_async_config_data = async_config_data;
8640}
8641
Gilles Peskine8f97af72018-04-26 11:46:10 +02008642void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8643{
8644 return( conf->p_async_config_data );
8645}
8646
Gilles Peskine1febfef2018-04-30 11:54:39 +02008647void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008648{
8649 if( ssl->handshake == NULL )
8650 return( NULL );
8651 else
8652 return( ssl->handshake->user_async_ctx );
8653}
8654
Gilles Peskine1febfef2018-04-30 11:54:39 +02008655void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008656 void *ctx )
8657{
8658 if( ssl->handshake != NULL )
8659 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008660}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008661#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008662
Paul Bakker5121ce52009-01-03 21:22:43 +00008663/*
8664 * SSL get accessors
8665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008667{
8668 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8669}
8670
Hanno Becker8b170a02017-10-10 11:51:19 +01008671int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8672{
8673 /*
8674 * Case A: We're currently holding back
8675 * a message for further processing.
8676 */
8677
8678 if( ssl->keep_current_message == 1 )
8679 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008681 return( 1 );
8682 }
8683
8684 /*
8685 * Case B: Further records are pending in the current datagram.
8686 */
8687
8688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008689 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008690 ssl->in_left > ssl->next_record_offset )
8691 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008693 return( 1 );
8694 }
8695#endif /* MBEDTLS_SSL_PROTO_DTLS */
8696
8697 /*
8698 * Case C: A handshake message is being processed.
8699 */
8700
Hanno Becker8b170a02017-10-10 11:51:19 +01008701 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8702 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008704 return( 1 );
8705 }
8706
8707 /*
8708 * Case D: An application data message is being processed
8709 */
8710 if( ssl->in_offt != NULL )
8711 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008713 return( 1 );
8714 }
8715
8716 /*
8717 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008718 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008719 * we implement support for multiple alerts in single records.
8720 */
8721
8722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8723 return( 0 );
8724}
8725
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008726uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008727{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008728 if( ssl->session != NULL )
8729 return( ssl->session->verify_result );
8730
8731 if( ssl->session_negotiate != NULL )
8732 return( ssl->session_negotiate->verify_result );
8733
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008734 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008735}
8736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008738{
Paul Bakker926c8e42013-03-06 10:23:34 +01008739 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008740 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008742 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008743}
8744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008745const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008748 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008749 {
8750 switch( ssl->minor_ver )
8751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008753 return( "DTLSv1.0" );
8754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008756 return( "DTLSv1.2" );
8757
8758 default:
8759 return( "unknown (DTLS)" );
8760 }
8761 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008762 MBEDTLS_SSL_TRANSPORT_ELSE
8763#endif /* MBEDTLS_SSL_PROTO_DTLS */
8764#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008765 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008766 switch( ssl->minor_ver )
8767 {
8768 case MBEDTLS_SSL_MINOR_VERSION_0:
8769 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008770
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008771 case MBEDTLS_SSL_MINOR_VERSION_1:
8772 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008773
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008774 case MBEDTLS_SSL_MINOR_VERSION_2:
8775 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008776
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008777 case MBEDTLS_SSL_MINOR_VERSION_3:
8778 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008779
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008780 default:
8781 return( "unknown" );
8782 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008783 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008784#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008785}
8786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008788{
Hanno Becker3136ede2018-08-17 15:28:19 +01008789 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008791 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008792
Hanno Becker43395762019-05-03 14:46:38 +01008793 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8794
Hanno Becker78640902018-08-13 16:35:15 +01008795 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008796 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798#if defined(MBEDTLS_ZLIB_SUPPORT)
8799 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8800 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008801#endif
8802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008803 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805 case MBEDTLS_MODE_GCM:
8806 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008807 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008808 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008809 transform_expansion = transform->minlen;
8810 break;
8811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008813
8814 block_size = mbedtls_cipher_get_block_size(
8815 &transform->cipher_ctx_enc );
8816
Hanno Becker3136ede2018-08-17 15:28:19 +01008817 /* Expansion due to the addition of the MAC. */
8818 transform_expansion += transform->maclen;
8819
8820 /* Expansion due to the addition of CBC padding;
8821 * Theoretically up to 256 bytes, but we never use
8822 * more than the block size of the underlying cipher. */
8823 transform_expansion += block_size;
8824
8825 /* For TLS 1.1 or higher, an explicit IV is added
8826 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008827#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8828 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008829 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008830#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008831
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008832 break;
8833
8834 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008837 }
8838
Hanno Beckera5a2b082019-05-15 14:03:01 +01008839#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008840 if( transform->out_cid_len != 0 )
8841 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008842#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008843
Hanno Becker43395762019-05-03 14:46:38 +01008844 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008845}
8846
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008847#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8848size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8849{
8850 size_t max_len;
8851
8852 /*
8853 * Assume mfl_code is correct since it was checked when set
8854 */
Angus Grattond8213d02016-05-25 20:56:48 +10008855 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008856
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008857 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008858 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008859 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008860 {
Angus Grattond8213d02016-05-25 20:56:48 +10008861 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008862 }
8863
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008864 /* During a handshake, use the value being negotiated */
8865 if( ssl->session_negotiate != NULL &&
8866 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8867 {
8868 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8869 }
8870
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008871 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008872}
8873#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8874
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008875#if defined(MBEDTLS_SSL_PROTO_DTLS)
8876static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8877{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008878 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8879 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8880 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8881 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8882 return ( 0 );
8883
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008884 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8885 return( ssl->mtu );
8886
8887 if( ssl->mtu == 0 )
8888 return( ssl->handshake->mtu );
8889
8890 return( ssl->mtu < ssl->handshake->mtu ?
8891 ssl->mtu : ssl->handshake->mtu );
8892}
8893#endif /* MBEDTLS_SSL_PROTO_DTLS */
8894
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008895int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8896{
8897 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8898
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008899#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8900 !defined(MBEDTLS_SSL_PROTO_DTLS)
8901 (void) ssl;
8902#endif
8903
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008904#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8905 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8906
8907 if( max_len > mfl )
8908 max_len = mfl;
8909#endif
8910
8911#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008912 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008913 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008914 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008915 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8916 const size_t overhead = (size_t) ret;
8917
8918 if( ret < 0 )
8919 return( ret );
8920
8921 if( mtu <= overhead )
8922 {
8923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8924 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8925 }
8926
8927 if( max_len > mtu - overhead )
8928 max_len = mtu - overhead;
8929 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008930#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008931
Hanno Becker0defedb2018-08-10 12:35:02 +01008932#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8933 !defined(MBEDTLS_SSL_PROTO_DTLS)
8934 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008935#endif
8936
8937 return( (int) max_len );
8938}
8939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008940#if defined(MBEDTLS_X509_CRT_PARSE_C)
8941const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008942{
8943 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008944 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008945
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008946 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008947}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00008951int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8952 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008953{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008954 if( ssl == NULL ||
8955 dst == NULL ||
8956 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008957 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008960 }
8961
Hanno Becker58fccf22019-02-06 14:30:46 +00008962 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008963}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008965
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008966const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8967{
8968 if( ssl == NULL )
8969 return( NULL );
8970
8971 return( ssl->session );
8972}
8973
Paul Bakker5121ce52009-01-03 21:22:43 +00008974/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008975 * Define ticket header determining Mbed TLS version
8976 * and structure of the ticket.
8977 */
8978
Hanno Becker41527622019-05-16 12:50:45 +01008979/*
Hanno Becker26829e92019-05-28 14:30:45 +01008980 * Define bitflag determining compile-time settings influencing
8981 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008982 */
8983
Hanno Becker26829e92019-05-28 14:30:45 +01008984#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008985#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008986#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008987#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008988#endif /* MBEDTLS_HAVE_TIME */
8989
8990#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008991#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008992#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008993#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008994#endif /* MBEDTLS_X509_CRT_PARSE_C */
8995
8996#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008997#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008998#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008999#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009000#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9001
9002#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009003#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009004#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009005#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009006#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9007
9008#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009009#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009010#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009011#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009012#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9013
9014#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009015#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009016#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009017#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009018#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9019
Hanno Becker41527622019-05-16 12:50:45 +01009020#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9021#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9022#else
9023#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9024#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9025
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009026#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9027#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9028#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9029#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9030#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9031#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9032#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009033
Hanno Becker26829e92019-05-28 14:30:45 +01009034#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009035 ( (uint16_t) ( \
9036 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9037 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9038 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9039 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9040 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9041 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01009042 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009043
Hanno Becker557fe9f2019-05-16 12:41:07 +01009044static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009045 MBEDTLS_VERSION_MAJOR,
9046 MBEDTLS_VERSION_MINOR,
9047 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009048 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9049 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009050};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009051
9052/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009053 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009054 * (in the presentation language of TLS, RFC 8446 section 3)
9055 *
Hanno Becker26829e92019-05-28 14:30:45 +01009056 * opaque mbedtls_version[3]; // major, minor, patch
9057 * opaque session_format[2]; // version-specific 16-bit field determining
9058 * // the format of the remaining
9059 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009060 *
9061 * Note: When updating the format, remember to keep
9062 * these version+format bytes.
9063 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009064 * // In this version, `session_format` determines
9065 * // the setting of those compile-time
9066 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009067 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009068 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009069 * uint8 ciphersuite[2]; // defined by the standard
9070 * uint8 compression; // 0 or 1
9071 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009072 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009073 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009074 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01009075 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
9076 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009077 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009078 * uint8 mfl_code; // up to 255 according to standard
9079 * uint8 trunc_hmac; // 0 or 1
9080 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009081 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009082 * The order is the same as in the definition of the structure, except
9083 * verify_result is put before peer_cert so that all mandatory fields come
9084 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009085 */
9086int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9087 unsigned char *buf,
9088 size_t buf_len,
9089 size_t *olen )
9090{
9091 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009092 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009093#if defined(MBEDTLS_HAVE_TIME)
9094 uint64_t start;
9095#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009096#if defined(MBEDTLS_X509_CRT_PARSE_C)
9097 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009098#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009099
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009100 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009101 * Add version identifier
9102 */
9103
9104 used += sizeof( ssl_serialized_session_header );
9105
9106 if( used <= buf_len )
9107 {
9108 memcpy( p, ssl_serialized_session_header,
9109 sizeof( ssl_serialized_session_header ) );
9110 p += sizeof( ssl_serialized_session_header );
9111 }
9112
9113 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009114 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009115 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009116#if defined(MBEDTLS_HAVE_TIME)
9117 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009118
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009119 if( used <= buf_len )
9120 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009121 start = (uint64_t) session->start;
9122
9123 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9124 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9125 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9126 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9127 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9128 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9129 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9130 *p++ = (unsigned char)( ( start ) & 0xFF );
9131 }
9132#endif /* MBEDTLS_HAVE_TIME */
9133
9134 /*
9135 * Basic mandatory fields
9136 */
9137 used += 2 /* ciphersuite */
9138 + 1 /* compression */
9139 + 1 /* id_len */
9140 + sizeof( session->id )
9141 + sizeof( session->master )
9142 + 4; /* verify_result */
9143
9144 if( used <= buf_len )
9145 {
9146 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9147 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9148
9149 *p++ = (unsigned char)( session->compression & 0xFF );
9150
9151 *p++ = (unsigned char)( session->id_len & 0xFF );
9152 memcpy( p, session->id, 32 );
9153 p += 32;
9154
9155 memcpy( p, session->master, 48 );
9156 p += 48;
9157
9158 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9159 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9160 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9161 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009162 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009163
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009164 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009165 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009166 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009167#if defined(MBEDTLS_X509_CRT_PARSE_C)
9168 if( session->peer_cert == NULL )
9169 cert_len = 0;
9170 else
9171 cert_len = session->peer_cert->raw.len;
9172
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009173 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009174
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009175 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009176 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009177 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9178 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9179 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9180
9181 if( session->peer_cert != NULL )
9182 {
9183 memcpy( p, session->peer_cert->raw.p, cert_len );
9184 p += cert_len;
9185 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009186 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009187#endif /* MBEDTLS_X509_CRT_PARSE_C */
9188
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009189 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009190 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009191 */
9192#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009193 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009194
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009195 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009196 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009197 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9198 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9199 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9200
9201 if( session->ticket != NULL )
9202 {
9203 memcpy( p, session->ticket, session->ticket_len );
9204 p += session->ticket_len;
9205 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009206
9207 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9208 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9209 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9210 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009211 }
9212#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9213
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009214 /*
9215 * Misc extension-related info
9216 */
9217#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9218 used += 1;
9219
9220 if( used <= buf_len )
9221 *p++ = session->mfl_code;
9222#endif
9223
9224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9225 used += 1;
9226
9227 if( used <= buf_len )
9228 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9229#endif
9230
9231#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9232 used += 1;
9233
9234 if( used <= buf_len )
9235 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9236#endif
9237
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009238 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009239 *olen = used;
9240
9241 if( used > buf_len )
9242 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009243
9244 return( 0 );
9245}
9246
9247/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009248 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009249 *
9250 * This internal version is wrapped by a public function that cleans up in
9251 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009252 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009253static int ssl_session_load( mbedtls_ssl_session *session,
9254 const unsigned char *buf,
9255 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009256{
9257 const unsigned char *p = buf;
9258 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009259#if defined(MBEDTLS_HAVE_TIME)
9260 uint64_t start;
9261#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009262#if defined(MBEDTLS_X509_CRT_PARSE_C)
9263 size_t cert_len;
9264#endif /* MBEDTLS_X509_CRT_PARSE_C */
9265
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009266 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009267 * Check version identifier
9268 */
9269
9270 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009272
9273 if( memcmp( p, ssl_serialized_session_header,
9274 sizeof( ssl_serialized_session_header ) ) != 0 )
9275 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009276 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009277 }
9278 p += sizeof( ssl_serialized_session_header );
9279
9280 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009281 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009282 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009283#if defined(MBEDTLS_HAVE_TIME)
9284 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9286
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009287 start = ( (uint64_t) p[0] << 56 ) |
9288 ( (uint64_t) p[1] << 48 ) |
9289 ( (uint64_t) p[2] << 40 ) |
9290 ( (uint64_t) p[3] << 32 ) |
9291 ( (uint64_t) p[4] << 24 ) |
9292 ( (uint64_t) p[5] << 16 ) |
9293 ( (uint64_t) p[6] << 8 ) |
9294 ( (uint64_t) p[7] );
9295 p += 8;
9296
9297 session->start = (time_t) start;
9298#endif /* MBEDTLS_HAVE_TIME */
9299
9300 /*
9301 * Basic mandatory fields
9302 */
9303 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9305
9306 session->ciphersuite = ( p[0] << 8 ) | p[1];
9307 p += 2;
9308
9309 session->compression = *p++;
9310
9311 session->id_len = *p++;
9312 memcpy( session->id, p, 32 );
9313 p += 32;
9314
9315 memcpy( session->master, p, 48 );
9316 p += 48;
9317
9318 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9319 ( (uint32_t) p[1] << 16 ) |
9320 ( (uint32_t) p[2] << 8 ) |
9321 ( (uint32_t) p[3] );
9322 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009323
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009324 /* Immediately clear invalid pointer values that have been read, in case
9325 * we exit early before we replaced them with valid ones. */
9326#if defined(MBEDTLS_X509_CRT_PARSE_C)
9327 session->peer_cert = NULL;
9328#endif
9329#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9330 session->ticket = NULL;
9331#endif
9332
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009333 /*
9334 * Peer certificate
9335 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009336#if defined(MBEDTLS_X509_CRT_PARSE_C)
9337 if( 3 > (size_t)( end - p ) )
9338 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9339
9340 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9341 p += 3;
9342
9343 if( cert_len == 0 )
9344 {
9345 session->peer_cert = NULL;
9346 }
9347 else
9348 {
9349 int ret;
9350
9351 if( cert_len > (size_t)( end - p ) )
9352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9353
9354 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9355
9356 if( session->peer_cert == NULL )
9357 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9358
9359 mbedtls_x509_crt_init( session->peer_cert );
9360
9361 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9362 p, cert_len ) ) != 0 )
9363 {
9364 mbedtls_x509_crt_free( session->peer_cert );
9365 mbedtls_free( session->peer_cert );
9366 session->peer_cert = NULL;
9367 return( ret );
9368 }
9369
9370 p += cert_len;
9371 }
9372#endif /* MBEDTLS_X509_CRT_PARSE_C */
9373
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009374 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009375 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009376 */
9377#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9378 if( 3 > (size_t)( end - p ) )
9379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9380
9381 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9382 p += 3;
9383
9384 if( session->ticket_len != 0 )
9385 {
9386 if( session->ticket_len > (size_t)( end - p ) )
9387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9388
9389 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9390 if( session->ticket == NULL )
9391 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9392
9393 memcpy( session->ticket, p, session->ticket_len );
9394 p += session->ticket_len;
9395 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009396
9397 if( 4 > (size_t)( end - p ) )
9398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9399
9400 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9401 ( (uint32_t) p[1] << 16 ) |
9402 ( (uint32_t) p[2] << 8 ) |
9403 ( (uint32_t) p[3] );
9404 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009405#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9406
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009407 /*
9408 * Misc extension-related info
9409 */
9410#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9411 if( 1 > (size_t)( end - p ) )
9412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9413
9414 session->mfl_code = *p++;
9415#endif
9416
9417#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9418 if( 1 > (size_t)( end - p ) )
9419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9420
9421 session->trunc_hmac = *p++;
9422#endif
9423
9424#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9425 if( 1 > (size_t)( end - p ) )
9426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9427
9428 session->encrypt_then_mac = *p++;
9429#endif
9430
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009431 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009432 if( p != end )
9433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9434
9435 return( 0 );
9436}
9437
9438/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009439 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009440 */
9441int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9442 const unsigned char *buf,
9443 size_t len )
9444{
9445 int ret = ssl_session_load( session, buf, len );
9446
9447 if( ret != 0 )
9448 mbedtls_ssl_session_free( session );
9449
9450 return( ret );
9451}
9452
9453/*
Paul Bakker1961b702013-01-25 14:49:24 +01009454 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009457{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009459
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009460 if( ssl == NULL || ssl->conf == NULL )
9461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009464 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009468 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009470#endif
9471
Paul Bakker1961b702013-01-25 14:49:24 +01009472 return( ret );
9473}
9474
9475/*
9476 * Perform the SSL handshake
9477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009479{
9480 int ret = 0;
9481
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009482 if( ssl == NULL || ssl->conf == NULL )
9483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009490
9491 if( ret != 0 )
9492 break;
9493 }
9494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009496
9497 return( ret );
9498}
9499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500#if defined(MBEDTLS_SSL_RENEGOTIATION)
9501#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009502/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009503 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009506{
9507 int ret;
9508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009510
9511 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9513 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009514
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009515 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009516 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009518 return( ret );
9519 }
9520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009522
9523 return( 0 );
9524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009526
9527/*
9528 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 * - any side: calling mbedtls_ssl_renegotiate(),
9530 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9531 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009532 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009533 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009537{
9538 int ret;
9539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009541
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009542 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9543 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009544
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009545 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9546 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009548 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009550 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009551 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009552 ssl->handshake->out_msg_seq = 1;
9553 else
9554 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009555 }
9556#endif
9557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9559 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009564 return( ret );
9565 }
9566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009568
9569 return( 0 );
9570}
9571
9572/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009573 * Renegotiate current connection on client,
9574 * or request renegotiation on server
9575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009579
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009580 if( ssl == NULL || ssl->conf == NULL )
9581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009584 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009585 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009591
9592 /* Did we already try/start sending HelloRequest? */
9593 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009595
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009596 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009597 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009601 /*
9602 * On client, either start the renegotiation process or,
9603 * if already in progress, continue the handshake
9604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009609
9610 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009613 return( ret );
9614 }
9615 }
9616 else
9617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009621 return( ret );
9622 }
9623 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009625
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009626 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009627}
9628
9629/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009630 * Check record counters and renegotiate if they're above the limit.
9631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009633{
Andres AG2196c7f2016-12-15 17:01:16 +00009634 size_t ep_len = ssl_ep_len( ssl );
9635 int in_ctr_cmp;
9636 int out_ctr_cmp;
9637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9639 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009640 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009641 {
9642 return( 0 );
9643 }
9644
Andres AG2196c7f2016-12-15 17:01:16 +00009645 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9646 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009647 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009648 ssl->conf->renego_period + ep_len, 8 - ep_len );
9649
9650 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009651 {
9652 return( 0 );
9653 }
9654
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009659
9660/*
9661 * Receive application data decrypted from the SSL layer
9662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009664{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009665 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009666 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009667
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009668 if( ssl == NULL || ssl->conf == NULL )
9669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009674 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009677 return( ret );
9678
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009679 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009681 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009682 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009683 return( ret );
9684 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009685 }
9686#endif
9687
Hanno Becker4a810fb2017-05-24 16:27:30 +01009688 /*
9689 * Check if renegotiation is necessary and/or handshake is
9690 * in process. If yes, perform/continue, and fall through
9691 * if an unexpected packet is received while the client
9692 * is waiting for the ServerHello.
9693 *
9694 * (There is no equivalent to the last condition on
9695 * the server-side as it is not treated as within
9696 * a handshake while waiting for the ClientHello
9697 * after a renegotiation request.)
9698 */
9699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009701 ret = ssl_check_ctr_renegotiate( ssl );
9702 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9703 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009706 return( ret );
9707 }
9708#endif
9709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009713 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9714 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009717 return( ret );
9718 }
9719 }
9720
Hanno Beckere41158b2017-10-23 13:30:32 +01009721 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009722 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009723 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009724 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009725 if( ssl->f_get_timer != NULL &&
9726 ssl->f_get_timer( ssl->p_timer ) == -1 )
9727 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009728 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009729 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009730
Hanno Becker327c93b2018-08-15 13:56:18 +01009731 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009732 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009733 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9734 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009735
Hanno Becker4a810fb2017-05-24 16:27:30 +01009736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9737 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009738 }
9739
9740 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009742 {
9743 /*
9744 * OpenSSL sends empty messages to randomize the IV
9745 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009746 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009749 return( 0 );
9750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009752 return( ret );
9753 }
9754 }
9755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009759
Hanno Becker4a810fb2017-05-24 16:27:30 +01009760 /*
9761 * - For client-side, expect SERVER_HELLO_REQUEST.
9762 * - For server-side, expect CLIENT_HELLO.
9763 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9764 */
9765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009767 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009769 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009772
9773 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009775 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009776 {
9777 continue;
9778 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009779 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009780#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009781#if defined(MBEDTLS_SSL_PROTO_TLS)
9782 {
9783 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9784 }
9785#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009786 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009787#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009788
Hanno Becker4a810fb2017-05-24 16:27:30 +01009789#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009790 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009794
9795 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009797 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009798 {
9799 continue;
9800 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009801 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009802#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009803#if defined(MBEDTLS_SSL_PROTO_TLS)
9804 {
9805 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9806 }
9807#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009808 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009809#endif /* MBEDTLS_SSL_SRV_C */
9810
Hanno Becker21df7f92017-10-17 11:03:26 +01009811#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009812 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009813 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9814 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9815 ssl->conf->allow_legacy_renegotiation ==
9816 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9817 {
9818 /*
9819 * Accept renegotiation request
9820 */
Paul Bakker48916f92012-09-16 19:57:18 +00009821
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009822 /* DTLS clients need to know renego is server-initiated */
9823#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009824 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009825 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9826 {
9827 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9828 }
9829#endif
9830 ret = ssl_start_renegotiation( ssl );
9831 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9832 ret != 0 )
9833 {
9834 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9835 return( ret );
9836 }
9837 }
9838 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009839#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009840 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009841 /*
9842 * Refuse renegotiation
9843 */
9844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847#if defined(MBEDTLS_SSL_PROTO_SSL3)
9848 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009849 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009850 /* SSLv3 does not have a "no_renegotiation" warning, so
9851 we send a fatal alert and abort the connection. */
9852 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9853 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9854 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009855 }
9856 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9858#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9859 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9860 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9863 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9864 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009865 {
9866 return( ret );
9867 }
Paul Bakker48916f92012-09-16 19:57:18 +00009868 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009869 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9871 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9874 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009875 }
Paul Bakker48916f92012-09-16 19:57:18 +00009876 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009877
Hanno Becker90333da2017-10-10 11:27:13 +01009878 /* At this point, we don't know whether the renegotiation has been
9879 * completed or not. The cases to consider are the following:
9880 * 1) The renegotiation is complete. In this case, no new record
9881 * has been read yet.
9882 * 2) The renegotiation is incomplete because the client received
9883 * an application data record while awaiting the ServerHello.
9884 * 3) The renegotiation is incomplete because the client received
9885 * a non-handshake, non-application data message while awaiting
9886 * the ServerHello.
9887 * In each of these case, looping will be the proper action:
9888 * - For 1), the next iteration will read a new record and check
9889 * if it's application data.
9890 * - For 2), the loop condition isn't satisfied as application data
9891 * is present, hence continue is the same as break
9892 * - For 3), the loop condition is satisfied and read_record
9893 * will re-deliver the message that was held back by the client
9894 * when expecting the ServerHello.
9895 */
9896 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009897 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009898#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009900 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009901 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009902 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009903 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009906 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009908 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009909 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9914 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009917 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009918 }
9919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9923 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009924 }
9925
9926 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009927
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009928 /* We're going to return something now, cancel timer,
9929 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009931 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009932
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009933#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009934 /* If we requested renego but received AppData, resend HelloRequest.
9935 * Do it now, after setting in_offt, to avoid taking this branch
9936 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009940 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009941 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009944 return( ret );
9945 }
9946 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009948#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009949 }
9950
9951 n = ( len < ssl->in_msglen )
9952 ? len : ssl->in_msglen;
9953
9954 memcpy( buf, ssl->in_offt, n );
9955 ssl->in_msglen -= n;
9956
9957 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009958 {
9959 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009960 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009961 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009963 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009964 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009965 /* more data available */
9966 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009967 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009970
Paul Bakker23986e52011-04-24 08:57:21 +00009971 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009972}
9973
9974/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009975 * Send application data to be encrypted by the SSL layer, taking care of max
9976 * fragment length and buffer size.
9977 *
9978 * According to RFC 5246 Section 6.2.1:
9979 *
9980 * Zero-length fragments of Application data MAY be sent as they are
9981 * potentially useful as a traffic analysis countermeasure.
9982 *
9983 * Therefore, it is possible that the input message length is 0 and the
9984 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009985 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009986static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009987 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009988{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009989 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9990 const size_t max_len = (size_t) ret;
9991
9992 if( ret < 0 )
9993 {
9994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9995 return( ret );
9996 }
9997
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009998 if( len > max_len )
9999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010001 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010004 "maximum fragment length: %d > %d",
10005 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010007 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010008 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010009#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010010#if defined(MBEDTLS_SSL_PROTO_TLS)
10011 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010012 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010013 }
10014#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010015 }
Paul Bakker887bd502011-06-08 13:10:54 +000010016
Paul Bakker5121ce52009-01-03 21:22:43 +000010017 if( ssl->out_left != 0 )
10018 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010019 /*
10020 * The user has previously tried to send the data and
10021 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10022 * written. In this case, we expect the high-level write function
10023 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010025 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010028 return( ret );
10029 }
10030 }
Paul Bakker887bd502011-06-08 13:10:54 +000010031 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010032 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010033 /*
10034 * The user is trying to send a message the first time, so we need to
10035 * copy the data into the internal buffers and setup the data structure
10036 * to keep track of partial writes
10037 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010038 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010040 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010041
Hanno Becker67bc7c32018-08-06 11:33:50 +010010042 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010045 return( ret );
10046 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010047 }
10048
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010049 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010050}
10051
10052/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010053 * Write application data, doing 1/n-1 splitting if necessary.
10054 *
10055 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010056 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010057 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010060static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010061 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010062{
10063 int ret;
10064
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010065 if( ssl->conf->cbc_record_splitting ==
10066 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010067 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10069 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10070 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010071 {
10072 return( ssl_write_real( ssl, buf, len ) );
10073 }
10074
10075 if( ssl->split_done == 0 )
10076 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010077 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010078 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010079 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010080 }
10081
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010082 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10083 return( ret );
10084 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010085
10086 return( ret + 1 );
10087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010089
10090/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010091 * Write application data (public-facing wrapper)
10092 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010093int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010094{
10095 int ret;
10096
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010098
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010099 if( ssl == NULL || ssl->conf == NULL )
10100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10101
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010102#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010103 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10104 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010105 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010106 return( ret );
10107 }
10108#endif
10109
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010110 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010111 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010112 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010113 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010115 return( ret );
10116 }
10117 }
10118
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010119#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010120 ret = ssl_write_split( ssl, buf, len );
10121#else
10122 ret = ssl_write_real( ssl, buf, len );
10123#endif
10124
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010126
10127 return( ret );
10128}
10129
10130/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010131 * Notify the peer that the connection is being closed
10132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010134{
10135 int ret;
10136
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010137 if( ssl == NULL || ssl->conf == NULL )
10138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010141
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010142 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010143 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010145 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010147 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10148 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10149 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010152 return( ret );
10153 }
10154 }
10155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010157
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010158 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010159}
10160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010161void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010162{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010163 if( transform == NULL )
10164 return;
10165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010166#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010167 deflateEnd( &transform->ctx_deflate );
10168 inflateEnd( &transform->ctx_inflate );
10169#endif
10170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10172 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010173
Hanno Becker92231322018-01-03 15:32:51 +000010174#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010175 mbedtls_md_free( &transform->md_ctx_enc );
10176 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010177#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010178
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010179 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010180}
10181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182#if defined(MBEDTLS_X509_CRT_PARSE_C)
10183static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010186
10187 while( cur != NULL )
10188 {
10189 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010191 cur = next;
10192 }
10193}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010195
Hanno Becker0271f962018-08-16 13:23:47 +010010196#if defined(MBEDTLS_SSL_PROTO_DTLS)
10197
10198static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10199{
10200 unsigned offset;
10201 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10202
10203 if( hs == NULL )
10204 return;
10205
Hanno Becker283f5ef2018-08-24 09:34:47 +010010206 ssl_free_buffered_record( ssl );
10207
Hanno Becker0271f962018-08-16 13:23:47 +010010208 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010209 ssl_buffering_free_slot( ssl, offset );
10210}
10211
10212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10213 uint8_t slot )
10214{
10215 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10216 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010217
10218 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10219 return;
10220
Hanno Beckere605b192018-08-21 15:59:07 +010010221 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010222 {
Hanno Beckere605b192018-08-21 15:59:07 +010010223 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010224 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010225 mbedtls_free( hs_buf->data );
10226 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010227 }
10228}
10229
10230#endif /* MBEDTLS_SSL_PROTO_DTLS */
10231
Gilles Peskine9b562d52018-04-25 20:32:43 +020010232void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010233{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010234 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10235
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010236 if( handshake == NULL )
10237 return;
10238
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010239#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10240 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10241 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010242 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010243 handshake->async_in_progress = 0;
10244 }
10245#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10246
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010247#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10248 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10249 mbedtls_md5_free( &handshake->fin_md5 );
10250 mbedtls_sha1_free( &handshake->fin_sha1 );
10251#endif
10252#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10253#if defined(MBEDTLS_SHA256_C)
10254 mbedtls_sha256_free( &handshake->fin_sha256 );
10255#endif
10256#if defined(MBEDTLS_SHA512_C)
10257 mbedtls_sha512_free( &handshake->fin_sha512 );
10258#endif
10259#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261#if defined(MBEDTLS_DHM_C)
10262 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264#if defined(MBEDTLS_ECDH_C)
10265 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010266#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010267#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010268 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010269#if defined(MBEDTLS_SSL_CLI_C)
10270 mbedtls_free( handshake->ecjpake_cache );
10271 handshake->ecjpake_cache = NULL;
10272 handshake->ecjpake_cache_len = 0;
10273#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010274#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010275
Janos Follath4ae5c292016-02-10 11:27:43 +000010276#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10277 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010278 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010279 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010280#endif
10281
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010282#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10283 if( handshake->psk != NULL )
10284 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010285 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010286 mbedtls_free( handshake->psk );
10287 }
10288#endif
10289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010290#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10291 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010292 /*
10293 * Free only the linked list wrapper, not the keys themselves
10294 * since the belong to the SNI callback
10295 */
10296 if( handshake->sni_key_cert != NULL )
10297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010299
10300 while( cur != NULL )
10301 {
10302 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010304 cur = next;
10305 }
10306 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010307#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010308
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010309#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010310 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010311 if( handshake->ecrs_peer_cert != NULL )
10312 {
10313 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10314 mbedtls_free( handshake->ecrs_peer_cert );
10315 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010316#endif
10317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318#if defined(MBEDTLS_SSL_PROTO_DTLS)
10319 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010320 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010321 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010322#endif
10323
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010324 mbedtls_platform_zeroize( handshake,
10325 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010326}
10327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010329{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010330 if( session == NULL )
10331 return;
10332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010333#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010334 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010335#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010336
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010337#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010339#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010340
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010341 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010342}
10343
Paul Bakker5121ce52009-01-03 21:22:43 +000010344/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010345 * Serialize a full SSL context
10346 */
10347int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10348 unsigned char *buf,
10349 size_t buf_len,
10350 size_t *olen )
10351{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010352 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010353 (void) ssl;
10354
10355 if( buf != NULL )
10356 memset( buf, 0, buf_len );
10357
10358 *olen = 0;
10359
10360 return( 0 );
10361}
10362
10363/*
10364 * Deserialize a full SSL context
10365 */
10366int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10367 const unsigned char *buf,
10368 size_t len )
10369{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010370 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010371 (void) ssl;
10372 (void) buf;
10373 (void) len;
10374
10375 return( 0 );
10376}
10377
10378/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010379 * Free an SSL context
10380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010382{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010383 if( ssl == NULL )
10384 return;
10385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010387
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010388 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010389 {
Angus Grattond8213d02016-05-25 20:56:48 +100010390 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010392 }
10393
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010394 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010395 {
Angus Grattond8213d02016-05-25 20:56:48 +100010396 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010398 }
10399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010401 if( ssl->compress_buf != NULL )
10402 {
Angus Grattond8213d02016-05-25 20:56:48 +100010403 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010405 }
10406#endif
10407
Paul Bakker48916f92012-09-16 19:57:18 +000010408 if( ssl->transform )
10409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410 mbedtls_ssl_transform_free( ssl->transform );
10411 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010412 }
10413
10414 if( ssl->handshake )
10415 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010416 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10418 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420 mbedtls_free( ssl->handshake );
10421 mbedtls_free( ssl->transform_negotiate );
10422 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010423 }
10424
Paul Bakkerc0463502013-02-14 11:19:38 +010010425 if( ssl->session )
10426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427 mbedtls_ssl_session_free( ssl->session );
10428 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010429 }
10430
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010431#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010432 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010433 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010434 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010436 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010437#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10440 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10443 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010444 }
10445#endif
10446
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010447#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010449#endif
10450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010452
Paul Bakker86f04f42013-02-14 11:20:09 +010010453 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010454 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010455}
10456
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010457/*
10458 * Initialze mbedtls_ssl_config
10459 */
10460void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10461{
10462 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010463
10464#if !defined(MBEDTLS_SSL_PROTO_TLS)
10465 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10466#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010467}
10468
Simon Butcherc97b6972015-12-27 23:48:17 +000010469#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010470static int ssl_preset_default_hashes[] = {
10471#if defined(MBEDTLS_SHA512_C)
10472 MBEDTLS_MD_SHA512,
10473 MBEDTLS_MD_SHA384,
10474#endif
10475#if defined(MBEDTLS_SHA256_C)
10476 MBEDTLS_MD_SHA256,
10477 MBEDTLS_MD_SHA224,
10478#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010479#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010480 MBEDTLS_MD_SHA1,
10481#endif
10482 MBEDTLS_MD_NONE
10483};
Simon Butcherc97b6972015-12-27 23:48:17 +000010484#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010485
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010486static int ssl_preset_suiteb_ciphersuites[] = {
10487 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10488 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10489 0
10490};
10491
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010492#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010493static int ssl_preset_suiteb_hashes[] = {
10494 MBEDTLS_MD_SHA256,
10495 MBEDTLS_MD_SHA384,
10496 MBEDTLS_MD_NONE
10497};
10498#endif
10499
10500#if defined(MBEDTLS_ECP_C)
10501static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10502 MBEDTLS_ECP_DP_SECP256R1,
10503 MBEDTLS_ECP_DP_SECP384R1,
10504 MBEDTLS_ECP_DP_NONE
10505};
10506#endif
10507
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010508/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010509 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010510 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010511int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010512 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010513{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010514#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010515 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010516#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010517
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010518 /* Use the functions here so that they are covered in tests,
10519 * but otherwise access member directly for efficiency */
10520 mbedtls_ssl_conf_endpoint( conf, endpoint );
10521 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010522
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010523 /*
10524 * Things that are common to all presets
10525 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010526#if defined(MBEDTLS_SSL_CLI_C)
10527 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10528 {
10529 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10530#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10531 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10532#endif
10533 }
10534#endif
10535
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010536#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010537 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010538#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010539
10540#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10541 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10542#endif
10543
10544#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10545 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010546 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010547 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010548#endif
10549
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010550#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10551 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10552#endif
10553
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010554#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010555 conf->f_cookie_write = ssl_cookie_write_dummy;
10556 conf->f_cookie_check = ssl_cookie_check_dummy;
10557#endif
10558
10559#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10560 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10561#endif
10562
Janos Follath088ce432017-04-10 12:42:31 +010010563#if defined(MBEDTLS_SSL_SRV_C)
10564 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10565#endif
10566
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010567#if defined(MBEDTLS_SSL_PROTO_DTLS)
10568 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10569 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10570#endif
10571
10572#if defined(MBEDTLS_SSL_RENEGOTIATION)
10573 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010574 memset( conf->renego_period, 0x00, 2 );
10575 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010576#endif
10577
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010578#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10579 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10580 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010581 const unsigned char dhm_p[] =
10582 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10583 const unsigned char dhm_g[] =
10584 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10585
Hanno Beckera90658f2017-10-04 15:29:08 +010010586 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10587 dhm_p, sizeof( dhm_p ),
10588 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010589 {
10590 return( ret );
10591 }
10592 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010593#endif
10594
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010595 /*
10596 * Preset-specific defaults
10597 */
10598 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010599 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010600 /*
10601 * NSA Suite B
10602 */
10603 case MBEDTLS_SSL_PRESET_SUITEB:
10604 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10605 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10606 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10607 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10608
10609 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10610 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10611 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10612 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10613 ssl_preset_suiteb_ciphersuites;
10614
10615#if defined(MBEDTLS_X509_CRT_PARSE_C)
10616 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010617#endif
10618
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010619#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010620 conf->sig_hashes = ssl_preset_suiteb_hashes;
10621#endif
10622
10623#if defined(MBEDTLS_ECP_C)
10624 conf->curve_list = ssl_preset_suiteb_curves;
10625#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010626 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010627
10628 /*
10629 * Default
10630 */
10631 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010632 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10633 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10634 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10635 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10636 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10637 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10638 MBEDTLS_SSL_MIN_MINOR_VERSION :
10639 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010640 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10641 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10642
10643#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010644 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010645 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10646#endif
10647
10648 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10649 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10650 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10651 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10652 mbedtls_ssl_list_ciphersuites();
10653
10654#if defined(MBEDTLS_X509_CRT_PARSE_C)
10655 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10656#endif
10657
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010658#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010659 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010660#endif
10661
10662#if defined(MBEDTLS_ECP_C)
10663 conf->curve_list = mbedtls_ecp_grp_id_list();
10664#endif
10665
10666#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10667 conf->dhm_min_bitlen = 1024;
10668#endif
10669 }
10670
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010671 return( 0 );
10672}
10673
10674/*
10675 * Free mbedtls_ssl_config
10676 */
10677void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10678{
10679#if defined(MBEDTLS_DHM_C)
10680 mbedtls_mpi_free( &conf->dhm_P );
10681 mbedtls_mpi_free( &conf->dhm_G );
10682#endif
10683
10684#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10685 if( conf->psk != NULL )
10686 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010687 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010688 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010689 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010690 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010691 }
10692
10693 if( conf->psk_identity != NULL )
10694 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010695 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010696 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010697 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010698 conf->psk_identity_len = 0;
10699 }
10700#endif
10701
10702#if defined(MBEDTLS_X509_CRT_PARSE_C)
10703 ssl_key_cert_free( conf->key_cert );
10704#endif
10705
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010706 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010707}
10708
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010709#if defined(MBEDTLS_PK_C) && \
10710 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010711/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010712 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010715{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716#if defined(MBEDTLS_RSA_C)
10717 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10718 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720#if defined(MBEDTLS_ECDSA_C)
10721 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10722 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010725}
10726
Hanno Becker7e5437a2017-04-28 17:15:26 +010010727unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10728{
10729 switch( type ) {
10730 case MBEDTLS_PK_RSA:
10731 return( MBEDTLS_SSL_SIG_RSA );
10732 case MBEDTLS_PK_ECDSA:
10733 case MBEDTLS_PK_ECKEY:
10734 return( MBEDTLS_SSL_SIG_ECDSA );
10735 default:
10736 return( MBEDTLS_SSL_SIG_ANON );
10737 }
10738}
10739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010741{
10742 switch( sig )
10743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010744#if defined(MBEDTLS_RSA_C)
10745 case MBEDTLS_SSL_SIG_RSA:
10746 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748#if defined(MBEDTLS_ECDSA_C)
10749 case MBEDTLS_SSL_SIG_ECDSA:
10750 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010751#endif
10752 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010754 }
10755}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010756#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010757
Hanno Becker7e5437a2017-04-28 17:15:26 +010010758#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10759 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10760
10761/* Find an entry in a signature-hash set matching a given hash algorithm. */
10762mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10763 mbedtls_pk_type_t sig_alg )
10764{
10765 switch( sig_alg )
10766 {
10767 case MBEDTLS_PK_RSA:
10768 return( set->rsa );
10769 case MBEDTLS_PK_ECDSA:
10770 return( set->ecdsa );
10771 default:
10772 return( MBEDTLS_MD_NONE );
10773 }
10774}
10775
10776/* Add a signature-hash-pair to a signature-hash set */
10777void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10778 mbedtls_pk_type_t sig_alg,
10779 mbedtls_md_type_t md_alg )
10780{
10781 switch( sig_alg )
10782 {
10783 case MBEDTLS_PK_RSA:
10784 if( set->rsa == MBEDTLS_MD_NONE )
10785 set->rsa = md_alg;
10786 break;
10787
10788 case MBEDTLS_PK_ECDSA:
10789 if( set->ecdsa == MBEDTLS_MD_NONE )
10790 set->ecdsa = md_alg;
10791 break;
10792
10793 default:
10794 break;
10795 }
10796}
10797
10798/* Allow exactly one hash algorithm for each signature. */
10799void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10800 mbedtls_md_type_t md_alg )
10801{
10802 set->rsa = md_alg;
10803 set->ecdsa = md_alg;
10804}
10805
10806#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10807 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10808
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010809/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010810 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010812mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010813{
10814 switch( hash )
10815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816#if defined(MBEDTLS_MD5_C)
10817 case MBEDTLS_SSL_HASH_MD5:
10818 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010819#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010820#if defined(MBEDTLS_SHA1_C)
10821 case MBEDTLS_SSL_HASH_SHA1:
10822 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010823#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010824#if defined(MBEDTLS_SHA256_C)
10825 case MBEDTLS_SSL_HASH_SHA224:
10826 return( MBEDTLS_MD_SHA224 );
10827 case MBEDTLS_SSL_HASH_SHA256:
10828 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010830#if defined(MBEDTLS_SHA512_C)
10831 case MBEDTLS_SSL_HASH_SHA384:
10832 return( MBEDTLS_MD_SHA384 );
10833 case MBEDTLS_SSL_HASH_SHA512:
10834 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010835#endif
10836 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010837 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010838 }
10839}
10840
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010841/*
10842 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10843 */
10844unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10845{
10846 switch( md )
10847 {
10848#if defined(MBEDTLS_MD5_C)
10849 case MBEDTLS_MD_MD5:
10850 return( MBEDTLS_SSL_HASH_MD5 );
10851#endif
10852#if defined(MBEDTLS_SHA1_C)
10853 case MBEDTLS_MD_SHA1:
10854 return( MBEDTLS_SSL_HASH_SHA1 );
10855#endif
10856#if defined(MBEDTLS_SHA256_C)
10857 case MBEDTLS_MD_SHA224:
10858 return( MBEDTLS_SSL_HASH_SHA224 );
10859 case MBEDTLS_MD_SHA256:
10860 return( MBEDTLS_SSL_HASH_SHA256 );
10861#endif
10862#if defined(MBEDTLS_SHA512_C)
10863 case MBEDTLS_MD_SHA384:
10864 return( MBEDTLS_SSL_HASH_SHA384 );
10865 case MBEDTLS_MD_SHA512:
10866 return( MBEDTLS_SSL_HASH_SHA512 );
10867#endif
10868 default:
10869 return( MBEDTLS_SSL_HASH_NONE );
10870 }
10871}
10872
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010873#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010874/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010875 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010876 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010877 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010878int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010880 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010881
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010882 if( ssl->conf->curve_list == NULL )
10883 return( -1 );
10884
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010885 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010886 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010887 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010888
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010889 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010890}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010891#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010892
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010893#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010894/*
10895 * Check if a hash proposed by the peer is in our list.
10896 * Return 0 if we're willing to use it, -1 otherwise.
10897 */
10898int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10899 mbedtls_md_type_t md )
10900{
10901 const int *cur;
10902
10903 if( ssl->conf->sig_hashes == NULL )
10904 return( -1 );
10905
10906 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10907 if( *cur == (int) md )
10908 return( 0 );
10909
10910 return( -1 );
10911}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010912#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914#if defined(MBEDTLS_X509_CRT_PARSE_C)
10915int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10916 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010917 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010918 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010919{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010920 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010922 int usage = 0;
10923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010925 const char *ext_oid;
10926 size_t ext_len;
10927#endif
10928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10930 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010931 ((void) cert);
10932 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010933 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010934#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10937 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010938 {
10939 /* Server part of the key exchange */
10940 switch( ciphersuite->key_exchange )
10941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010942 case MBEDTLS_KEY_EXCHANGE_RSA:
10943 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010944 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010945 break;
10946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010947 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10948 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10949 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10950 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010951 break;
10952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10954 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010955 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010956 break;
10957
10958 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959 case MBEDTLS_KEY_EXCHANGE_NONE:
10960 case MBEDTLS_KEY_EXCHANGE_PSK:
10961 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10962 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010963 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010964 usage = 0;
10965 }
10966 }
10967 else
10968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010969 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10970 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010971 }
10972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010973 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010974 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010975 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010976 ret = -1;
10977 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010978#else
10979 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010980#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010982#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10983 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010985 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10986 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010987 }
10988 else
10989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10991 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010992 }
10993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010994 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010995 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010996 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010997 ret = -1;
10998 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010999#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011000
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011001 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011004
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011005/*
11006 * Convert version numbers to/from wire format
11007 * and, for DTLS, to/from TLS equivalent.
11008 *
11009 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011010 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011011 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11012 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011015 unsigned char ver[2] )
11016{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011017#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11018 ((void) transport);
11019#endif
11020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011021#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011022 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011025 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11026
11027 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11028 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11029 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011030 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011031#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011032#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011033 {
11034 ver[0] = (unsigned char) major;
11035 ver[1] = (unsigned char) minor;
11036 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011037#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011038}
11039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011041 const unsigned char ver[2] )
11042{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011043#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11044 ((void) transport);
11045#endif
11046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011048 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011049 {
11050 *major = 255 - ver[0] + 2;
11051 *minor = 255 - ver[1] + 1;
11052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011053 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011054 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11055 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011056 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011057#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011058#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011059 {
11060 *major = ver[0];
11061 *minor = ver[1];
11062 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011063#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011064}
11065
Simon Butcher99000142016-10-13 17:21:01 +010011066int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11067{
11068#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11069 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11070 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11071
11072 switch( md )
11073 {
11074#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11075#if defined(MBEDTLS_MD5_C)
11076 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011077 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011078#endif
11079#if defined(MBEDTLS_SHA1_C)
11080 case MBEDTLS_SSL_HASH_SHA1:
11081 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11082 break;
11083#endif
11084#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11085#if defined(MBEDTLS_SHA512_C)
11086 case MBEDTLS_SSL_HASH_SHA384:
11087 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11088 break;
11089#endif
11090#if defined(MBEDTLS_SHA256_C)
11091 case MBEDTLS_SSL_HASH_SHA256:
11092 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11093 break;
11094#endif
11095 default:
11096 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11097 }
11098
11099 return 0;
11100#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11101 (void) ssl;
11102 (void) md;
11103
11104 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11105#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11106}
11107
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011108#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11109 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11110int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11111 unsigned char *output,
11112 unsigned char *data, size_t data_len )
11113{
11114 int ret = 0;
11115 mbedtls_md5_context mbedtls_md5;
11116 mbedtls_sha1_context mbedtls_sha1;
11117
11118 mbedtls_md5_init( &mbedtls_md5 );
11119 mbedtls_sha1_init( &mbedtls_sha1 );
11120
11121 /*
11122 * digitally-signed struct {
11123 * opaque md5_hash[16];
11124 * opaque sha_hash[20];
11125 * };
11126 *
11127 * md5_hash
11128 * MD5(ClientHello.random + ServerHello.random
11129 * + ServerParams);
11130 * sha_hash
11131 * SHA(ClientHello.random + ServerHello.random
11132 * + ServerParams);
11133 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011134 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011135 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011137 goto exit;
11138 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011139 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011140 ssl->handshake->randbytes, 64 ) ) != 0 )
11141 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011143 goto exit;
11144 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011145 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011146 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011148 goto exit;
11149 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011150 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011151 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011153 goto exit;
11154 }
11155
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011156 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011157 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011159 goto exit;
11160 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011161 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011162 ssl->handshake->randbytes, 64 ) ) != 0 )
11163 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011165 goto exit;
11166 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011167 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011168 data_len ) ) != 0 )
11169 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011171 goto exit;
11172 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011173 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011174 output + 16 ) ) != 0 )
11175 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 goto exit;
11178 }
11179
11180exit:
11181 mbedtls_md5_free( &mbedtls_md5 );
11182 mbedtls_sha1_free( &mbedtls_sha1 );
11183
11184 if( ret != 0 )
11185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11186 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11187
11188 return( ret );
11189
11190}
11191#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11192 MBEDTLS_SSL_PROTO_TLS1_1 */
11193
11194#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11195 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11196int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011197 unsigned char *hash, size_t *hashlen,
11198 unsigned char *data, size_t data_len,
11199 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011200{
11201 int ret = 0;
11202 mbedtls_md_context_t ctx;
11203 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011204 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011205
11206 mbedtls_md_init( &ctx );
11207
11208 /*
11209 * digitally-signed struct {
11210 * opaque client_random[32];
11211 * opaque server_random[32];
11212 * ServerDHParams params;
11213 * };
11214 */
11215 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11216 {
11217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11218 goto exit;
11219 }
11220 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11221 {
11222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11223 goto exit;
11224 }
11225 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11226 {
11227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11228 goto exit;
11229 }
11230 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11231 {
11232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11233 goto exit;
11234 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011235 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011236 {
11237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11238 goto exit;
11239 }
11240
11241exit:
11242 mbedtls_md_free( &ctx );
11243
11244 if( ret != 0 )
11245 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11246 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11247
11248 return( ret );
11249}
11250#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11251 MBEDTLS_SSL_PROTO_TLS1_2 */
11252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011253#endif /* MBEDTLS_SSL_TLS_C */