blob: 912fdec8023f749d4c5f68232398c753beaa0105 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200371#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200378 if( src->peer_cert != NULL )
379 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200380 int ret;
381
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200382 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200384 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200389 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 dst->peer_cert = NULL;
393 return( ret );
394 }
395 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200397
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200398#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399 if( src->ticket != NULL )
400 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200401 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200402 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200403 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200404
405 memcpy( dst->ticket, src->ticket, src->ticket_len );
406 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200407#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200408
409 return( 0 );
410}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200411#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
414int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200415 const unsigned char *key_enc, const unsigned char *key_dec,
416 size_t keylen,
417 const unsigned char *iv_enc, const unsigned char *iv_dec,
418 size_t ivlen,
419 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200420 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
422int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
423int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
424int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
425int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
426#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000427
Paul Bakker5121ce52009-01-03 21:22:43 +0000428/*
429 * Key material generation
430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200432static int ssl3_prf( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000435 unsigned char *dstbuf, size_t dlen )
436{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100437 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000438 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200439 mbedtls_md5_context md5;
440 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000441 unsigned char padding[16];
442 unsigned char sha1sum[20];
443 ((void)label);
444
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200445 mbedtls_md5_init( &md5 );
446 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200447
Paul Bakker5f70b252012-09-13 14:23:06 +0000448 /*
449 * SSLv3:
450 * block =
451 * MD5( secret + SHA1( 'A' + secret + random ) ) +
452 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
453 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
454 * ...
455 */
456 for( i = 0; i < dlen / 16; i++ )
457 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200458 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000459
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100461 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100463 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100464 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100465 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000470
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100473 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100474 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000479 }
480
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200482 mbedtls_md5_free( &md5 );
483 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000484
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500485 mbedtls_platform_zeroize( padding, sizeof( padding ) );
486 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000487
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200493static int tls1_prf( const unsigned char *secret, size_t slen,
494 const char *label,
495 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000496 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000497{
Paul Bakker23986e52011-04-24 08:57:21 +0000498 size_t nb, hs;
499 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200500 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 unsigned char tmp[128];
502 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 const mbedtls_md_info_t *md_info;
504 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100505 int ret;
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 hs = ( slen + 1 ) / 2;
513 S1 = secret;
514 S2 = secret + slen - hs;
515
516 nb = strlen( label );
517 memcpy( tmp + 20, label, nb );
518 memcpy( tmp + 20 + nb, random, rlen );
519 nb += rlen;
520
521 /*
522 * First compute P_md5(secret,label+random)[0..dlen]
523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100528 return( ret );
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
531 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
532 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534 for( i = 0; i < dlen; i += 16 )
535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_md_hmac_reset ( &md_ctx );
537 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
538 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_md_hmac_reset ( &md_ctx );
541 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
542 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
545
546 for( j = 0; j < k; j++ )
547 dstbuf[i + j] = h_i[j];
548 }
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100551
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 /*
553 * XOR out with P_sha1(secret,label+random)[0..dlen]
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100559 return( ret );
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
562 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
563 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565 for( i = 0; i < dlen; i += 20 )
566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_md_hmac_reset ( &md_ctx );
568 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
569 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_md_hmac_reset ( &md_ctx );
572 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
573 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
576
577 for( j = 0; j < k; j++ )
578 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
579 }
580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100582
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500583 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
584 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 return( 0 );
587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
591static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100592 const unsigned char *secret, size_t slen,
593 const char *label,
594 const unsigned char *random, size_t rlen,
595 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000596{
597 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100598 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000599 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
601 const mbedtls_md_info_t *md_info;
602 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100603 int ret;
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100611
612 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614
615 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100616 memcpy( tmp + md_len, label, nb );
617 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000618 nb += rlen;
619
620 /*
621 * Compute P_<hash>(secret, label + random)[0..dlen]
622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100624 return( ret );
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
627 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
628 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100629
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100630 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
634 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_md_hmac_reset ( &md_ctx );
637 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
638 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000639
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100640 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000641
642 for( j = 0; j < k; j++ )
643 dstbuf[i + j] = h_i[j];
644 }
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100647
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500648 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
649 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000650
651 return( 0 );
652}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100655static int tls_prf_sha256( const unsigned char *secret, size_t slen,
656 const char *label,
657 const unsigned char *random, size_t rlen,
658 unsigned char *dstbuf, size_t dlen )
659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100661 label, random, rlen, dstbuf, dlen ) );
662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200666static int tls_prf_sha384( const unsigned char *secret, size_t slen,
667 const char *label,
668 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000669 unsigned char *dstbuf, size_t dlen )
670{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100672 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#endif /* MBEDTLS_SHA512_C */
675#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
680 defined(MBEDTLS_SSL_PROTO_TLS1_1)
681static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200682#endif
Paul Bakker380da532012-04-18 16:10:25 +0000683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200685static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200687#endif
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200690static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200692#endif
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
695#if defined(MBEDTLS_SHA256_C)
696static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200697static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200699#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#if defined(MBEDTLS_SHA512_C)
702static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200703static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000707
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200708/* Type for the TLS PRF */
709typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
710 const unsigned char *, size_t,
711 unsigned char *, size_t);
712
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200713/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200714 * Populate a transform structure with session keys and all the other
715 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200716 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200717 * Parameters:
718 * - [in/out]: transform: structure to populate
719 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200720 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200721 * - [in] ciphersuite
722 * - [in] master
723 * - [in] encrypt_then_mac
724 * - [in] trunc_hmac
725 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200726 * - [in] tls_prf: pointer to PRF to use for key derivation
727 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200728 * - [in] minor_ver: SSL/TLS minor version
729 * - [in] endpoint: client or server
730 * - [in] ssl: optionally used for:
731 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
732 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
733 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200734 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200735static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200736 int ciphersuite,
737 const unsigned char master[48],
738#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
739 int encrypt_then_mac,
740#endif
741#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
742 int trunc_hmac,
743#endif
744#if defined(MBEDTLS_ZLIB_SUPPORT)
745 int compression,
746#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200747 ssl_tls_prf_t tls_prf,
748 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200749 int minor_ver,
750 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000752{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200753 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 unsigned char keyblk[256];
755 unsigned char *key1;
756 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100757 unsigned char *mac_enc;
758 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000759 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200760 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000761 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000762 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 const mbedtls_cipher_info_t *cipher_info;
764 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100765
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200766#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
767 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
768 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200769 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200770 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000771#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000772
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200773 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200775 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000776#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200777 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200779 /*
780 * Get various info structures
781 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200782 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200783 if( ciphersuite_info == NULL )
784 {
785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200786 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
788 }
789
Hanno Becker8759e162017-12-27 21:34:08 +0000790 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100791 if( cipher_info == NULL )
792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000794 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100796 }
797
Hanno Becker8759e162017-12-27 21:34:08 +0000798 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100799 if( md_info == NULL )
800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000802 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100804 }
805
Hanno Beckera5a2b082019-05-15 14:03:01 +0100806#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100807 /* Copy own and peer's CID if the use of the CID
808 * extension has been negotiated. */
809 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
810 {
811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100812
Hanno Becker4932f9f2019-05-03 15:23:51 +0100813 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100814 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100815 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100816 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100817
818 transform->out_cid_len = ssl->handshake->peer_cid_len;
819 memcpy( transform->out_cid, ssl->handshake->peer_cid,
820 ssl->handshake->peer_cid_len );
821 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
822 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100823 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100824#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100825
Paul Bakker5121ce52009-01-03 21:22:43 +0000826 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200827 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200829 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100830 if( ret != 0 )
831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100833 return( ret );
834 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200837 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200838 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200839 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 /*
843 * Determine the appropriate key, IV and MAC length.
844 */
Paul Bakker68884e32013-01-07 18:20:04 +0100845
Hanno Beckere7f2df02017-12-27 08:17:40 +0000846 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200847
Hanno Beckerf1229442018-01-03 15:32:31 +0000848#if defined(MBEDTLS_GCM_C) || \
849 defined(MBEDTLS_CCM_C) || \
850 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200852 cipher_info->mode == MBEDTLS_MODE_CCM ||
853 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 {
Hanno Becker8759e162017-12-27 21:34:08 +0000855 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200856
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200857 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000858 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000859 transform->taglen =
860 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200861
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200862 /* All modes haves 96-bit IVs;
863 * GCM and CCM has 4 implicit and 8 explicit bytes
864 * ChachaPoly has all 12 bytes implicit
865 */
Paul Bakker68884e32013-01-07 18:20:04 +0100866 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
868 transform->fixed_ivlen = 12;
869 else
870 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200871
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200872 /* Minimum length of encrypted record */
873 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000874 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100875 }
876 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000877#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
878#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
879 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
880 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100881 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200882 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
884 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200887 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100888 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000891 mac_key_len = mbedtls_md_get_size( md_info );
892 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200895 /*
896 * If HMAC is to be truncated, we shall keep the leftmost bytes,
897 * (rfc 6066 page 13 or rfc 2104 section 4),
898 * so we only need to adjust the length here.
899 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200900 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000903
904#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
905 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000906 * HMAC implementation which also truncates the key
907 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000908 mac_key_len = transform->maclen;
909#endif
910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
913 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100914 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200916 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200918 transform->minlen = transform->maclen;
919 else
Paul Bakker68884e32013-01-07 18:20:04 +0100920 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200921 /*
922 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100923 * 1. if EtM is in use: one block plus MAC
924 * otherwise: * first multiple of blocklen greater than maclen
925 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200928 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100929 {
930 transform->minlen = transform->maclen
931 + cipher_info->block_size;
932 }
933 else
934#endif
935 {
936 transform->minlen = transform->maclen
937 + cipher_info->block_size
938 - transform->maclen % cipher_info->block_size;
939 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200942 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
943 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200944 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100945 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200948 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
949 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200950 {
951 transform->minlen += transform->ivlen;
952 }
953 else
954#endif
955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
957 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200958 }
Paul Bakker68884e32013-01-07 18:20:04 +0100959 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000961 else
962#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
963 {
964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
966 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Hanno Beckere7f2df02017-12-27 08:17:40 +0000968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
969 (unsigned) keylen,
970 (unsigned) transform->minlen,
971 (unsigned) transform->ivlen,
972 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
974 /*
975 * Finally setup the cipher contexts, IVs and MAC secrets.
976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200978 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000980 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000981 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Paul Bakker68884e32013-01-07 18:20:04 +0100983 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000984 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000986 /*
987 * This is not used in TLS v1.1.
988 */
Paul Bakker48916f92012-09-16 19:57:18 +0000989 iv_copy_len = ( transform->fixed_ivlen ) ?
990 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000991 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
992 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000993 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 }
995 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#endif /* MBEDTLS_SSL_CLI_C */
997#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200998 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001000 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001001 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001004 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001005
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001006 /*
1007 * This is not used in TLS v1.1.
1008 */
Paul Bakker48916f92012-09-16 19:57:18 +00001009 iv_copy_len = ( transform->fixed_ivlen ) ?
1010 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001011 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1012 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001013 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001015 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker92231322018-01-03 15:32:51 +00001022#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001024 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001025 {
Hanno Becker92231322018-01-03 15:32:51 +00001026 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001030 }
1031
Hanno Becker81c7b182017-11-09 18:39:33 +00001032 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1033 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001034 }
1035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1037#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1038 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001039 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001040 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001041 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1042 For AEAD-based ciphersuites, there is nothing to do here. */
1043 if( mac_key_len != 0 )
1044 {
1045 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1046 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1047 }
Paul Bakker68884e32013-01-07 18:20:04 +01001048 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001049 else
1050#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001054 }
Hanno Becker92231322018-01-03 15:32:51 +00001055#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1058 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001059 {
1060 int ret = 0;
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001063
Hanno Beckere7f2df02017-12-27 08:17:40 +00001064 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001065 transform->iv_enc, transform->iv_dec,
1066 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001067 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001068 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1071 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001072 }
1073 }
Hanno Becker92231322018-01-03 15:32:51 +00001074#else
1075 ((void) mac_dec);
1076 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001078
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001079#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1080 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001081 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001082 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001083 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001084 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001085 iv_copy_len );
1086 }
1087#endif
1088
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001089 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001093 return( ret );
1094 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001095
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001096 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001097 cipher_info ) ) != 0 )
1098 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001100 return( ret );
1101 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001104 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001108 return( ret );
1109 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001112 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 return( ret );
1117 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119#if defined(MBEDTLS_CIPHER_MODE_CBC)
1120 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1123 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001126 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001127 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1130 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001133 return( ret );
1134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001138 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001140 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001142 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001145
Paul Bakker48916f92012-09-16 19:57:18 +00001146 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1147 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001148
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001149 if( deflateInit( &transform->ctx_deflate,
1150 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001151 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1154 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001155 }
1156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001158
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 return( 0 );
1160}
1161
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001162/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001163 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001164 *
1165 * Inputs:
1166 * - SSL/TLS minor version
1167 * - hash associated with the ciphersuite (only used by TLS 1.2)
1168 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001169 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001170 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1171 */
1172static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1173 int minor_ver,
1174 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001175{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001176#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1177 (void) hash;
1178#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001179
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001180#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001181 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001182 {
1183 handshake->tls_prf = ssl3_prf;
1184 handshake->calc_verify = ssl_calc_verify_ssl;
1185 handshake->calc_finished = ssl_calc_finished_ssl;
1186 }
1187 else
1188#endif
1189#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001190 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001191 {
1192 handshake->tls_prf = tls1_prf;
1193 handshake->calc_verify = ssl_calc_verify_tls;
1194 handshake->calc_finished = ssl_calc_finished_tls;
1195 }
1196 else
1197#endif
1198#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1199#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1201 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001202 {
1203 handshake->tls_prf = tls_prf_sha384;
1204 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1205 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1206 }
1207 else
1208#endif
1209#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001210 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001211 {
1212 handshake->tls_prf = tls_prf_sha256;
1213 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1214 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1215 }
1216 else
1217#endif
1218#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1219 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1221 }
1222
1223 return( 0 );
1224}
1225
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001226/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001227 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001228 *
1229 * Parameters:
1230 * [in/out] handshake
1231 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1232 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001233 * [out] master
1234 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001235 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001236static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001237 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001238 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001239{
1240 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001241
1242#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001243 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001244 (void) ssl;
1245#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001246
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001247 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001248 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1250 return( 0 );
1251 }
1252
1253 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1254 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001255
1256#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001257 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001258 {
1259 unsigned char session_hash[48];
1260 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001261
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001262 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001264 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1265 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001266
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001267 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268 "extended master secret",
1269 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001270 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001271 }
1272 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001273#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001274 {
1275 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1276 "master secret",
1277 handshake->randbytes, 64,
1278 master, 48 );
1279 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001280 if( ret != 0 )
1281 {
1282 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1283 return( ret );
1284 }
1285
1286 mbedtls_platform_zeroize( handshake->premaster,
1287 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001288
1289 return( 0 );
1290}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001291
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001292int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1293{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001294 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001295 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1296 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001297
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1299
1300 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001301 ret = ssl_set_handshake_prfs( ssl->handshake,
1302 ssl->minor_ver,
1303 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001304 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001305 {
1306 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001307 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001308 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001309
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001310 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001311 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001312 ssl->session_negotiate->master,
1313 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001314 if( ret != 0 )
1315 {
1316 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1317 return( ret );
1318 }
1319
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001320 /* Swap the client and server random values:
1321 * - MS derivation wanted client+server (RFC 5246 8.1)
1322 * - key derivation wants server+client (RFC 5246 6.3) */
1323 {
1324 unsigned char tmp[64];
1325 memcpy( tmp, ssl->handshake->randbytes, 64 );
1326 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1327 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1328 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1329 }
1330
1331 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001332 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001333 ssl->session_negotiate->ciphersuite,
1334 ssl->session_negotiate->master,
1335#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1336 ssl->session_negotiate->encrypt_then_mac,
1337#endif
1338#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1339 ssl->session_negotiate->trunc_hmac,
1340#endif
1341#if defined(MBEDTLS_ZLIB_SUPPORT)
1342 ssl->session_negotiate->compression,
1343#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001344 ssl->handshake->tls_prf,
1345 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001346 ssl->minor_ver,
1347 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001348 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001349 if( ret != 0 )
1350 {
1351 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1352 return( ret );
1353 }
1354
1355 /* We no longer need Server/ClientHello.random values */
1356 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1357 sizeof( ssl->handshake->randbytes ) );
1358
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001359 /* Allocate compression buffer */
1360#if defined(MBEDTLS_ZLIB_SUPPORT)
1361 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1362 ssl->compress_buf == NULL )
1363 {
1364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1365 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1366 if( ssl->compress_buf == NULL )
1367 {
1368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001369 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001370 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1371 }
1372 }
1373#endif
1374
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1376
1377 return( 0 );
1378}
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001381void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1382 unsigned char hash[36],
1383 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001385 mbedtls_md5_context md5;
1386 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 unsigned char pad_1[48];
1388 unsigned char pad_2[48];
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001392 mbedtls_md5_init( &md5 );
1393 mbedtls_sha1_init( &sha1 );
1394
1395 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1396 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Paul Bakker380da532012-04-18 16:10:25 +00001398 memset( pad_1, 0x36, 48 );
1399 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001401 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1402 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1403 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001405 mbedtls_md5_starts_ret( &md5 );
1406 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1407 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1408 mbedtls_md5_update_ret( &md5, hash, 16 );
1409 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001411 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1412 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1413 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001414
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001415 mbedtls_sha1_starts_ret( &sha1 );
1416 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1417 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1418 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1419 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001420
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001421 *hlen = 36;
1422
1423 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001425
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001426 mbedtls_md5_free( &md5 );
1427 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001428
Paul Bakker380da532012-04-18 16:10:25 +00001429 return;
1430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001434void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1435 unsigned char hash[36],
1436 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001437{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001438 mbedtls_md5_context md5;
1439 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001443 mbedtls_md5_init( &md5 );
1444 mbedtls_sha1_init( &sha1 );
1445
1446 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1447 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001448
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001449 mbedtls_md5_finish_ret( &md5, hash );
1450 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001452 *hlen = 36;
1453
1454 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001457 mbedtls_md5_free( &md5 );
1458 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001459
Paul Bakker380da532012-04-18 16:10:25 +00001460 return;
1461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1465#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001466void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1467 unsigned char hash[32],
1468 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001469{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001470 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001471
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001472 mbedtls_sha256_init( &sha256 );
1473
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001475
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001476 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001477 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001479 *hlen = 32;
1480
1481 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001484 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001485
Paul Bakker380da532012-04-18 16:10:25 +00001486 return;
1487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001491void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1492 unsigned char hash[48],
1493 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001494{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001496
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001497 mbedtls_sha512_init( &sha512 );
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001501 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001502 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001504 *hlen = 48;
1505
1506 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001509 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001510
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 return;
1512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_SHA512_C */
1514#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1517int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001518{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001519 unsigned char *p = ssl->handshake->premaster;
1520 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001521 const unsigned char *psk = ssl->conf->psk;
1522 size_t psk_len = ssl->conf->psk_len;
1523
1524 /* If the psk callback was called, use its result */
1525 if( ssl->handshake->psk != NULL )
1526 {
1527 psk = ssl->handshake->psk;
1528 psk_len = ssl->handshake->psk_len;
1529 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001530
1531 /*
1532 * PMS = struct {
1533 * opaque other_secret<0..2^16-1>;
1534 * opaque psk<0..2^16-1>;
1535 * };
1536 * with "other_secret" depending on the particular key exchange
1537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1539 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001540 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001541 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001543
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001544 *(p++) = (unsigned char)( psk_len >> 8 );
1545 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001546
1547 if( end < p || (size_t)( end - p ) < psk_len )
1548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1549
1550 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001551 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001552 }
1553 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1555#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1556 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001557 {
1558 /*
1559 * other_secret already set by the ClientKeyExchange message,
1560 * and is 48 bytes long
1561 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001562 if( end - p < 2 )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001565 *p++ = 0;
1566 *p++ = 48;
1567 p += 48;
1568 }
1569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1571#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1572 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001573 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001574 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001575 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001576
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001577 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001579 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001580 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001583 return( ret );
1584 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001585 *(p++) = (unsigned char)( len >> 8 );
1586 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001587 p += len;
1588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001590 }
1591 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1593#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1594 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001595 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001596 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 size_t zlen;
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001600 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001601 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604 return( ret );
1605 }
1606
1607 *(p++) = (unsigned char)( zlen >> 8 );
1608 *(p++) = (unsigned char)( zlen );
1609 p += zlen;
1610
Janos Follath3fbdada2018-08-15 10:26:53 +01001611 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1612 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 }
1614 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1618 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 }
1620
1621 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001622 if( end - p < 2 )
1623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001624
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001625 *(p++) = (unsigned char)( psk_len >> 8 );
1626 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001627
1628 if( end < p || (size_t)( end - p ) < psk_len )
1629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1630
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001631 memcpy( p, psk, psk_len );
1632 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633
1634 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1635
1636 return( 0 );
1637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001641/*
1642 * SSLv3.0 MAC functions
1643 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001644#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001645static void ssl_mac( mbedtls_md_context_t *md_ctx,
1646 const unsigned char *secret,
1647 const unsigned char *buf, size_t len,
1648 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001649 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
1651 unsigned char header[11];
1652 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001653 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1655 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001656
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001657 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001659 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001660 else
Paul Bakker68884e32013-01-07 18:20:04 +01001661 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
1663 memcpy( header, ctr, 8 );
1664 header[ 8] = (unsigned char) type;
1665 header[ 9] = (unsigned char)( len >> 8 );
1666 header[10] = (unsigned char)( len );
1667
Paul Bakker68884e32013-01-07 18:20:04 +01001668 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 mbedtls_md_starts( md_ctx );
1670 mbedtls_md_update( md_ctx, secret, md_size );
1671 mbedtls_md_update( md_ctx, padding, padlen );
1672 mbedtls_md_update( md_ctx, header, 11 );
1673 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001674 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Paul Bakker68884e32013-01-07 18:20:04 +01001676 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_md_starts( md_ctx );
1678 mbedtls_md_update( md_ctx, secret, md_size );
1679 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001680 mbedtls_md_update( md_ctx, out, md_size );
1681 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001684
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001685/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001686 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001687#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001688 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1689 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1690 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1691/* This function makes sure every byte in the memory region is accessed
1692 * (in ascending addresses order) */
1693static void ssl_read_memory( unsigned char *p, size_t len )
1694{
1695 unsigned char acc = 0;
1696 volatile unsigned char force;
1697
1698 for( ; len != 0; p++, len-- )
1699 acc ^= *p;
1700
1701 force = acc;
1702 (void) force;
1703}
1704#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1705
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001706/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001708 */
Hanno Becker3307b532017-12-27 21:37:21 +00001709
Hanno Beckera5a2b082019-05-15 14:03:01 +01001710#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001711/* This functions transforms a DTLS plaintext fragment and a record content
1712 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001713 *
1714 * struct {
1715 * opaque content[DTLSPlaintext.length];
1716 * ContentType real_type;
1717 * uint8 zeros[length_of_padding];
1718 * } DTLSInnerPlaintext;
1719 *
1720 * Input:
1721 * - `content`: The beginning of the buffer holding the
1722 * plaintext to be wrapped.
1723 * - `*content_size`: The length of the plaintext in Bytes.
1724 * - `max_len`: The number of Bytes available starting from
1725 * `content`. This must be `>= *content_size`.
1726 * - `rec_type`: The desired record content type.
1727 *
1728 * Output:
1729 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1730 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1731 *
1732 * Returns:
1733 * - `0` on success.
1734 * - A negative error code if `max_len` didn't offer enough space
1735 * for the expansion.
1736 */
1737static int ssl_cid_build_inner_plaintext( unsigned char *content,
1738 size_t *content_size,
1739 size_t remaining,
1740 uint8_t rec_type )
1741{
1742 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001743 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1744 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1745 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001746
1747 /* Write real content type */
1748 if( remaining == 0 )
1749 return( -1 );
1750 content[ len ] = rec_type;
1751 len++;
1752 remaining--;
1753
1754 if( remaining < pad )
1755 return( -1 );
1756 memset( content + len, 0, pad );
1757 len += pad;
1758 remaining -= pad;
1759
1760 *content_size = len;
1761 return( 0 );
1762}
1763
Hanno Becker7dc25772019-05-20 15:08:01 +01001764/* This function parses a DTLSInnerPlaintext structure.
1765 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001766static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1767 size_t *content_size,
1768 uint8_t *rec_type )
1769{
1770 size_t remaining = *content_size;
1771
1772 /* Determine length of padding by skipping zeroes from the back. */
1773 do
1774 {
1775 if( remaining == 0 )
1776 return( -1 );
1777 remaining--;
1778 } while( content[ remaining ] == 0 );
1779
1780 *content_size = remaining;
1781 *rec_type = content[ remaining ];
1782
1783 return( 0 );
1784}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001785#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001786
Hanno Becker99abf512019-05-20 14:50:53 +01001787/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001788 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001789static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001790 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001791 mbedtls_record *rec )
1792{
Hanno Becker99abf512019-05-20 14:50:53 +01001793 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001794 *
1795 * additional_data = seq_num + TLSCompressed.type +
1796 * TLSCompressed.version + TLSCompressed.length;
1797 *
Hanno Becker99abf512019-05-20 14:50:53 +01001798 * For the CID extension, this is extended as follows
1799 * (quoting draft-ietf-tls-dtls-connection-id-05,
1800 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001801 *
1802 * additional_data = seq_num + DTLSPlaintext.type +
1803 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001804 * cid +
1805 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001806 * length_of_DTLSInnerPlaintext;
1807 */
1808
Hanno Becker3307b532017-12-27 21:37:21 +00001809 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1810 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001811 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001812
Hanno Beckera5a2b082019-05-15 14:03:01 +01001813#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001814 if( rec->cid_len != 0 )
1815 {
1816 memcpy( add_data + 11, rec->cid, rec->cid_len );
1817 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1818 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1819 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1820 *add_data_len = 13 + 1 + rec->cid_len;
1821 }
1822 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001823#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001824 {
1825 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1826 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1827 *add_data_len = 13;
1828 }
Hanno Becker3307b532017-12-27 21:37:21 +00001829}
1830
Hanno Becker611a83b2018-01-03 14:27:32 +00001831int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1832 mbedtls_ssl_transform *transform,
1833 mbedtls_record *rec,
1834 int (*f_rng)(void *, unsigned char *, size_t),
1835 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001838 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001839 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001840 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001841 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001842 size_t post_avail;
1843
1844 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001845#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001846 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001847 ((void) ssl);
1848#endif
1849
1850 /* The PRNG is used for dynamic IV generation that's used
1851 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1852#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1853 ( defined(MBEDTLS_AES_C) || \
1854 defined(MBEDTLS_ARIA_C) || \
1855 defined(MBEDTLS_CAMELLIA_C) ) && \
1856 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1857 ((void) f_rng);
1858 ((void) p_rng);
1859#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Hanno Becker3307b532017-12-27 21:37:21 +00001863 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001864 {
Hanno Becker3307b532017-12-27 21:37:21 +00001865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1867 }
Hanno Becker505089d2019-05-01 09:45:57 +01001868 if( rec == NULL
1869 || rec->buf == NULL
1870 || rec->buf_len < rec->data_offset
1871 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001872#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001873 || rec->cid_len != 0
1874#endif
1875 )
Hanno Becker3307b532017-12-27 21:37:21 +00001876 {
1877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001879 }
1880
Hanno Becker3307b532017-12-27 21:37:21 +00001881 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001882 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001884 data, rec->data_len );
1885
1886 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1887
1888 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1889 {
1890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1891 (unsigned) rec->data_len,
1892 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1894 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001895
Hanno Beckera5a2b082019-05-15 14:03:01 +01001896#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001897 /*
1898 * Add CID information
1899 */
1900 rec->cid_len = transform->out_cid_len;
1901 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1902 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001903
1904 if( rec->cid_len != 0 )
1905 {
1906 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001907 * Wrap plaintext into DTLSInnerPlaintext structure.
1908 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001909 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001910 * Note that this changes `rec->data_len`, and hence
1911 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001912 */
1913 if( ssl_cid_build_inner_plaintext( data,
1914 &rec->data_len,
1915 post_avail,
1916 rec->type ) != 0 )
1917 {
1918 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1919 }
1920
1921 rec->type = MBEDTLS_SSL_MSG_CID;
1922 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001923#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001924
Hanno Becker92c930f2019-04-29 17:31:37 +01001925 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001928 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001930#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 if( mode == MBEDTLS_MODE_STREAM ||
1932 ( mode == MBEDTLS_MODE_CBC
1933#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001934 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001935#endif
1936 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 {
Hanno Becker3307b532017-12-27 21:37:21 +00001938 if( post_avail < transform->maclen )
1939 {
1940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1941 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1942 }
1943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001945 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001946 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001947 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001948 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1949 data, rec->data_len, rec->ctr, rec->type, mac );
1950 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001951 }
1952 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1955 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001956 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001957 {
Hanno Becker992b6872017-11-09 18:57:39 +00001958 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1959
Hanno Beckere83efe62019-04-29 13:52:53 +01001960 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001961
Hanno Becker3307b532017-12-27 21:37:21 +00001962 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001963 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001964 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1965 data, rec->data_len );
1966 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1967 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1968
1969 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 }
1971 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001972#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001976 }
1977
Hanno Becker3307b532017-12-27 21:37:21 +00001978 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1979 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001980
Hanno Becker3307b532017-12-27 21:37:21 +00001981 rec->data_len += transform->maclen;
1982 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001983 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001984 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001985#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001987 /*
1988 * Encrypt
1989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1991 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001993 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001994 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001996 "including %d bytes of padding",
1997 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
Hanno Becker3307b532017-12-27 21:37:21 +00001999 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2000 transform->iv_enc, transform->ivlen,
2001 data, rec->data_len,
2002 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002005 return( ret );
2006 }
2007
Hanno Becker3307b532017-12-27 21:37:21 +00002008 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
Paul Bakker68884e32013-01-07 18:20:04 +01002014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002016
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002017#if defined(MBEDTLS_GCM_C) || \
2018 defined(MBEDTLS_CCM_C) || \
2019 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002021 mode == MBEDTLS_MODE_CCM ||
2022 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002023 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002024 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002025 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002026 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002027
Hanno Becker3307b532017-12-27 21:37:21 +00002028 /* Check that there's space for both the authentication tag
2029 * and the explicit IV before and after the record content. */
2030 if( post_avail < transform->taglen ||
2031 rec->data_offset < explicit_iv_len )
2032 {
2033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2034 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2035 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002036
Paul Bakker68884e32013-01-07 18:20:04 +01002037 /*
2038 * Generate IV
2039 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2041 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002042 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002043 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002044 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2045 explicit_iv_len );
2046 /* Prefix record content with explicit IV. */
2047 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002048 }
2049 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2050 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002051 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002052 unsigned char i;
2053
2054 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2055
2056 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002057 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 }
2059 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002060 {
2061 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002064 }
2065
Hanno Beckere83efe62019-04-29 13:52:53 +01002066 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002067
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002068 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2069 iv, transform->ivlen );
2070 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002071 data - explicit_iv_len, explicit_iv_len );
2072 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002073 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002075 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002076 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002077
Paul Bakker68884e32013-01-07 18:20:04 +01002078 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002079 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002080 */
Hanno Becker3307b532017-12-27 21:37:21 +00002081
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002083 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002084 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002085 data, rec->data_len, /* source */
2086 data, &rec->data_len, /* destination */
2087 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002090 return( ret );
2091 }
2092
Hanno Becker3307b532017-12-27 21:37:21 +00002093 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2094 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002095
Hanno Becker3307b532017-12-27 21:37:21 +00002096 rec->data_len += transform->taglen + explicit_iv_len;
2097 rec->data_offset -= explicit_iv_len;
2098 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002099 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2103#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002104 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002107 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002108 size_t padlen, i;
2109 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002110
Hanno Becker3307b532017-12-27 21:37:21 +00002111 /* Currently we're always using minimal padding
2112 * (up to 255 bytes would be allowed). */
2113 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2114 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 padlen = 0;
2116
Hanno Becker3307b532017-12-27 21:37:21 +00002117 /* Check there's enough space in the buffer for the padding. */
2118 if( post_avail < padlen + 1 )
2119 {
2120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2121 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2122 }
2123
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002125 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Hanno Becker3307b532017-12-27 21:37:21 +00002127 rec->data_len += padlen + 1;
2128 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002132 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2133 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002134 */
Hanno Becker3307b532017-12-27 21:37:21 +00002135 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002136 {
Hanno Becker3307b532017-12-27 21:37:21 +00002137 if( f_rng == NULL )
2138 {
2139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2141 }
2142
2143 if( rec->data_offset < transform->ivlen )
2144 {
2145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2146 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2147 }
2148
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149 /*
2150 * Generate IV
2151 */
Hanno Becker3307b532017-12-27 21:37:21 +00002152 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002153 if( ret != 0 )
2154 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155
Hanno Becker3307b532017-12-27 21:37:21 +00002156 memcpy( data - transform->ivlen, transform->iv_enc,
2157 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002163 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002164 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002165 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
Hanno Becker3307b532017-12-27 21:37:21 +00002167 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2168 transform->iv_enc,
2169 transform->ivlen,
2170 data, rec->data_len,
2171 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002174 return( ret );
2175 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002176
Hanno Becker3307b532017-12-27 21:37:21 +00002177 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002181 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002184 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002185 {
2186 /*
2187 * Save IV in SSL3 and TLS1
2188 */
Hanno Becker3307b532017-12-27 21:37:21 +00002189 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2190 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 }
Hanno Becker3307b532017-12-27 21:37:21 +00002192 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002193#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002194 {
2195 data -= transform->ivlen;
2196 rec->data_offset -= transform->ivlen;
2197 rec->data_len += transform->ivlen;
2198 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002201 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002202 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002203 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2204
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002205 /*
2206 * MAC(MAC_write_key, seq_num +
2207 * TLSCipherText.type +
2208 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002209 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002210 * IV + // except for TLS 1.0
2211 * ENC(content + padding + padding_length));
2212 */
Hanno Becker3307b532017-12-27 21:37:21 +00002213
2214 if( post_avail < transform->maclen)
2215 {
2216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2217 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2218 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002219
Hanno Beckere83efe62019-04-29 13:52:53 +01002220 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002223 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002224 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002225
Hanno Becker3307b532017-12-27 21:37:21 +00002226 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002227 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002228 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2229 data, rec->data_len );
2230 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2231 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002232
Hanno Becker3307b532017-12-27 21:37:21 +00002233 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234
Hanno Becker3307b532017-12-27 21:37:21 +00002235 rec->data_len += transform->maclen;
2236 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002237 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002238 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002241 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002243 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002249 /* Make extra sure authentication was performed, exactly once */
2250 if( auth_done != 1 )
2251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2253 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002254 }
2255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
2258 return( 0 );
2259}
2260
Hanno Becker611a83b2018-01-03 14:27:32 +00002261int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2262 mbedtls_ssl_transform *transform,
2263 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002265 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002267 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002268#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002269 size_t padlen = 0, correct = 1;
2270#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002271 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002272 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002273 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002274
Hanno Becker611a83b2018-01-03 14:27:32 +00002275#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002276 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002277 ((void) ssl);
2278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002281 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002282 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2285 }
2286 if( rec == NULL ||
2287 rec->buf == NULL ||
2288 rec->buf_len < rec->data_offset ||
2289 rec->buf_len - rec->data_offset < rec->data_len )
2290 {
2291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002293 }
2294
Hanno Becker4c6876b2017-12-27 21:28:58 +00002295 data = rec->buf + rec->data_offset;
2296 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Hanno Beckera5a2b082019-05-15 14:03:01 +01002298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002299 /*
2300 * Match record's CID with incoming CID.
2301 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002302 if( rec->cid_len != transform->in_cid_len ||
2303 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2304 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002305 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002306 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002307#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2310 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002311 {
2312 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002313 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2314 transform->iv_dec,
2315 transform->ivlen,
2316 data, rec->data_len,
2317 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002320 return( ret );
2321 }
2322
Hanno Becker4c6876b2017-12-27 21:28:58 +00002323 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002327 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 }
Paul Bakker68884e32013-01-07 18:20:04 +01002329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002331#if defined(MBEDTLS_GCM_C) || \
2332 defined(MBEDTLS_CCM_C) || \
2333 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 mode == MBEDTLS_MODE_CCM ||
2336 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002337 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002340
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002341 /*
2342 * Compute and update sizes
2343 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002344 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002347 "+ taglen (%d)", rec->data_len,
2348 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002350 }
Paul Bakker68884e32013-01-07 18:20:04 +01002351
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002352 /*
2353 * Prepare IV
2354 */
2355 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2356 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002357 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002358 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002359 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002360
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002361 }
2362 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2363 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002364 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002365 unsigned char i;
2366
2367 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2368
2369 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002370 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002371 }
2372 else
2373 {
2374 /* Reminder if we ever add an AEAD mode with a different size */
2375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2377 }
2378
Hanno Becker4c6876b2017-12-27 21:28:58 +00002379 data += explicit_iv_len;
2380 rec->data_offset += explicit_iv_len;
2381 rec->data_len -= explicit_iv_len + transform->taglen;
2382
Hanno Beckere83efe62019-04-29 13:52:53 +01002383 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002384 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002385 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386
2387 memcpy( transform->iv_dec + transform->fixed_ivlen,
2388 data - explicit_iv_len, explicit_iv_len );
2389
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002390 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002391 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002392 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002393
Paul Bakker68884e32013-01-07 18:20:04 +01002394
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002395 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002396 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002397 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002398 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2399 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401 data, rec->data_len,
2402 data, &olen,
2403 data + rec->data_len,
2404 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2409 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002410
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 return( ret );
2412 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002413 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002414
Hanno Becker4c6876b2017-12-27 21:28:58 +00002415 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002419 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2423#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002424 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002427 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002428
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 /*
Paul Bakker45829992013-01-03 14:52:21 +01002430 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002433 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2434 {
2435 /* The ciphertext is prefixed with the CBC IV. */
2436 minlen += transform->ivlen;
2437 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002438#endif
Paul Bakker45829992013-01-03 14:52:21 +01002439
Hanno Becker4c6876b2017-12-27 21:28:58 +00002440 /* Size considerations:
2441 *
2442 * - The CBC cipher text must not be empty and hence
2443 * at least of size transform->ivlen.
2444 *
2445 * Together with the potential IV-prefix, this explains
2446 * the first of the two checks below.
2447 *
2448 * - The record must contain a MAC, either in plain or
2449 * encrypted, depending on whether Encrypt-then-MAC
2450 * is used or not.
2451 * - If it is, the message contains the IV-prefix,
2452 * the CBC ciphertext, and the MAC.
2453 * - If it is not, the padded plaintext, and hence
2454 * the CBC ciphertext, has at least length maclen + 1
2455 * because there is at least the padding length byte.
2456 *
2457 * As the CBC ciphertext is not empty, both cases give the
2458 * lower bound minlen + maclen + 1 on the record size, which
2459 * we test for in the second check below.
2460 */
2461 if( rec->data_len < minlen + transform->ivlen ||
2462 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002465 "+ 1 ) ( + expl IV )", rec->data_len,
2466 transform->ivlen,
2467 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002469 }
2470
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 /*
2472 * Authenticate before decrypt if enabled
2473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002475 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002476 {
Hanno Becker992b6872017-11-09 18:57:39 +00002477 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002480
Hanno Becker4c6876b2017-12-27 21:28:58 +00002481 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2482 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002483
Hanno Beckere83efe62019-04-29 13:52:53 +01002484 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002485
Hanno Beckere83efe62019-04-29 13:52:53 +01002486 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2487 add_data_len );
2488 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2489 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2491 data, rec->data_len );
2492 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2493 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002494
Hanno Becker4c6876b2017-12-27 21:28:58 +00002495 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2496 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002497 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002498 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Hanno Becker4c6876b2017-12-27 21:28:58 +00002500 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2501 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002506 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002509
2510 /*
2511 * Check length sanity
2512 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002513 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002516 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002518 }
2519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002521 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002522 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002523 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002524 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002525 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002526 /* This is safe because data_len >= minlen + maclen + 1 initially,
2527 * and at this point we have at most subtracted maclen (note that
2528 * minlen == transform->ivlen here). */
2529 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002530
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 data += transform->ivlen;
2532 rec->data_offset += transform->ivlen;
2533 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002536
Hanno Becker4c6876b2017-12-27 21:28:58 +00002537 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2538 transform->iv_dec, transform->ivlen,
2539 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002542 return( ret );
2543 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002544
Hanno Becker4c6876b2017-12-27 21:28:58 +00002545 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002553 {
2554 /*
2555 * Save IV in SSL3 and TLS1
2556 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002557 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2558 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002560#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Hanno Becker4c6876b2017-12-27 21:28:58 +00002562 /* Safe since data_len >= minlen + maclen + 1, so after having
2563 * subtracted at most minlen and maclen up to this point,
2564 * data_len > 0. */
2565 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002566
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( auth_done == 1 )
2568 {
2569 correct *= ( rec->data_len >= padlen + 1 );
2570 padlen *= ( rec->data_len >= padlen + 1 );
2571 }
2572 else
Paul Bakker45829992013-01-03 14:52:21 +01002573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002575 if( rec->data_len < transform->maclen + padlen + 1 )
2576 {
2577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2578 rec->data_len,
2579 transform->maclen,
2580 padlen + 1 ) );
2581 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002582#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583
2584 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2585 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Hanno Becker4c6876b2017-12-27 21:28:58 +00002588 padlen++;
2589
2590 /* Regardless of the validity of the padding,
2591 * we have data_len >= padlen here. */
2592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002594 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002596 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_DEBUG_ALL)
2599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002600 "should be no more than %d",
2601 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002602#endif
Paul Bakker45829992013-01-03 14:52:21 +01002603 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605 }
2606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2608#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2609 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002610 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002612 /* The padding check involves a series of up to 256
2613 * consecutive memory reads at the end of the record
2614 * plaintext buffer. In order to hide the length and
2615 * validity of the padding, always perform exactly
2616 * `min(256,plaintext_len)` reads (but take into account
2617 * only the last `padlen` bytes for the padding check). */
2618 size_t pad_count = 0;
2619 size_t real_count = 0;
2620 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002621
Hanno Becker4c6876b2017-12-27 21:28:58 +00002622 /* Index of first padding byte; it has been ensured above
2623 * that the subtraction is safe. */
2624 size_t const padding_idx = rec->data_len - padlen;
2625 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2626 size_t const start_idx = rec->data_len - num_checks;
2627 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002628
Hanno Becker4c6876b2017-12-27 21:28:58 +00002629 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002630 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002631 real_count |= ( idx >= padding_idx );
2632 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002633 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002634 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002637 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002639#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002640 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002642 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2644 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002648 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002649
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 /* If the padding was found to be invalid, padlen == 0
2651 * and the subtraction is safe. If the padding was found valid,
2652 * padlen hasn't been changed and the previous assertion
2653 * data_len >= padlen still holds. */
2654 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002656 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002658 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002664#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002666 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002667#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
2669 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670 * Authenticate if not done yet.
2671 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002673#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002674 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002675 {
Hanno Becker992b6872017-11-09 18:57:39 +00002676 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002677
Hanno Becker4c6876b2017-12-27 21:28:58 +00002678 /* If the initial value of padlen was such that
2679 * data_len < maclen + padlen + 1, then padlen
2680 * got reset to 1, and the initial check
2681 * data_len >= minlen + maclen + 1
2682 * guarantees that at this point we still
2683 * have at least data_len >= maclen.
2684 *
2685 * If the initial value of padlen was such that
2686 * data_len >= maclen + padlen + 1, then we have
2687 * subtracted either padlen + 1 (if the padding was correct)
2688 * or 0 (if the padding was incorrect) since then,
2689 * hence data_len >= maclen in any case.
2690 */
2691 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Hanno Beckere83efe62019-04-29 13:52:53 +01002693 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002696 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002697 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002698 ssl_mac( &transform->md_ctx_dec,
2699 transform->mac_dec,
2700 data, rec->data_len,
2701 rec->ctr, rec->type,
2702 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002703 }
2704 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2706#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2707 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002708 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002709 {
2710 /*
2711 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002712 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002713 *
2714 * Known timing attacks:
2715 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2716 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002717 * To compensate for different timings for the MAC calculation
2718 * depending on how much padding was removed (which is determined
2719 * by padlen), process extra_run more blocks through the hash
2720 * function.
2721 *
2722 * The formula in the paper is
2723 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2724 * where L1 is the size of the header plus the decrypted message
2725 * plus CBC padding and L2 is the size of the header plus the
2726 * decrypted message. This is for an underlying hash function
2727 * with 64-byte blocks.
2728 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2729 * correctly. We round down instead of up, so -56 is the correct
2730 * value for our calculations instead of -55.
2731 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002732 * Repeat the formula rather than defining a block_size variable.
2733 * This avoids requiring division by a variable at runtime
2734 * (which would be marginally less efficient and would require
2735 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002736 */
2737 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002738 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002739
2740 /*
2741 * The next two sizes are the minimum and maximum values of
2742 * in_msglen over all padlen values.
2743 *
2744 * They're independent of padlen, since we previously did
2745 * in_msglen -= padlen.
2746 *
2747 * Note that max_len + maclen is never more than the buffer
2748 * length, as we previously did in_msglen -= maclen too.
2749 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002750 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002751 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2752
Hanno Becker4c6876b2017-12-27 21:28:58 +00002753 memset( tmp, 0, sizeof( tmp ) );
2754
2755 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002757#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2758 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002759 case MBEDTLS_MD_MD5:
2760 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002762 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002763 extra_run =
2764 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2765 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002766 break;
2767#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002768#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002769 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002770 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002771 extra_run =
2772 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2773 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002774 break;
2775#endif
2776 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2779 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002780
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002781 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002782
Hanno Beckere83efe62019-04-29 13:52:53 +01002783 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2784 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002785 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2786 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002787 /* Make sure we access everything even when padlen > 0. This
2788 * makes the synchronisation requirements for just-in-time
2789 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002790 ssl_read_memory( data + rec->data_len, padlen );
2791 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002792
2793 /* Call mbedtls_md_process at least once due to cache attacks
2794 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002795 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002796 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002797
Hanno Becker4c6876b2017-12-27 21:28:58 +00002798 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002799
2800 /* Make sure we access all the memory that could contain the MAC,
2801 * before we check it in the next code block. This makes the
2802 * synchronisation requirements for just-in-time Prime+Probe
2803 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002804 ssl_read_memory( data + min_len,
2805 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002806 }
2807 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2809 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002815#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002816 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2817 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002818#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2821 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#if defined(MBEDTLS_SSL_DEBUG_ALL)
2824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002825#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002826 correct = 0;
2827 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002828 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002829 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002830
2831 /*
2832 * Finally check the correct flag
2833 */
2834 if( correct == 0 )
2835 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002836#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837
2838 /* Make extra sure authentication was performed, exactly once */
2839 if( auth_done != 1 )
2840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002843 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Hanno Beckera5a2b082019-05-15 14:03:01 +01002845#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002846 if( rec->cid_len != 0 )
2847 {
2848 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2849 &rec->type );
2850 if( ret != 0 )
2851 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2852 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002853#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
2857 return( 0 );
2858}
2859
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002860#undef MAC_NONE
2861#undef MAC_PLAINTEXT
2862#undef MAC_CIPHERTEXT
2863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002865/*
2866 * Compression/decompression functions
2867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002869{
2870 int ret;
2871 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002872 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002873 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002874 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002877
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002878 if( len_pre == 0 )
2879 return( 0 );
2880
Paul Bakker2770fbd2012-07-03 13:30:23 +00002881 memcpy( msg_pre, ssl->out_msg, len_pre );
2882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002884 ssl->out_msglen ) );
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002887 ssl->out_msg, ssl->out_msglen );
2888
Paul Bakker48916f92012-09-16 19:57:18 +00002889 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2890 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2891 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002892 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893
Paul Bakker48916f92012-09-16 19:57:18 +00002894 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002895 if( ret != Z_OK )
2896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2898 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 }
2900
Angus Grattond8213d02016-05-25 20:56:48 +10002901 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002902 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905 ssl->out_msglen ) );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908 ssl->out_msg, ssl->out_msglen );
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911
2912 return( 0 );
2913}
2914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002916{
2917 int ret;
2918 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002919 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002921 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002925 if( len_pre == 0 )
2926 return( 0 );
2927
Paul Bakker2770fbd2012-07-03 13:30:23 +00002928 memcpy( msg_pre, ssl->in_msg, len_pre );
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931 ssl->in_msglen ) );
2932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002934 ssl->in_msg, ssl->in_msglen );
2935
Paul Bakker48916f92012-09-16 19:57:18 +00002936 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2937 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2938 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002939 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002940 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002941
Paul Bakker48916f92012-09-16 19:57:18 +00002942 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 if( ret != Z_OK )
2944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2946 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947 }
2948
Angus Grattond8213d02016-05-25 20:56:48 +10002949 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002950 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953 ssl->in_msglen ) );
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956 ssl->in_msg, ssl->in_msglen );
2957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002959
2960 return( 0 );
2961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2965static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#if defined(MBEDTLS_SSL_PROTO_DTLS)
2968static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002969{
2970 /* If renegotiation is not enforced, retransmit until we would reach max
2971 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002972 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002973 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002974 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002975 unsigned char doublings = 1;
2976
2977 while( ratio != 0 )
2978 {
2979 ++doublings;
2980 ratio >>= 1;
2981 }
2982
2983 if( ++ssl->renego_records_seen > doublings )
2984 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002986 return( 0 );
2987 }
2988 }
2989
2990 return( ssl_write_hello_request( ssl ) );
2991}
2992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002994
Paul Bakker5121ce52009-01-03 21:22:43 +00002995/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002996 * Fill the input message buffer by appending data to it.
2997 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002998 *
2999 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3000 * available (from this read and/or a previous one). Otherwise, an error code
3001 * is returned (possibly EOF or WANT_READ).
3002 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003003 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3004 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3005 * since we always read a whole datagram at once.
3006 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003007 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003008 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003011{
Paul Bakker23986e52011-04-24 08:57:21 +00003012 int ret;
3013 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003017 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003020 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003022 }
3023
Angus Grattond8213d02016-05-25 20:56:48 +10003024 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003028 }
3029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003031 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003033 uint32_t timeout;
3034
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003035 /* Just to be sure */
3036 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3037 {
3038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3039 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3041 }
3042
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003043 /*
3044 * The point is, we need to always read a full datagram at once, so we
3045 * sometimes read more then requested, and handle the additional data.
3046 * It could be the rest of the current record (while fetching the
3047 * header) and/or some other records in the same datagram.
3048 */
3049
3050 /*
3051 * Move to the next record in the already read datagram if applicable
3052 */
3053 if( ssl->next_record_offset != 0 )
3054 {
3055 if( ssl->in_left < ssl->next_record_offset )
3056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003059 }
3060
3061 ssl->in_left -= ssl->next_record_offset;
3062
3063 if( ssl->in_left != 0 )
3064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003066 ssl->next_record_offset ) );
3067 memmove( ssl->in_hdr,
3068 ssl->in_hdr + ssl->next_record_offset,
3069 ssl->in_left );
3070 }
3071
3072 ssl->next_record_offset = 0;
3073 }
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003077
3078 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003079 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003080 */
3081 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003084 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003085 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003086
3087 /*
3088 * A record can't be split accross datagrams. If we need to read but
3089 * are not at the beginning of a new record, the caller did something
3090 * wrong.
3091 */
3092 if( ssl->in_left != 0 )
3093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003096 }
3097
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003098 /*
3099 * Don't even try to read if time's out already.
3100 * This avoids by-passing the timer when repeatedly receiving messages
3101 * that will end up being dropped.
3102 */
3103 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003104 {
3105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003106 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003107 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003108 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003109 {
Angus Grattond8213d02016-05-25 20:56:48 +10003110 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113 timeout = ssl->handshake->retransmit_timeout;
3114 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003115 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003118
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003119 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003120 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3121 timeout );
3122 else
3123 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126
3127 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 }
3130
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003131 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003134 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003137 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003138 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003141 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003142 }
3143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003147 return( ret );
3148 }
3149
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003150 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003153 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003155 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003156 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003159 return( ret );
3160 }
3161
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003162 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003163 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003165 }
3166
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 if( ret < 0 )
3168 return( ret );
3169
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003170 ssl->in_left = ret;
3171 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003172 MBEDTLS_SSL_TRANSPORT_ELSE
3173#endif /* MBEDTLS_SSL_PROTO_DTLS */
3174#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003177 ssl->in_left, nb_want ) );
3178
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003179 while( ssl->in_left < nb_want )
3180 {
3181 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003182
3183 if( ssl_check_timer( ssl ) != 0 )
3184 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3185 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003186 {
3187 if( ssl->f_recv_timeout != NULL )
3188 {
3189 ret = ssl->f_recv_timeout( ssl->p_bio,
3190 ssl->in_hdr + ssl->in_left, len,
3191 ssl->conf->read_timeout );
3192 }
3193 else
3194 {
3195 ret = ssl->f_recv( ssl->p_bio,
3196 ssl->in_hdr + ssl->in_left, len );
3197 }
3198 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003201 ssl->in_left, nb_want ) );
3202 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003203
3204 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003206
3207 if( ret < 0 )
3208 return( ret );
3209
mohammad160352aecb92018-03-28 23:41:40 -07003210 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003211 {
Darryl Green11999bb2018-03-13 15:22:58 +00003212 MBEDTLS_SSL_DEBUG_MSG( 1,
3213 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003214 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3216 }
3217
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003218 ssl->in_left += ret;
3219 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003221#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
3225 return( 0 );
3226}
3227
3228/*
3229 * Flush any data not yet written
3230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003232{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003233 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003234 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003238 if( ssl->f_send == NULL )
3239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003241 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003243 }
3244
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003245 /* Avoid incrementing counter if data is flushed */
3246 if( ssl->out_left == 0 )
3247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003249 return( 0 );
3250 }
3251
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 while( ssl->out_left > 0 )
3253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003255 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Hanno Becker2b1e3542018-08-06 11:19:13 +01003257 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003258 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
3262 if( ret <= 0 )
3263 return( ret );
3264
mohammad160352aecb92018-03-28 23:41:40 -07003265 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003266 {
Darryl Green11999bb2018-03-13 15:22:58 +00003267 MBEDTLS_SSL_DEBUG_MSG( 1,
3268 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003269 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3271 }
3272
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 ssl->out_left -= ret;
3274 }
3275
Hanno Becker2b1e3542018-08-06 11:19:13 +01003276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003277 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003278 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003279 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003280 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003281 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003282#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003283#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003284 {
3285 ssl->out_hdr = ssl->out_buf + 8;
3286 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003287#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003288 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291
3292 return( 0 );
3293}
3294
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003295/*
3296 * Functions to handle the DTLS retransmission state machine
3297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003299/*
3300 * Append current handshake message to current outgoing flight
3301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3306 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3307 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003308
3309 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003310 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003311 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003314 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003315 }
3316
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003317 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003321 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003322 }
3323
3324 /* Copy current handshake message with headers */
3325 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3326 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003327 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003328 msg->next = NULL;
3329
3330 /* Append to the current flight */
3331 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003332 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 else
3334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003336 while( cur->next != NULL )
3337 cur = cur->next;
3338 cur->next = msg;
3339 }
3340
Hanno Becker3b235902018-08-06 09:54:53 +01003341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342 return( 0 );
3343}
3344
3345/*
3346 * Free the current flight of handshake messages
3347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_ssl_flight_item *cur = flight;
3351 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352
3353 while( cur != NULL )
3354 {
3355 next = cur->next;
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_free( cur->p );
3358 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003359
3360 cur = next;
3361 }
3362}
3363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3365static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003366#endif
3367
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003369 * Swap transform_out and out_ctr with the alternative ones
3370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003374 unsigned char tmp_out_ctr[8];
3375
3376 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003379 return;
3380 }
3381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003383
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003384 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003385 tmp_transform = ssl->transform_out;
3386 ssl->transform_out = ssl->handshake->alt_transform_out;
3387 ssl->handshake->alt_transform_out = tmp_transform;
3388
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003389 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003390 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3391 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003392 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003393
3394 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003395 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3398 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3403 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003404 }
3405 }
3406#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407}
3408
3409/*
3410 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003411 */
3412int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3413{
3414 int ret = 0;
3415
3416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3417
3418 ret = mbedtls_ssl_flight_transmit( ssl );
3419
3420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3421
3422 return( ret );
3423}
3424
3425/*
3426 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003427 *
3428 * Need to remember the current message in case flush_output returns
3429 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003430 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003431 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003432int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003433{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003434 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003438 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003440
3441 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003442 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003443 ssl_swap_epochs( ssl );
3444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003446 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003447
3448 while( ssl->handshake->cur_msg != NULL )
3449 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003450 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003451 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003452
Hanno Beckere1dcb032018-08-17 16:47:58 +01003453 int const is_finished =
3454 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3455 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3456
Hanno Becker04da1892018-08-14 13:22:10 +01003457 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3458 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3459
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003460 /* Swap epochs before sending Finished: we can't do it after
3461 * sending ChangeCipherSpec, in case write returns WANT_READ.
3462 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003463 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003464 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003466 ssl_swap_epochs( ssl );
3467 }
3468
Hanno Becker67bc7c32018-08-06 11:33:50 +01003469 ret = ssl_get_remaining_payload_in_datagram( ssl );
3470 if( ret < 0 )
3471 return( ret );
3472 max_frag_len = (size_t) ret;
3473
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003474 /* CCS is copied as is, while HS messages may need fragmentation */
3475 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3476 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003477 if( max_frag_len == 0 )
3478 {
3479 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3480 return( ret );
3481
3482 continue;
3483 }
3484
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003485 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003486 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003487 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003488
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003489 /* Update position inside current message */
3490 ssl->handshake->cur_msg_p += cur->len;
3491 }
3492 else
3493 {
3494 const unsigned char * const p = ssl->handshake->cur_msg_p;
3495 const size_t hs_len = cur->len - 12;
3496 const size_t frag_off = p - ( cur->p + 12 );
3497 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003498 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003499
Hanno Beckere1dcb032018-08-17 16:47:58 +01003500 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003501 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003502 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003503 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003504
Hanno Becker67bc7c32018-08-06 11:33:50 +01003505 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3506 return( ret );
3507
3508 continue;
3509 }
3510 max_hs_frag_len = max_frag_len - 12;
3511
3512 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3513 max_hs_frag_len : rem_len;
3514
3515 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003516 {
3517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003518 (unsigned) cur_hs_frag_len,
3519 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003520 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003521
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003522 /* Messages are stored with handshake headers as if not fragmented,
3523 * copy beginning of headers then fill fragmentation fields.
3524 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3525 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003526
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003527 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3528 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3529 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3530
Hanno Becker67bc7c32018-08-06 11:33:50 +01003531 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3532 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3533 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003534
3535 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3536
Hanno Becker3f7b9732018-08-28 09:53:25 +01003537 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003538 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3539 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003540 ssl->out_msgtype = cur->type;
3541
3542 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003543 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003544 }
3545
3546 /* If done with the current message move to the next one if any */
3547 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3548 {
3549 if( cur->next != NULL )
3550 {
3551 ssl->handshake->cur_msg = cur->next;
3552 ssl->handshake->cur_msg_p = cur->next->p + 12;
3553 }
3554 else
3555 {
3556 ssl->handshake->cur_msg = NULL;
3557 ssl->handshake->cur_msg_p = NULL;
3558 }
3559 }
3560
3561 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003562 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003565 return( ret );
3566 }
3567 }
3568
Hanno Becker67bc7c32018-08-06 11:33:50 +01003569 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3570 return( ret );
3571
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003572 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3574 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003575 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003578 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3579 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003580
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582
3583 return( 0 );
3584}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003585
3586/*
3587 * To be called when the last message of an incoming flight is received.
3588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003590{
3591 /* We won't need to resend that one any more */
3592 ssl_flight_free( ssl->handshake->flight );
3593 ssl->handshake->flight = NULL;
3594 ssl->handshake->cur_msg = NULL;
3595
3596 /* The next incoming flight will start with this msg_seq */
3597 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3598
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003599 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003600 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003601
Hanno Becker0271f962018-08-16 13:23:47 +01003602 /* Clear future message buffering structure. */
3603 ssl_buffering_free( ssl );
3604
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003605 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003606 ssl_set_timer( ssl, 0 );
3607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3609 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003612 }
3613 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003615}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003616
3617/*
3618 * To be called when the last message of an outgoing flight is send.
3619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003621{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003622 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003623 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3626 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003629 }
3630 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003634
Paul Bakker5121ce52009-01-03 21:22:43 +00003635/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003636 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003638
3639/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003640 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003641 *
3642 * - fill in handshake headers
3643 * - update handshake checksum
3644 * - DTLS: save message for resending
3645 * - then pass to the record layer
3646 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003647 * DTLS: except for HelloRequest, messages are only queued, and will only be
3648 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003649 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003650 * Inputs:
3651 * - ssl->out_msglen: 4 + actual handshake message len
3652 * (4 is the size of handshake headers for TLS)
3653 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3654 * - ssl->out_msg + 4: the handshake message body
3655 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003656 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657 * - ssl->out_msglen: the length of the record contents
3658 * (including handshake headers but excluding record headers)
3659 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003660 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003661int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003662{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003663 int ret;
3664 const size_t hs_len = ssl->out_msglen - 4;
3665 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003666
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3668
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003669 /*
3670 * Sanity checks
3671 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003672 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003673 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3674 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003675 /* In SSLv3, the client might send a NoCertificate alert. */
3676#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3677 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3678 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3679 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3680#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3681 {
3682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3684 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003687 /* Whenever we send anything different from a
3688 * HelloRequest we should be in a handshake - double check. */
3689 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3690 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003691 ssl->handshake == NULL )
3692 {
3693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3695 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003698 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003699 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003704 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003705#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003706
Hanno Beckerb50a2532018-08-06 11:52:54 +01003707 /* Double-check that we did not exceed the bounds
3708 * of the outgoing record buffer.
3709 * This should never fail as the various message
3710 * writing functions must obey the bounds of the
3711 * outgoing record buffer, but better be safe.
3712 *
3713 * Note: We deliberately do not check for the MTU or MFL here.
3714 */
3715 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3716 {
3717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3718 "size %u, maximum %u",
3719 (unsigned) ssl->out_msglen,
3720 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3722 }
3723
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003724 /*
3725 * Fill handshake headers
3726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003729 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3730 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3731 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003733 /*
3734 * DTLS has additional fields in the Handshake layer,
3735 * between the length field and the actual payload:
3736 * uint16 message_seq;
3737 * uint24 fragment_offset;
3738 * uint24 fragment_length;
3739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003742 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003743 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003744 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003745 {
3746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3747 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003748 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003749 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3751 }
3752
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003753 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003754 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003755
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003756 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003757 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003758 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003759 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3760 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3761 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003762 }
3763 else
3764 {
3765 ssl->out_msg[4] = 0;
3766 ssl->out_msg[5] = 0;
3767 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003768
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 /* Handshake hashes are computed without fragmentation,
3770 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003771 memset( ssl->out_msg + 6, 0x00, 3 );
3772 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003773 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003775
Hanno Becker0207e532018-08-28 10:28:28 +01003776 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003777 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3778 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 }
3780
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003781 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003783 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003784 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3785 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003786 {
3787 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003790 return( ret );
3791 }
3792 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003793 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003794#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003795 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003796 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003797 {
3798 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3799 return( ret );
3800 }
3801 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003802
3803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3804
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003805 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003806}
3807
3808/*
3809 * Record layer functions
3810 */
3811
3812/*
3813 * Write current record.
3814 *
3815 * Uses:
3816 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3817 * - ssl->out_msglen: length of the record content (excl headers)
3818 * - ssl->out_msg: record content
3819 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003821{
3822 int ret, done = 0;
3823 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003824 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003825
3826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003829 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003831 {
3832 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003835 return( ret );
3836 }
3837
3838 len = ssl->out_msglen;
3839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3843 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 ret = mbedtls_ssl_hw_record_write( ssl );
3848 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3851 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003852 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003853
3854 if( ret == 0 )
3855 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003858 if( !done )
3859 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003860 unsigned i;
3861 size_t protected_record_size;
3862
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003863 /* Skip writing the record content type to after the encryption,
3864 * as it may change when using the CID extension. */
3865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003867 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003868
Hanno Becker19859472018-08-06 09:40:20 +01003869 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003870 ssl->out_len[0] = (unsigned char)( len >> 8 );
3871 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003872
Paul Bakker48916f92012-09-16 19:57:18 +00003873 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003874 {
Hanno Becker3307b532017-12-27 21:37:21 +00003875 mbedtls_record rec;
3876
3877 rec.buf = ssl->out_iv;
3878 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3879 ( ssl->out_iv - ssl->out_buf );
3880 rec.data_len = ssl->out_msglen;
3881 rec.data_offset = ssl->out_msg - rec.buf;
3882
3883 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3884 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3885 ssl->conf->transport, rec.ver );
3886 rec.type = ssl->out_msgtype;
3887
Hanno Beckera5a2b082019-05-15 14:03:01 +01003888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003889 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003890 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003891#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003892
Hanno Becker611a83b2018-01-03 14:27:32 +00003893 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003894 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003897 return( ret );
3898 }
3899
Hanno Becker3307b532017-12-27 21:37:21 +00003900 if( rec.data_offset != 0 )
3901 {
3902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3903 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3904 }
3905
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003906 /* Update the record content type and CID. */
3907 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003908#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003909 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003910#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003911 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003912 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3913 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003914 }
3915
Hanno Becker43395762019-05-03 14:46:38 +01003916 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003917
3918#if defined(MBEDTLS_SSL_PROTO_DTLS)
3919 /* In case of DTLS, double-check that we don't exceed
3920 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003921 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003922 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003923 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003924 if( ret < 0 )
3925 return( ret );
3926
3927 if( protected_record_size > (size_t) ret )
3928 {
3929 /* Should never happen */
3930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3931 }
3932 }
3933#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003934
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003935 /* Now write the potentially updated record content type. */
3936 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003939 "version = [%d:%d], msglen = %d",
3940 ssl->out_hdr[0], ssl->out_hdr[1],
3941 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003944 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003945
3946 ssl->out_left += protected_record_size;
3947 ssl->out_hdr += protected_record_size;
3948 ssl_update_out_pointers( ssl, ssl->transform_out );
3949
Hanno Becker04484622018-08-06 09:49:38 +01003950 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3951 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3952 break;
3953
3954 /* The loop goes to its end iff the counter is wrapping */
3955 if( i == ssl_ep_len( ssl ) )
3956 {
3957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3958 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003960 }
3961
Hanno Becker67bc7c32018-08-06 11:33:50 +01003962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003963 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003964 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003965 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003966 size_t remaining;
3967 ret = ssl_get_remaining_payload_in_datagram( ssl );
3968 if( ret < 0 )
3969 {
3970 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3971 ret );
3972 return( ret );
3973 }
3974
3975 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003976 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003977 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003978 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003979 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003980 else
3981 {
Hanno Becker513815a2018-08-20 11:56:09 +01003982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003983 }
3984 }
3985#endif /* MBEDTLS_SSL_PROTO_DTLS */
3986
3987 if( ( flush == SSL_FORCE_FLUSH ) &&
3988 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991 return( ret );
3992 }
3993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003995
3996 return( 0 );
3997}
3998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004000
4001static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4002{
4003 if( ssl->in_msglen < ssl->in_hslen ||
4004 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4005 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4006 {
4007 return( 1 );
4008 }
4009 return( 0 );
4010}
Hanno Becker44650b72018-08-16 12:51:11 +01004011
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004012static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004013{
4014 return( ( ssl->in_msg[9] << 16 ) |
4015 ( ssl->in_msg[10] << 8 ) |
4016 ssl->in_msg[11] );
4017}
4018
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004019static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004020{
4021 return( ( ssl->in_msg[6] << 16 ) |
4022 ( ssl->in_msg[7] << 8 ) |
4023 ssl->in_msg[8] );
4024}
4025
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004026static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004027{
4028 uint32_t msg_len, frag_off, frag_len;
4029
4030 msg_len = ssl_get_hs_total_len( ssl );
4031 frag_off = ssl_get_hs_frag_off( ssl );
4032 frag_len = ssl_get_hs_frag_len( ssl );
4033
4034 if( frag_off > msg_len )
4035 return( -1 );
4036
4037 if( frag_len > msg_len - frag_off )
4038 return( -1 );
4039
4040 if( frag_len + 12 > ssl->in_msglen )
4041 return( -1 );
4042
4043 return( 0 );
4044}
4045
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004046/*
4047 * Mark bits in bitmask (used for DTLS HS reassembly)
4048 */
4049static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4050{
4051 unsigned int start_bits, end_bits;
4052
4053 start_bits = 8 - ( offset % 8 );
4054 if( start_bits != 8 )
4055 {
4056 size_t first_byte_idx = offset / 8;
4057
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004058 /* Special case */
4059 if( len <= start_bits )
4060 {
4061 for( ; len != 0; len-- )
4062 mask[first_byte_idx] |= 1 << ( start_bits - len );
4063
4064 /* Avoid potential issues with offset or len becoming invalid */
4065 return;
4066 }
4067
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004068 offset += start_bits; /* Now offset % 8 == 0 */
4069 len -= start_bits;
4070
4071 for( ; start_bits != 0; start_bits-- )
4072 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4073 }
4074
4075 end_bits = len % 8;
4076 if( end_bits != 0 )
4077 {
4078 size_t last_byte_idx = ( offset + len ) / 8;
4079
4080 len -= end_bits; /* Now len % 8 == 0 */
4081
4082 for( ; end_bits != 0; end_bits-- )
4083 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4084 }
4085
4086 memset( mask + offset / 8, 0xFF, len / 8 );
4087}
4088
4089/*
4090 * Check that bitmask is full
4091 */
4092static int ssl_bitmask_check( unsigned char *mask, size_t len )
4093{
4094 size_t i;
4095
4096 for( i = 0; i < len / 8; i++ )
4097 if( mask[i] != 0xFF )
4098 return( -1 );
4099
4100 for( i = 0; i < len % 8; i++ )
4101 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4102 return( -1 );
4103
4104 return( 0 );
4105}
4106
Hanno Becker56e205e2018-08-16 09:06:12 +01004107/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004108static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004109 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004110{
Hanno Becker56e205e2018-08-16 09:06:12 +01004111 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004112
Hanno Becker56e205e2018-08-16 09:06:12 +01004113 alloc_len = 12; /* Handshake header */
4114 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004115
Hanno Beckerd07df862018-08-16 09:14:58 +01004116 if( add_bitmap )
4117 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004118
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004119 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004120}
Hanno Becker56e205e2018-08-16 09:06:12 +01004121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004123
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004124static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004125{
4126 return( ( ssl->in_msg[1] << 16 ) |
4127 ( ssl->in_msg[2] << 8 ) |
4128 ssl->in_msg[3] );
4129}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004130
Simon Butcher99000142016-10-13 17:21:01 +01004131int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004136 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004138 }
4139
Hanno Becker12555c62018-08-16 12:47:53 +01004140 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004143 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004144 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004146#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004147 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004148 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004149 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004150 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004151
Hanno Becker44650b72018-08-16 12:51:11 +01004152 if( ssl_check_hs_header( ssl ) != 0 )
4153 {
4154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4155 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4156 }
4157
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004158 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004159 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4160 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4161 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4162 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004163 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004164 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4165 {
4166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4167 recv_msg_seq,
4168 ssl->handshake->in_msg_seq ) );
4169 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4170 }
4171
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004172 /* Retransmit only on last message from previous flight, to avoid
4173 * too many retransmissions.
4174 * Besides, No sane server ever retransmits HelloVerifyRequest */
4175 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004179 "message_seq = %d, start_of_flight = %d",
4180 recv_msg_seq,
4181 ssl->handshake->in_flight_start_seq ) );
4182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004186 return( ret );
4187 }
4188 }
4189 else
4190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004192 "message_seq = %d, expected = %d",
4193 recv_msg_seq,
4194 ssl->handshake->in_msg_seq ) );
4195 }
4196
Hanno Becker90333da2017-10-10 11:27:13 +01004197 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004198 }
4199 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004200
Hanno Becker6d97ef52018-08-16 13:09:04 +01004201 /* Message reassembly is handled alongside buffering of future
4202 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004203 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004204 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004205 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004208 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004209 }
4210 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004211 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004213#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004214 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004215 /* With TLS we don't handle fragmentation (for now) */
4216 if( ssl->in_msglen < ssl->in_hslen )
4217 {
4218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4219 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4220 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004221 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004222#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004223
Simon Butcher99000142016-10-13 17:21:01 +01004224 return( 0 );
4225}
4226
4227void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4228{
Hanno Becker0271f962018-08-16 13:23:47 +01004229 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004230
Hanno Becker0271f962018-08-16 13:23:47 +01004231 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004232 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004233 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004234 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004235
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004236 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004238 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004239 ssl->handshake != NULL )
4240 {
Hanno Becker0271f962018-08-16 13:23:47 +01004241 unsigned offset;
4242 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004243
Hanno Becker0271f962018-08-16 13:23:47 +01004244 /* Increment handshake sequence number */
4245 hs->in_msg_seq++;
4246
4247 /*
4248 * Clear up handshake buffering and reassembly structure.
4249 */
4250
4251 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004252 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004253
4254 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004255 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4256 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004257 offset++, hs_buf++ )
4258 {
4259 *hs_buf = *(hs_buf + 1);
4260 }
4261
4262 /* Create a fresh last entry */
4263 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004264 }
4265#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004266}
4267
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004268/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004269 * DTLS anti-replay: RFC 6347 4.1.2.6
4270 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004271 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4272 * Bit n is set iff record number in_window_top - n has been seen.
4273 *
4274 * Usually, in_window_top is the last record number seen and the lsb of
4275 * in_window is set. The only exception is the initial state (record number 0
4276 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4279static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004280{
4281 ssl->in_window_top = 0;
4282 ssl->in_window = 0;
4283}
4284
4285static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4286{
4287 return( ( (uint64_t) buf[0] << 40 ) |
4288 ( (uint64_t) buf[1] << 32 ) |
4289 ( (uint64_t) buf[2] << 24 ) |
4290 ( (uint64_t) buf[3] << 16 ) |
4291 ( (uint64_t) buf[4] << 8 ) |
4292 ( (uint64_t) buf[5] ) );
4293}
4294
4295/*
4296 * Return 0 if sequence number is acceptable, -1 otherwise
4297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004299{
4300 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4301 uint64_t bit;
4302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004303 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004304 return( 0 );
4305
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004306 if( rec_seqnum > ssl->in_window_top )
4307 return( 0 );
4308
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004309 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004310
4311 if( bit >= 64 )
4312 return( -1 );
4313
4314 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4315 return( -1 );
4316
4317 return( 0 );
4318}
4319
4320/*
4321 * Update replay window on new validated record
4322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004324{
4325 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4326
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004327 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004328 return;
4329
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004330 if( rec_seqnum > ssl->in_window_top )
4331 {
4332 /* Update window_top and the contents of the window */
4333 uint64_t shift = rec_seqnum - ssl->in_window_top;
4334
4335 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004336 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004337 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004338 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004340 ssl->in_window |= 1;
4341 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004342
4343 ssl->in_window_top = rec_seqnum;
4344 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345 else
4346 {
4347 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004348 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004349
4350 if( bit < 64 ) /* Always true, but be extra sure */
4351 ssl->in_window |= (uint64_t) 1 << bit;
4352 }
4353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004355
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004356#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004357/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004358static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4359
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004360/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004361 * Without any SSL context, check if a datagram looks like a ClientHello with
4362 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004363 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004364 *
4365 * - if cookie is valid, return 0
4366 * - if ClientHello looks superficially valid but cookie is not,
4367 * fill obuf and set olen, then
4368 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4369 * - otherwise return a specific error code
4370 */
4371static int ssl_check_dtls_clihlo_cookie(
4372 mbedtls_ssl_cookie_write_t *f_cookie_write,
4373 mbedtls_ssl_cookie_check_t *f_cookie_check,
4374 void *p_cookie,
4375 const unsigned char *cli_id, size_t cli_id_len,
4376 const unsigned char *in, size_t in_len,
4377 unsigned char *obuf, size_t buf_len, size_t *olen )
4378{
4379 size_t sid_len, cookie_len;
4380 unsigned char *p;
4381
4382 if( f_cookie_write == NULL || f_cookie_check == NULL )
4383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4384
4385 /*
4386 * Structure of ClientHello with record and handshake headers,
4387 * and expected values. We don't need to check a lot, more checks will be
4388 * done when actually parsing the ClientHello - skipping those checks
4389 * avoids code duplication and does not make cookie forging any easier.
4390 *
4391 * 0-0 ContentType type; copied, must be handshake
4392 * 1-2 ProtocolVersion version; copied
4393 * 3-4 uint16 epoch; copied, must be 0
4394 * 5-10 uint48 sequence_number; copied
4395 * 11-12 uint16 length; (ignored)
4396 *
4397 * 13-13 HandshakeType msg_type; (ignored)
4398 * 14-16 uint24 length; (ignored)
4399 * 17-18 uint16 message_seq; copied
4400 * 19-21 uint24 fragment_offset; copied, must be 0
4401 * 22-24 uint24 fragment_length; (ignored)
4402 *
4403 * 25-26 ProtocolVersion client_version; (ignored)
4404 * 27-58 Random random; (ignored)
4405 * 59-xx SessionID session_id; 1 byte len + sid_len content
4406 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4407 * ...
4408 *
4409 * Minimum length is 61 bytes.
4410 */
4411 if( in_len < 61 ||
4412 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4413 in[3] != 0 || in[4] != 0 ||
4414 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4415 {
4416 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4417 }
4418
4419 sid_len = in[59];
4420 if( sid_len > in_len - 61 )
4421 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4422
4423 cookie_len = in[60 + sid_len];
4424 if( cookie_len > in_len - 60 )
4425 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4426
4427 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4428 cli_id, cli_id_len ) == 0 )
4429 {
4430 /* Valid cookie */
4431 return( 0 );
4432 }
4433
4434 /*
4435 * If we get here, we've got an invalid cookie, let's prepare HVR.
4436 *
4437 * 0-0 ContentType type; copied
4438 * 1-2 ProtocolVersion version; copied
4439 * 3-4 uint16 epoch; copied
4440 * 5-10 uint48 sequence_number; copied
4441 * 11-12 uint16 length; olen - 13
4442 *
4443 * 13-13 HandshakeType msg_type; hello_verify_request
4444 * 14-16 uint24 length; olen - 25
4445 * 17-18 uint16 message_seq; copied
4446 * 19-21 uint24 fragment_offset; copied
4447 * 22-24 uint24 fragment_length; olen - 25
4448 *
4449 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4450 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4451 *
4452 * Minimum length is 28.
4453 */
4454 if( buf_len < 28 )
4455 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4456
4457 /* Copy most fields and adapt others */
4458 memcpy( obuf, in, 25 );
4459 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4460 obuf[25] = 0xfe;
4461 obuf[26] = 0xff;
4462
4463 /* Generate and write actual cookie */
4464 p = obuf + 28;
4465 if( f_cookie_write( p_cookie,
4466 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4467 {
4468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4469 }
4470
4471 *olen = p - obuf;
4472
4473 /* Go back and fill length fields */
4474 obuf[27] = (unsigned char)( *olen - 28 );
4475
4476 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4477 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4478 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4479
4480 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4481 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4482
4483 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4484}
4485
4486/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004487 * Handle possible client reconnect with the same UDP quadruplet
4488 * (RFC 6347 Section 4.2.8).
4489 *
4490 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4491 * that looks like a ClientHello.
4492 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004493 * - if the input looks like a ClientHello without cookies,
4494 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004495 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004496 * - if the input looks like a ClientHello with a valid cookie,
4497 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004498 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004499 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004500 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004501 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004502 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4503 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004504 */
4505static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4506{
4507 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004508 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 ret = ssl_check_dtls_clihlo_cookie(
4511 ssl->conf->f_cookie_write,
4512 ssl->conf->f_cookie_check,
4513 ssl->conf->p_cookie,
4514 ssl->cli_id, ssl->cli_id_len,
4515 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004516 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004517
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004518 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4519
4520 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004521 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004522 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 * If the error is permanent we'll catch it later,
4524 * if it's not, then hopefully it'll work next time. */
4525 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4526
4527 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004528 }
4529
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004530 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004531 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004532 /* Got a valid cookie, partially reset context */
4533 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4534 {
4535 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4536 return( ret );
4537 }
4538
4539 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004540 }
4541
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004542 return( ret );
4543}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004544#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004545
Hanno Becker46483f12019-05-03 13:25:54 +01004546static int ssl_check_record_type( uint8_t record_type )
4547{
4548 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4549 record_type != MBEDTLS_SSL_MSG_ALERT &&
4550 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4551 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4552 {
4553 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4554 }
4555
4556 return( 0 );
4557}
4558
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004559/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004560 * ContentType type;
4561 * ProtocolVersion version;
4562 * uint16 epoch; // DTLS only
4563 * uint48 sequence_number; // DTLS only
4564 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004565 *
4566 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004567 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004568 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4569 *
4570 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004571 * 1. proceed with the record if this function returns 0
4572 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4573 * 3. return CLIENT_RECONNECT if this function return that value
4574 * 4. drop the whole datagram if this function returns anything else.
4575 * Point 2 is needed when the peer is resending, and we have already received
4576 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004579{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004580 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004581 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004582
Hanno Becker8b09b732019-05-08 12:03:28 +01004583 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004584
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004586 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004587
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004588 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004589#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004590 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004591 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4592 ssl->conf->cid_len != 0 )
4593 {
4594 /* Shift pointers to account for record header including CID
4595 * struct {
4596 * ContentType special_type = tls12_cid;
4597 * ProtocolVersion version;
4598 * uint16 epoch;
4599 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004600 * opaque cid[cid_length]; // Additional field compared to
4601 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004602 * uint16 length;
4603 * opaque enc_content[DTLSCiphertext.length];
4604 * } DTLSCiphertext;
4605 */
4606
4607 /* So far, we only support static CID lengths
4608 * fixed in the configuration. */
4609 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4610 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4611 }
4612 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004613#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004614 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004617
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004618#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004619 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004620 * Section 4.1.2.7, that is, send alert only with TLS */
4621 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4622 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004623 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4624 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004625 }
4626#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004629 }
4630
4631 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004632 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4635 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 }
4637
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004638 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4641 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004642 }
4643
Hanno Becker8b09b732019-05-08 12:03:28 +01004644 /* Now that the total length of the record header is known, ensure
4645 * that the current datagram is large enough to hold it.
4646 * This would fail, for example, if we received a datagram of
4647 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4648 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4649 if( ret != 0 )
4650 {
4651 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4652 return( ret );
4653 }
4654 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4655
4656 /* Parse and validate record length
4657 * This must happen after the CID parsing because
4658 * its position in the record header depends on
4659 * the presence of a CID. */
4660
4661 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004662 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004663 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4666 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004667 }
4668
Hanno Becker8b09b732019-05-08 12:03:28 +01004669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004670 "version = [%d:%d], msglen = %d",
4671 ssl->in_msgtype,
4672 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004673
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004674 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004675 * DTLS-related tests.
4676 * Check epoch before checking length constraint because
4677 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4678 * message gets duplicated before the corresponding Finished message,
4679 * the second ChangeCipherSpec should be discarded because it belongs
4680 * to an old epoch, but not because its length is shorter than
4681 * the minimum record length for packets using the new record transform.
4682 * Note that these two kinds of failures are handled differently,
4683 * as an unexpected record is silently skipped but an invalid
4684 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004685 */
4686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004687 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004688 {
4689 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4690
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004691 /* Check epoch (and sequence number) with DTLS */
4692 if( rec_epoch != ssl->in_epoch )
4693 {
4694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4695 "expected %d, received %d",
4696 ssl->in_epoch, rec_epoch ) );
4697
4698#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4699 /*
4700 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4701 * access the first byte of record content (handshake type), as we
4702 * have an active transform (possibly iv_len != 0), so use the
4703 * fact that the record header len is 13 instead.
4704 */
4705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4706 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4707 rec_epoch == 0 &&
4708 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4709 ssl->in_left > 13 &&
4710 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4711 {
4712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4713 "from the same port" ) );
4714 return( ssl_handle_possible_reconnect( ssl ) );
4715 }
4716 else
4717#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004718 {
4719 /* Consider buffering the record. */
4720 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4723 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4724 }
4725
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004726 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004727 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004728 }
4729
4730#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4731 /* Replay detection only works for the current epoch */
4732 if( rec_epoch == ssl->in_epoch &&
4733 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4734 {
4735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4736 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4737 }
4738#endif
4739 }
4740#endif /* MBEDTLS_SSL_PROTO_DTLS */
4741
Hanno Becker52c6dc62017-05-26 16:07:36 +01004742
4743 /* Check length against bounds of the current transform and version */
4744 if( ssl->transform_in == NULL )
4745 {
4746 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004747 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004748 {
4749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4750 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4751 }
4752 }
4753 else
4754 {
4755 if( ssl->in_msglen < ssl->transform_in->minlen )
4756 {
4757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4758 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4759 }
4760
4761#if defined(MBEDTLS_SSL_PROTO_SSL3)
4762 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004763 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004764 {
4765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4766 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4767 }
4768#endif
4769#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4770 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4771 /*
4772 * TLS encrypted messages can have up to 256 bytes of padding
4773 */
4774 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4775 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004776 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004777 {
4778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4779 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4780 }
4781#endif
4782 }
4783
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004784 return( 0 );
4785}
Paul Bakker5121ce52009-01-03 21:22:43 +00004786
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004787/*
4788 * If applicable, decrypt (and decompress) record content
4789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004791{
4792 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004795 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4798 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802 ret = mbedtls_ssl_hw_record_read( ssl );
4803 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4806 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004807 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004808
4809 if( ret == 0 )
4810 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004811 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004813 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004815 mbedtls_record rec;
4816
4817 rec.buf = ssl->in_iv;
4818 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4819 - ( ssl->in_iv - ssl->in_buf );
4820 rec.data_len = ssl->in_msglen;
4821 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004822#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004823 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004824 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004825#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004826
4827 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4828 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4829 ssl->conf->transport, rec.ver );
4830 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004831 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4832 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004835
Hanno Beckera5a2b082019-05-15 14:03:01 +01004836#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004837 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4838 ssl->conf->ignore_unexpected_cid
4839 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4840 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004842 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004843 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004844#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004845
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 return( ret );
4847 }
4848
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004849 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004850 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004851 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4852 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004854
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004855 /* The record content type may change during decryption,
4856 * so re-read it. */
4857 ssl->in_msgtype = rec.type;
4858 /* Also update the input buffer, because unfortunately
4859 * the server-side ssl_parse_client_hello() reparses the
4860 * record header when receiving a ClientHello initiating
4861 * a renegotiation. */
4862 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004863 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004864 ssl->in_msglen = rec.data_len;
4865 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4866 ssl->in_len[1] = (unsigned char)( rec.data_len );
4867
Paul Bakker5121ce52009-01-03 21:22:43 +00004868 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4869 ssl->in_msg, ssl->in_msglen );
4870
Hanno Beckera5a2b082019-05-15 14:03:01 +01004871#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004872 /* We have already checked the record content type
4873 * in ssl_parse_record_header(), failing or silently
4874 * dropping the record in the case of an unknown type.
4875 *
4876 * Since with the use of CIDs, the record content type
4877 * might change during decryption, re-check the record
4878 * content type, but treat a failure as fatal this time. */
4879 if( ssl_check_record_type( ssl->in_msgtype ) )
4880 {
4881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4882 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4883 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004884#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004885
Angus Grattond8213d02016-05-25 20:56:48 +10004886 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4889 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004890 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004891 else if( ssl->in_msglen == 0 )
4892 {
4893#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4894 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4895 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4896 {
4897 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4899 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4900 }
4901#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4902
4903 ssl->nb_zero++;
4904
4905 /*
4906 * Three or more empty messages may be a DoS attack
4907 * (excessive CPU consumption).
4908 */
4909 if( ssl->nb_zero > 3 )
4910 {
4911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004912 "messages, possible DoS attack" ) );
4913 /* Treat the records as if they were not properly authenticated,
4914 * thereby failing the connection if we see more than allowed
4915 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004916 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4917 }
4918 }
4919 else
4920 ssl->nb_zero = 0;
4921
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004922 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4923#if defined(MBEDTLS_SSL_PROTO_TLS)
4924 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004925 {
4926 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004927 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004928 if( ++ssl->in_ctr[i - 1] != 0 )
4929 break;
4930
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004931 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004932 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004933 {
4934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4935 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4936 }
4937 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004938#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004939
Paul Bakker5121ce52009-01-03 21:22:43 +00004940 }
4941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004943 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004945 {
4946 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004948 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004949 return( ret );
4950 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004951 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004955 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004958 }
4959#endif
4960
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004961 return( 0 );
4962}
4963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004965
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004966/*
4967 * Read a record.
4968 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004969 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4970 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4971 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004972 */
Hanno Becker1097b342018-08-15 14:09:41 +01004973
4974/* Helper functions for mbedtls_ssl_read_record(). */
4975static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004976static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4977static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004978
Hanno Becker327c93b2018-08-15 13:56:18 +01004979int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004980 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004981{
4982 int ret;
4983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004985
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004986 if( ssl->keep_current_message == 0 )
4987 {
4988 do {
Simon Butcher99000142016-10-13 17:21:01 +01004989
Hanno Becker26994592018-08-15 14:14:59 +01004990 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004991 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004992 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004993
Hanno Beckere74d5562018-08-15 14:26:08 +01004994 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004995 {
Hanno Becker40f50842018-08-15 14:48:01 +01004996#if defined(MBEDTLS_SSL_PROTO_DTLS)
4997 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004998
Hanno Becker40f50842018-08-15 14:48:01 +01004999 /* We only check for buffered messages if the
5000 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005001 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005002 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005003 {
Hanno Becker40f50842018-08-15 14:48:01 +01005004 if( ssl_load_buffered_message( ssl ) == 0 )
5005 have_buffered = 1;
5006 }
5007
5008 if( have_buffered == 0 )
5009#endif /* MBEDTLS_SSL_PROTO_DTLS */
5010 {
5011 ret = ssl_get_next_record( ssl );
5012 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5013 continue;
5014
5015 if( ret != 0 )
5016 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005017 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005018 return( ret );
5019 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005020 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005021 }
5022
5023 ret = mbedtls_ssl_handle_message_type( ssl );
5024
Hanno Becker40f50842018-08-15 14:48:01 +01005025#if defined(MBEDTLS_SSL_PROTO_DTLS)
5026 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5027 {
5028 /* Buffer future message */
5029 ret = ssl_buffer_message( ssl );
5030 if( ret != 0 )
5031 return( ret );
5032
5033 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5034 }
5035#endif /* MBEDTLS_SSL_PROTO_DTLS */
5036
Hanno Becker90333da2017-10-10 11:27:13 +01005037 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5038 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005039
5040 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005041 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005042 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005043 return( ret );
5044 }
5045
Hanno Becker327c93b2018-08-15 13:56:18 +01005046 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005047 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005048 {
5049 mbedtls_ssl_update_handshake_status( ssl );
5050 }
Simon Butcher99000142016-10-13 17:21:01 +01005051 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005052 else
Simon Butcher99000142016-10-13 17:21:01 +01005053 {
Hanno Becker02f59072018-08-15 14:00:24 +01005054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005055 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005056 }
5057
5058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5059
5060 return( 0 );
5061}
5062
Hanno Becker40f50842018-08-15 14:48:01 +01005063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005064static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005065{
Hanno Becker40f50842018-08-15 14:48:01 +01005066 if( ssl->in_left > ssl->next_record_offset )
5067 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005068
Hanno Becker40f50842018-08-15 14:48:01 +01005069 return( 0 );
5070}
5071
5072static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5073{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005074 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005075 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005076 int ret = 0;
5077
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005078 if( hs == NULL )
5079 return( -1 );
5080
Hanno Beckere00ae372018-08-20 09:39:42 +01005081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5082
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005083 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5084 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5085 {
5086 /* Check if we have seen a ChangeCipherSpec before.
5087 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005088 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005089 {
5090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5091 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005092 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 }
5094
Hanno Becker39b8bc92018-08-28 17:17:13 +01005095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005096 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5097 ssl->in_msglen = 1;
5098 ssl->in_msg[0] = 1;
5099
5100 /* As long as they are equal, the exact value doesn't matter. */
5101 ssl->in_left = 0;
5102 ssl->next_record_offset = 0;
5103
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005104 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005105 goto exit;
5106 }
Hanno Becker37f95322018-08-16 13:55:32 +01005107
Hanno Beckerb8f50142018-08-28 10:01:34 +01005108#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005109 /* Debug only */
5110 {
5111 unsigned offset;
5112 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5113 {
5114 hs_buf = &hs->buffering.hs[offset];
5115 if( hs_buf->is_valid == 1 )
5116 {
5117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5118 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005119 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005120 }
5121 }
5122 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005123#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005124
5125 /* Check if we have buffered and/or fully reassembled the
5126 * next handshake message. */
5127 hs_buf = &hs->buffering.hs[0];
5128 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5129 {
5130 /* Synthesize a record containing the buffered HS message. */
5131 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5132 ( hs_buf->data[2] << 8 ) |
5133 hs_buf->data[3];
5134
5135 /* Double-check that we haven't accidentally buffered
5136 * a message that doesn't fit into the input buffer. */
5137 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5138 {
5139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5141 }
5142
5143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5144 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5145 hs_buf->data, msg_len + 12 );
5146
5147 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5148 ssl->in_hslen = msg_len + 12;
5149 ssl->in_msglen = msg_len + 12;
5150 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5151
5152 ret = 0;
5153 goto exit;
5154 }
5155 else
5156 {
5157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5158 hs->in_msg_seq ) );
5159 }
5160
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005161 ret = -1;
5162
5163exit:
5164
5165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5166 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005167}
5168
Hanno Beckera02b0b42018-08-21 17:20:27 +01005169static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5170 size_t desired )
5171{
5172 int offset;
5173 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5175 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005176
Hanno Becker01315ea2018-08-21 17:22:17 +01005177 /* Get rid of future records epoch first, if such exist. */
5178 ssl_free_buffered_record( ssl );
5179
5180 /* Check if we have enough space available now. */
5181 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5182 hs->buffering.total_bytes_buffered ) )
5183 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005185 return( 0 );
5186 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005187
Hanno Becker4f432ad2018-08-28 10:02:32 +01005188 /* We don't have enough space to buffer the next expected handshake
5189 * message. Remove buffers used for future messages to gain space,
5190 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5192 offset >= 0; offset-- )
5193 {
5194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5195 offset ) );
5196
Hanno Beckerb309b922018-08-23 13:18:05 +01005197 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005198
5199 /* Check if we have enough space available now. */
5200 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5201 hs->buffering.total_bytes_buffered ) )
5202 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005204 return( 0 );
5205 }
5206 }
5207
5208 return( -1 );
5209}
5210
Hanno Becker40f50842018-08-15 14:48:01 +01005211static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5212{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005213 int ret = 0;
5214 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5215
5216 if( hs == NULL )
5217 return( 0 );
5218
5219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5220
5221 switch( ssl->in_msgtype )
5222 {
5223 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005225
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005226 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005227 break;
5228
5229 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005230 {
5231 unsigned recv_msg_seq_offset;
5232 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5233 mbedtls_ssl_hs_buffer *hs_buf;
5234 size_t msg_len = ssl->in_hslen - 12;
5235
5236 /* We should never receive an old handshake
5237 * message - double-check nonetheless. */
5238 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5239 {
5240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5242 }
5243
5244 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5245 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5246 {
5247 /* Silently ignore -- message too far in the future */
5248 MBEDTLS_SSL_DEBUG_MSG( 2,
5249 ( "Ignore future HS message with sequence number %u, "
5250 "buffering window %u - %u",
5251 recv_msg_seq, ssl->handshake->in_msg_seq,
5252 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5253
5254 goto exit;
5255 }
5256
5257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5258 recv_msg_seq, recv_msg_seq_offset ) );
5259
5260 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5261
5262 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005263 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005264 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005265 size_t reassembly_buf_sz;
5266
Hanno Becker37f95322018-08-16 13:55:32 +01005267 hs_buf->is_fragmented =
5268 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5269
5270 /* We copy the message back into the input buffer
5271 * after reassembly, so check that it's not too large.
5272 * This is an implementation-specific limitation
5273 * and not one from the standard, hence it is not
5274 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005275 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005276 {
5277 /* Ignore message */
5278 goto exit;
5279 }
5280
Hanno Beckere0b150f2018-08-21 15:51:03 +01005281 /* Check if we have enough space to buffer the message. */
5282 if( hs->buffering.total_bytes_buffered >
5283 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5284 {
5285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5287 }
5288
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005289 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5290 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005291
5292 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5293 hs->buffering.total_bytes_buffered ) )
5294 {
5295 if( recv_msg_seq_offset > 0 )
5296 {
5297 /* If we can't buffer a future message because
5298 * of space limitations -- ignore. */
5299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5300 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5301 (unsigned) hs->buffering.total_bytes_buffered ) );
5302 goto exit;
5303 }
Hanno Beckere1801392018-08-21 16:51:05 +01005304 else
5305 {
5306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5307 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5308 (unsigned) hs->buffering.total_bytes_buffered ) );
5309 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005310
Hanno Beckera02b0b42018-08-21 17:20:27 +01005311 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005312 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5314 (unsigned) msg_len,
5315 (unsigned) reassembly_buf_sz,
5316 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005317 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005318 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5319 goto exit;
5320 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005321 }
5322
5323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5324 msg_len ) );
5325
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005326 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5327 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005328 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005329 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005330 goto exit;
5331 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005332 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005333
5334 /* Prepare final header: copy msg_type, length and message_seq,
5335 * then add standardised fragment_offset and fragment_length */
5336 memcpy( hs_buf->data, ssl->in_msg, 6 );
5337 memset( hs_buf->data + 6, 0, 3 );
5338 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5339
5340 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005341
5342 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005343 }
5344 else
5345 {
5346 /* Make sure msg_type and length are consistent */
5347 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5348 {
5349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5350 /* Ignore */
5351 goto exit;
5352 }
5353 }
5354
Hanno Becker4422bbb2018-08-20 09:40:19 +01005355 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005356 {
5357 size_t frag_len, frag_off;
5358 unsigned char * const msg = hs_buf->data + 12;
5359
5360 /*
5361 * Check and copy current fragment
5362 */
5363
5364 /* Validation of header fields already done in
5365 * mbedtls_ssl_prepare_handshake_record(). */
5366 frag_off = ssl_get_hs_frag_off( ssl );
5367 frag_len = ssl_get_hs_frag_len( ssl );
5368
5369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5370 frag_off, frag_len ) );
5371 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5372
5373 if( hs_buf->is_fragmented )
5374 {
5375 unsigned char * const bitmask = msg + msg_len;
5376 ssl_bitmask_set( bitmask, frag_off, frag_len );
5377 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5378 msg_len ) == 0 );
5379 }
5380 else
5381 {
5382 hs_buf->is_complete = 1;
5383 }
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5386 hs_buf->is_complete ? "" : "not yet " ) );
5387 }
5388
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005389 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005390 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005391
5392 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005393 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005394 break;
5395 }
5396
5397exit:
5398
5399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5400 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005401}
5402#endif /* MBEDTLS_SSL_PROTO_DTLS */
5403
Hanno Becker1097b342018-08-15 14:09:41 +01005404static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005405{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005406 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005407 * Consume last content-layer message and potentially
5408 * update in_msglen which keeps track of the contents'
5409 * consumption state.
5410 *
5411 * (1) Handshake messages:
5412 * Remove last handshake message, move content
5413 * and adapt in_msglen.
5414 *
5415 * (2) Alert messages:
5416 * Consume whole record content, in_msglen = 0.
5417 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005418 * (3) Change cipher spec:
5419 * Consume whole record content, in_msglen = 0.
5420 *
5421 * (4) Application data:
5422 * Don't do anything - the record layer provides
5423 * the application data as a stream transport
5424 * and consumes through mbedtls_ssl_read only.
5425 *
5426 */
5427
5428 /* Case (1): Handshake messages */
5429 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005430 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005431 /* Hard assertion to be sure that no application data
5432 * is in flight, as corrupting ssl->in_msglen during
5433 * ssl->in_offt != NULL is fatal. */
5434 if( ssl->in_offt != NULL )
5435 {
5436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5438 }
5439
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005440 /*
5441 * Get next Handshake message in the current record
5442 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005443
Hanno Becker4a810fb2017-05-24 16:27:30 +01005444 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005445 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005446 * current handshake content: If DTLS handshake
5447 * fragmentation is used, that's the fragment
5448 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005449 * size here is faulty and should be changed at
5450 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005451 * (2) While it doesn't seem to cause problems, one
5452 * has to be very careful not to assume that in_hslen
5453 * is always <= in_msglen in a sensible communication.
5454 * Again, it's wrong for DTLS handshake fragmentation.
5455 * The following check is therefore mandatory, and
5456 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005457 * Additionally, ssl->in_hslen might be arbitrarily out of
5458 * bounds after handling a DTLS message with an unexpected
5459 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005460 */
5461 if( ssl->in_hslen < ssl->in_msglen )
5462 {
5463 ssl->in_msglen -= ssl->in_hslen;
5464 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5465 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005466
Hanno Becker4a810fb2017-05-24 16:27:30 +01005467 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5468 ssl->in_msg, ssl->in_msglen );
5469 }
5470 else
5471 {
5472 ssl->in_msglen = 0;
5473 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005474
Hanno Becker4a810fb2017-05-24 16:27:30 +01005475 ssl->in_hslen = 0;
5476 }
5477 /* Case (4): Application data */
5478 else if( ssl->in_offt != NULL )
5479 {
5480 return( 0 );
5481 }
5482 /* Everything else (CCS & Alerts) */
5483 else
5484 {
5485 ssl->in_msglen = 0;
5486 }
5487
Hanno Becker1097b342018-08-15 14:09:41 +01005488 return( 0 );
5489}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005490
Hanno Beckere74d5562018-08-15 14:26:08 +01005491static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5492{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005493 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005494 return( 1 );
5495
5496 return( 0 );
5497}
5498
Hanno Becker5f066e72018-08-16 14:56:31 +01005499#if defined(MBEDTLS_SSL_PROTO_DTLS)
5500
5501static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5502{
5503 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5504 if( hs == NULL )
5505 return;
5506
Hanno Becker01315ea2018-08-21 17:22:17 +01005507 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005508 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005509 hs->buffering.total_bytes_buffered -=
5510 hs->buffering.future_record.len;
5511
5512 mbedtls_free( hs->buffering.future_record.data );
5513 hs->buffering.future_record.data = NULL;
5514 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005515}
5516
5517static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5518{
5519 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5520 unsigned char * rec;
5521 size_t rec_len;
5522 unsigned rec_epoch;
5523
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005524 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005525 return( 0 );
5526
5527 if( hs == NULL )
5528 return( 0 );
5529
Hanno Becker5f066e72018-08-16 14:56:31 +01005530 rec = hs->buffering.future_record.data;
5531 rec_len = hs->buffering.future_record.len;
5532 rec_epoch = hs->buffering.future_record.epoch;
5533
5534 if( rec == NULL )
5535 return( 0 );
5536
Hanno Becker4cb782d2018-08-20 11:19:05 +01005537 /* Only consider loading future records if the
5538 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005539 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005540 return( 0 );
5541
Hanno Becker5f066e72018-08-16 14:56:31 +01005542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5543
5544 if( rec_epoch != ssl->in_epoch )
5545 {
5546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5547 goto exit;
5548 }
5549
5550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5551
5552 /* Double-check that the record is not too large */
5553 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5554 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5555 {
5556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5558 }
5559
5560 memcpy( ssl->in_hdr, rec, rec_len );
5561 ssl->in_left = rec_len;
5562 ssl->next_record_offset = 0;
5563
5564 ssl_free_buffered_record( ssl );
5565
5566exit:
5567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5568 return( 0 );
5569}
5570
5571static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5572{
5573 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5574 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005575 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005576
5577 /* Don't buffer future records outside handshakes. */
5578 if( hs == NULL )
5579 return( 0 );
5580
5581 /* Only buffer handshake records (we are only interested
5582 * in Finished messages). */
5583 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5584 return( 0 );
5585
5586 /* Don't buffer more than one future epoch record. */
5587 if( hs->buffering.future_record.data != NULL )
5588 return( 0 );
5589
Hanno Becker01315ea2018-08-21 17:22:17 +01005590 /* Don't buffer record if there's not enough buffering space remaining. */
5591 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5592 hs->buffering.total_bytes_buffered ) )
5593 {
5594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5595 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5596 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005597 return( 0 );
5598 }
5599
Hanno Becker5f066e72018-08-16 14:56:31 +01005600 /* Buffer record */
5601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5602 ssl->in_epoch + 1 ) );
5603 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5604 rec_hdr_len + ssl->in_msglen );
5605
5606 /* ssl_parse_record_header() only considers records
5607 * of the next epoch as candidates for buffering. */
5608 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005609 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005610
5611 hs->buffering.future_record.data =
5612 mbedtls_calloc( 1, hs->buffering.future_record.len );
5613 if( hs->buffering.future_record.data == NULL )
5614 {
5615 /* If we run out of RAM trying to buffer a
5616 * record from the next epoch, just ignore. */
5617 return( 0 );
5618 }
5619
Hanno Becker01315ea2018-08-21 17:22:17 +01005620 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005621
Hanno Becker01315ea2018-08-21 17:22:17 +01005622 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005623 return( 0 );
5624}
5625
5626#endif /* MBEDTLS_SSL_PROTO_DTLS */
5627
Hanno Beckere74d5562018-08-15 14:26:08 +01005628static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005629{
5630 int ret;
5631
Hanno Becker5f066e72018-08-16 14:56:31 +01005632#if defined(MBEDTLS_SSL_PROTO_DTLS)
5633 /* We might have buffered a future record; if so,
5634 * and if the epoch matches now, load it.
5635 * On success, this call will set ssl->in_left to
5636 * the length of the buffered record, so that
5637 * the calls to ssl_fetch_input() below will
5638 * essentially be no-ops. */
5639 ret = ssl_load_buffered_record( ssl );
5640 if( ret != 0 )
5641 return( ret );
5642#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005643
Hanno Becker8b09b732019-05-08 12:03:28 +01005644 /* Reset in pointers to default state for TLS/DTLS records,
5645 * assuming no CID and no offset between record content and
5646 * record plaintext. */
5647 ssl_update_in_pointers( ssl );
5648
5649 /* Ensure that we have enough space available for the default form
5650 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5651 * with no space for CIDs counted in). */
5652 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5653 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005656 return( ret );
5657 }
5658
5659 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005662 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005663 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005664 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005665 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5666 {
5667 ret = ssl_buffer_future_record( ssl );
5668 if( ret != 0 )
5669 return( ret );
5670
5671 /* Fall through to handling of unexpected records */
5672 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5673 }
5674
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005675 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5676 {
5677 /* Skip unexpected record (but not whole datagram) */
5678 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005679 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005680
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5682 "(header)" ) );
5683 }
5684 else
5685 {
5686 /* Skip invalid record and the rest of the datagram */
5687 ssl->next_record_offset = 0;
5688 ssl->in_left = 0;
5689
5690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5691 "(header)" ) );
5692 }
5693
5694 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005695 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005696 }
5697#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005698 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005699 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005700
5701 /*
5702 * Read and optionally decrypt the message contents
5703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005705 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005708 return( ret );
5709 }
5710
5711 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005713 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005714 {
Hanno Becker43395762019-05-03 14:46:38 +01005715 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005716 if( ssl->next_record_offset < ssl->in_left )
5717 {
5718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5719 }
5720 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005721 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005723#if defined(MBEDTLS_SSL_PROTO_TLS)
5724 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005725 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005726 }
5727#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005728
5729 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005732 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005733 {
5734 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005735 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005736 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005737 /* Except when waiting for Finished as a bad mac here
5738 * probably means something went wrong in the handshake
5739 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5740 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5741 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5742 {
5743#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5744 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5745 {
5746 mbedtls_ssl_send_alert_message( ssl,
5747 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5748 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5749 }
5750#endif
5751 return( ret );
5752 }
5753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005755 if( ssl->conf->badmac_limit != 0 &&
5756 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5759 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005760 }
5761#endif
5762
Hanno Becker4a810fb2017-05-24 16:27:30 +01005763 /* As above, invalid records cause
5764 * dismissal of the whole datagram. */
5765
5766 ssl->next_record_offset = 0;
5767 ssl->in_left = 0;
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005770 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005771 }
5772
5773 return( ret );
5774 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005775 MBEDTLS_SSL_TRANSPORT_ELSE
5776#endif /* MBEDTLS_SSL_PROTO_DTLS */
5777#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005778 {
5779 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5781 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783 mbedtls_ssl_send_alert_message( ssl,
5784 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5785 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005786 }
5787#endif
5788 return( ret );
5789 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005790#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005791 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005792
Simon Butcher99000142016-10-13 17:21:01 +01005793 return( 0 );
5794}
5795
5796int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5797{
5798 int ret;
5799
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005800 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005801 * Handle particular types of records
5802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005804 {
Simon Butcher99000142016-10-13 17:21:01 +01005805 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5806 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005807 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 }
5810
Hanno Beckere678eaa2018-08-21 14:57:46 +01005811 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005812 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005813 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005814 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5816 ssl->in_msglen ) );
5817 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005818 }
5819
Hanno Beckere678eaa2018-08-21 14:57:46 +01005820 if( ssl->in_msg[0] != 1 )
5821 {
5822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5823 ssl->in_msg[0] ) );
5824 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5825 }
5826
5827#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005828 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005829 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5830 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5831 {
5832 if( ssl->handshake == NULL )
5833 {
5834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5835 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5836 }
5837
5838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5839 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5840 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005841#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005842 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005846 if( ssl->in_msglen != 2 )
5847 {
5848 /* Note: Standard allows for more than one 2 byte alert
5849 to be packed in a single message, but Mbed TLS doesn't
5850 currently support this. */
5851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5852 ssl->in_msglen ) );
5853 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5854 }
5855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005857 ssl->in_msg[0], ssl->in_msg[1] ) );
5858
5859 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005860 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005865 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005867 }
5868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5870 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5873 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005874 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005875
5876#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5877 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5878 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5879 {
Hanno Becker90333da2017-10-10 11:27:13 +01005880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005881 /* Will be handled when trying to parse ServerHello */
5882 return( 0 );
5883 }
5884#endif
5885
5886#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5887 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5888 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5889 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5890 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5891 {
5892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5893 /* Will be handled in mbedtls_ssl_parse_certificate() */
5894 return( 0 );
5895 }
5896#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5897
5898 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005899 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005900 }
5901
Hanno Beckerc76c6192017-06-06 10:03:17 +01005902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005903 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005904 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005905 /* Drop unexpected ApplicationData records,
5906 * except at the beginning of renegotiations */
5907 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5908 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5909#if defined(MBEDTLS_SSL_RENEGOTIATION)
5910 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5911 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005912#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005913 )
5914 {
5915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5916 return( MBEDTLS_ERR_SSL_NON_FATAL );
5917 }
5918
5919 if( ssl->handshake != NULL &&
5920 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5921 {
5922 ssl_handshake_wrapup_free_hs_transform( ssl );
5923 }
5924 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005925#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005926
Paul Bakker5121ce52009-01-03 21:22:43 +00005927 return( 0 );
5928}
5929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005931{
5932 int ret;
5933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5935 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5936 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005937 {
5938 return( ret );
5939 }
5940
5941 return( 0 );
5942}
5943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005945 unsigned char level,
5946 unsigned char message )
5947{
5948 int ret;
5949
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005950 if( ssl == NULL || ssl->conf == NULL )
5951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005957 ssl->out_msglen = 2;
5958 ssl->out_msg[0] = level;
5959 ssl->out_msg[1] = message;
5960
Hanno Becker67bc7c32018-08-06 11:33:50 +01005961 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005964 return( ret );
5965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005967
5968 return( 0 );
5969}
5970
Paul Bakker5121ce52009-01-03 21:22:43 +00005971/*
5972 * Handshake functions
5973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5975 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5976 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5977 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5978 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5979 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5980 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005981/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005983{
Hanno Becker8759e162017-12-27 21:34:08 +00005984 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5989 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005990 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5991 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005994 ssl->state++;
5995 return( 0 );
5996 }
5997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006000}
6001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006003{
Hanno Becker8759e162017-12-27 21:34:08 +00006004 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6009 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006010 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6011 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006014 ssl->state++;
6015 return( 0 );
6016 }
6017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006020}
Gilles Peskinef9828522017-05-03 12:28:43 +02006021
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006022#else
Gilles Peskinef9828522017-05-03 12:28:43 +02006023/* Some certificate support -> implement write and parse */
6024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006028 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006030 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6035 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006036 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6037 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040 ssl->state++;
6041 return( 0 );
6042 }
6043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006045 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 {
6047 if( ssl->client_auth == 0 )
6048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 ssl->state++;
6051 return( 0 );
6052 }
6053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 /*
6056 * If using SSLv3 and got no cert, send an Alert message
6057 * (otherwise an empty Certificate message will be sent).
6058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6060 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 {
6062 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6064 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6065 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 goto write_msg;
6069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072#endif /* MBEDTLS_SSL_CLI_C */
6073#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006074 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6079 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006080 }
6081 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006085
6086 /*
6087 * 0 . 0 handshake type
6088 * 1 . 3 handshake length
6089 * 4 . 6 length of all certs
6090 * 7 . 9 length of cert. 1
6091 * 10 . n-1 peer certificate
6092 * n . n+2 length of cert. 2
6093 * n+3 . ... upper level cert, etc.
6094 */
6095 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
Paul Bakker29087132010-03-21 21:03:34 +00006098 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
6100 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006101 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006104 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 }
6107
6108 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6109 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6110 ssl->out_msg[i + 2] = (unsigned char)( n );
6111
6112 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6113 i += n; crt = crt->next;
6114 }
6115
6116 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6117 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6118 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6119
6120 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6122 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006123
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006124#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006125write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006127
6128 ssl->state++;
6129
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006130 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 return( ret );
6134 }
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
Paul Bakkered27a042013-04-18 22:46:23 +02006138 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006139}
6140
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006141/*
6142 * Once the certificate message is read, parse it into a cert chain and
6143 * perform basic checks, but leave actual verification to the caller
6144 */
6145static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006146{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006147 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006148 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006149 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151#if defined(MBEDTLS_SSL_SRV_C)
6152#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006153 /*
6154 * Check if the client sent an empty certificate
6155 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006156 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006158 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006159 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6161 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6162 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006165
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006166 /* The client was asked for a certificate but didn't send
6167 one. The client should know what's going on, so we
6168 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006169 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006170 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006171 }
6172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6176 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006177 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6181 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6182 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6183 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006186
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006187 /* The client was asked for a certificate but didn't send
6188 one. The client should know what's going on, so we
6189 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006190 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006191 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006192 }
6193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6195 MBEDTLS_SSL_PROTO_TLS1_2 */
6196#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006201 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6202 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006204 }
6205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6207 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006210 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6211 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006213 }
6214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006216
Paul Bakker5121ce52009-01-03 21:22:43 +00006217 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006220 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006221
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006222 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006226 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6227 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006229 }
6230
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006231 /* In case we tried to reuse a session but it failed */
6232 if( ssl->session_negotiate->peer_cert != NULL )
6233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6235 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006236 }
6237
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006238 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006240 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006243 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6244 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006245 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 }
6247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006249
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006250 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006251
6252 while( i < ssl->in_hslen )
6253 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006254 if ( i + 3 > ssl->in_hslen ) {
6255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6256 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6257 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6258 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6259 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 if( ssl->in_msg[i] != 0 )
6261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6264 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006266 }
6267
6268 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6269 | (unsigned int) ssl->in_msg[i + 2];
6270 i += 3;
6271
6272 if( n < 128 || i + n > ssl->in_hslen )
6273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006275 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6276 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006277 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006278 }
6279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006281 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006282 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006283 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006284 case 0: /*ok*/
6285 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6286 /* Ignore certificate with an unknown algorithm: maybe a
6287 prior certificate was already trusted. */
6288 break;
6289
6290 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6291 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6292 goto crt_parse_der_failed;
6293
6294 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6295 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6296 goto crt_parse_der_failed;
6297
6298 default:
6299 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6300 crt_parse_der_failed:
6301 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006303 return( ret );
6304 }
6305
6306 i += n;
6307 }
6308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006310
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006311 /*
6312 * On client, make sure the server cert doesn't change during renego to
6313 * avoid "triple handshake" attack: https://secure-resumption.com/
6314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006316 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006318 {
6319 if( ssl->session->peer_cert == NULL )
6320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006322 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6323 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006324 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006325 }
6326
6327 if( ssl->session->peer_cert->raw.len !=
6328 ssl->session_negotiate->peer_cert->raw.len ||
6329 memcmp( ssl->session->peer_cert->raw.p,
6330 ssl->session_negotiate->peer_cert->raw.p,
6331 ssl->session->peer_cert->raw.len ) != 0 )
6332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6335 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006337 }
6338 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006340
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006341 return( 0 );
6342}
6343
6344int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6345{
6346 int ret;
6347 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006348 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006349#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6350 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6351 ? ssl->handshake->sni_authmode
6352 : ssl->conf->authmode;
6353#else
6354 const int authmode = ssl->conf->authmode;
6355#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006356 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006357
6358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6359
6360 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6361 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6362 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6363 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6364 {
6365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6366 ssl->state++;
6367 return( 0 );
6368 }
6369
6370#if defined(MBEDTLS_SSL_SRV_C)
6371 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6372 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6373 {
6374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6375 ssl->state++;
6376 return( 0 );
6377 }
6378
6379 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6380 authmode == MBEDTLS_SSL_VERIFY_NONE )
6381 {
6382 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006384
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006385 ssl->state++;
6386 return( 0 );
6387 }
6388#endif
6389
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006390#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6391 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006392 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006393 {
6394 goto crt_verify;
6395 }
6396#endif
6397
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006398 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006399 {
6400 /* mbedtls_ssl_read_record may have sent an alert already. We
6401 let it decide whether to alert. */
6402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6403 return( ret );
6404 }
6405
6406 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6407 {
6408#if defined(MBEDTLS_SSL_SRV_C)
6409 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6410 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6411 {
6412 ret = 0;
6413 }
6414#endif
6415
6416 ssl->state++;
6417 return( ret );
6418 }
6419
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006420#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6421 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006422 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006423
6424crt_verify:
6425 if( ssl->handshake->ecrs_enabled)
6426 rs_ctx = &ssl->handshake->ecrs_ctx;
6427#endif
6428
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006429 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006430 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006431 mbedtls_x509_crt *ca_chain;
6432 mbedtls_x509_crl *ca_crl;
6433
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006434#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006435 if( ssl->handshake->sni_ca_chain != NULL )
6436 {
6437 ca_chain = ssl->handshake->sni_ca_chain;
6438 ca_crl = ssl->handshake->sni_ca_crl;
6439 }
6440 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006441#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006442 {
6443 ca_chain = ssl->conf->ca_chain;
6444 ca_crl = ssl->conf->ca_crl;
6445 }
6446
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006447 /*
6448 * Main check: verify certificate
6449 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006450 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006451 ssl->session_negotiate->peer_cert,
6452 ca_chain, ca_crl,
6453 ssl->conf->cert_profile,
6454 ssl->hostname,
6455 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006456 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006457
6458 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006461 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006462
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006463#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6464 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006465 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006466#endif
6467
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006468 /*
6469 * Secondary checks: always done, but change 'ret' only if it was 0
6470 */
6471
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006472#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006475
6476 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006478 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006479 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006480 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006483 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006485 }
6486 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006487#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006490 ciphersuite_info,
6491 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006492 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006495 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006497 }
6498
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006499 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6500 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6501 * with details encoded in the verification flags. All other kinds
6502 * of error codes, including those from the user provided f_vrfy
6503 * functions, are treated as fatal and lead to a failure of
6504 * ssl_parse_certificate even if verification was optional. */
6505 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6506 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6507 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6508 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006509 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006510 }
6511
6512 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6513 {
6514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6515 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6516 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006517
6518 if( ret != 0 )
6519 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006520 uint8_t alert;
6521
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006522 /* The certificate may have been rejected for several reasons.
6523 Pick one and send the corresponding alert. Which alert to send
6524 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006525 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6526 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6527 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6528 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6529 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6530 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6531 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6532 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6533 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6534 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6535 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6536 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6537 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6538 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6539 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6540 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6541 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6542 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6543 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6544 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006545 else
6546 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006547 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6548 alert );
6549 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006550
Hanno Beckere6706e62017-05-15 16:05:15 +01006551#if defined(MBEDTLS_DEBUG_C)
6552 if( ssl->session_negotiate->verify_result != 0 )
6553 {
6554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6555 ssl->session_negotiate->verify_result ) );
6556 }
6557 else
6558 {
6559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6560 }
6561#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006562 }
6563
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006564 ssl->state++;
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006567
6568 return( ret );
6569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6571 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6572 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6573 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6574 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6575 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6576 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006579{
6580 int ret;
6581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006585 ssl->out_msglen = 1;
6586 ssl->out_msg[0] = 1;
6587
Paul Bakker5121ce52009-01-03 21:22:43 +00006588 ssl->state++;
6589
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006590 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006593 return( ret );
6594 }
6595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006597
6598 return( 0 );
6599}
6600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006602{
6603 int ret;
6604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006606
Hanno Becker327c93b2018-08-15 13:56:18 +01006607 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006610 return( ret );
6611 }
6612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006616 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6617 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006619 }
6620
Hanno Beckere678eaa2018-08-21 14:57:46 +01006621 /* CCS records are only accepted if they have length 1 and content '1',
6622 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006623
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006624 /*
6625 * Switch to our negotiated transform and session parameters for inbound
6626 * data.
6627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006629 ssl->transform_in = ssl->transform_negotiate;
6630 ssl->session_in = ssl->session_negotiate;
6631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006633 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006636 ssl_dtls_replay_reset( ssl );
6637#endif
6638
6639 /* Increment epoch */
6640 if( ++ssl->in_epoch == 0 )
6641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006643 /* This is highly unlikely to happen for legitimate reasons, so
6644 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006646 }
6647 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006648 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006650#if defined(MBEDTLS_SSL_PROTO_TLS)
6651 {
6652 memset( ssl->in_ctr, 0, 8 );
6653 }
6654#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006655
Hanno Beckerf5970a02019-05-08 09:38:41 +01006656 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6659 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006664 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6665 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006667 }
6668 }
6669#endif
6670
Paul Bakker5121ce52009-01-03 21:22:43 +00006671 ssl->state++;
6672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006674
6675 return( 0 );
6676}
6677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6679 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006680{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006681 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6684 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6685 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006686 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006687 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006688#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6690#if defined(MBEDTLS_SHA512_C)
6691 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006692 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6693 else
6694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695#if defined(MBEDTLS_SHA256_C)
6696 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006697 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006698 else
6699#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006703 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006704 }
Paul Bakker380da532012-04-18 16:10:25 +00006705}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6710 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006711 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6712 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006713#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6715#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006716 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006717#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006719 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006722}
6723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006725 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6728 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006729 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6730 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006731#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6733#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006734 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006737 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006740}
6741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6743 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6744static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006745 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006746{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006747 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6748 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006749}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006750#endif
Paul Bakker380da532012-04-18 16:10:25 +00006751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6753#if defined(MBEDTLS_SHA256_C)
6754static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006755 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006756{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006757 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006758}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006759#endif
Paul Bakker380da532012-04-18 16:10:25 +00006760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761#if defined(MBEDTLS_SHA512_C)
6762static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006763 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006764{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006765 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006766}
Paul Bakker769075d2012-11-24 11:26:46 +01006767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006771static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006773{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006774 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006775 mbedtls_md5_context md5;
6776 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006777
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 unsigned char padbuf[48];
6779 unsigned char md5sum[16];
6780 unsigned char sha1sum[20];
6781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006783 if( !session )
6784 session = ssl->session;
6785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006787
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006788 mbedtls_md5_init( &md5 );
6789 mbedtls_sha1_init( &sha1 );
6790
6791 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6792 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006793
6794 /*
6795 * SSLv3:
6796 * hash =
6797 * MD5( master + pad2 +
6798 * MD5( handshake + sender + master + pad1 ) )
6799 * + SHA1( master + pad2 +
6800 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006801 */
6802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006804 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6805 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006806#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006809 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6810 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006811#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006814 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006815
Paul Bakker1ef83d62012-04-11 12:09:53 +00006816 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006817
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006818 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6819 mbedtls_md5_update_ret( &md5, session->master, 48 );
6820 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6821 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006823 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6824 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6825 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6826 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
Paul Bakker1ef83d62012-04-11 12:09:53 +00006828 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006829
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006830 mbedtls_md5_starts_ret( &md5 );
6831 mbedtls_md5_update_ret( &md5, session->master, 48 );
6832 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6833 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6834 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006835
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006836 mbedtls_sha1_starts_ret( &sha1 );
6837 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6838 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6839 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6840 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006843
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006844 mbedtls_md5_free( &md5 );
6845 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006846
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006847 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6848 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6849 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006856static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006858{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006859 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006860 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006861 mbedtls_md5_context md5;
6862 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006863 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006866 if( !session )
6867 session = ssl->session;
6868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006870
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006871 mbedtls_md5_init( &md5 );
6872 mbedtls_sha1_init( &sha1 );
6873
6874 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6875 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006876
Paul Bakker1ef83d62012-04-11 12:09:53 +00006877 /*
6878 * TLSv1:
6879 * hash = PRF( master, finished_label,
6880 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6881 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006884 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6885 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006886#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006889 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6890 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006891#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006894 ? "client finished"
6895 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006896
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006897 mbedtls_md5_finish_ret( &md5, padbuf );
6898 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006899
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006900 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006901 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006904
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006905 mbedtls_md5_free( &md5 );
6906 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006907
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006908 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6915#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006916static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006918{
6919 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006920 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006921 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006922 unsigned char padbuf[32];
6923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006925 if( !session )
6926 session = ssl->session;
6927
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006928 mbedtls_sha256_init( &sha256 );
6929
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006931
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006932 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006933
6934 /*
6935 * TLSv1.2:
6936 * hash = PRF( master, finished_label,
6937 * Hash( handshake ) )[0.11]
6938 */
6939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940#if !defined(MBEDTLS_SHA256_ALT)
6941 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006942 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006943#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006946 ? "client finished"
6947 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006948
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006949 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006950
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006951 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006952 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006955
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006956 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006957
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006958 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006965static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006967{
6968 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006969 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006970 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006971 unsigned char padbuf[48];
6972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006974 if( !session )
6975 session = ssl->session;
6976
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006977 mbedtls_sha512_init( &sha512 );
6978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006980
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006981 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006982
6983 /*
6984 * TLSv1.2:
6985 * hash = PRF( master, finished_label,
6986 * Hash( handshake ) )[0.11]
6987 */
6988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006990 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6991 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006992#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006995 ? "client finished"
6996 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006997
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006998 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006999
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007000 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007001 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007004
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007005 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007006
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007007 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011#endif /* MBEDTLS_SHA512_C */
7012#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007017
7018 /*
7019 * Free our handshake params
7020 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007021 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007023 ssl->handshake = NULL;
7024
7025 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007026 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007027 */
7028 if( ssl->transform )
7029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 mbedtls_ssl_transform_free( ssl->transform );
7031 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007032 }
7033 ssl->transform = ssl->transform_negotiate;
7034 ssl->transform_negotiate = NULL;
7035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007037}
7038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007040{
7041 int resume = ssl->handshake->resume;
7042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#if defined(MBEDTLS_SSL_RENEGOTIATION)
7046 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007049 ssl->renego_records_seen = 0;
7050 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007051#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007052
7053 /*
7054 * Free the previous session and switch in the current one
7055 */
Paul Bakker0a597072012-09-25 21:55:46 +00007056 if( ssl->session )
7057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007059 /* RFC 7366 3.1: keep the EtM state */
7060 ssl->session_negotiate->encrypt_then_mac =
7061 ssl->session->encrypt_then_mac;
7062#endif
7063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 mbedtls_ssl_session_free( ssl->session );
7065 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007066 }
7067 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007068 ssl->session_negotiate = NULL;
7069
Paul Bakker0a597072012-09-25 21:55:46 +00007070 /*
7071 * Add cache entry
7072 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007073 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007074 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007075 resume == 0 )
7076 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007077 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007079 }
Paul Bakker0a597072012-09-25 21:55:46 +00007080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007082 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007083 ssl->handshake->flight != NULL )
7084 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007085 /* Cancel handshake timer */
7086 ssl_set_timer( ssl, 0 );
7087
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007088 /* Keep last flight around in case we need to resend it:
7089 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007091 }
7092 else
7093#endif
7094 ssl_handshake_wrapup_free_hs_transform( ssl );
7095
Paul Bakker48916f92012-09-16 19:57:18 +00007096 ssl->state++;
7097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007099}
7100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007102{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007103 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007107 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007108
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007109 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007110
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007111 /*
7112 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7113 * may define some other value. Currently (early 2016), no defined
7114 * ciphersuite does this (and this is unlikely to change as activity has
7115 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007120 ssl->verify_data_len = hash_len;
7121 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007122#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007123
Paul Bakker5121ce52009-01-03 21:22:43 +00007124 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7126 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007127
7128 /*
7129 * In case of session resuming, invert the client and server
7130 * ChangeCipherSpec messages order.
7131 */
Paul Bakker0a597072012-09-25 21:55:46 +00007132 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007135 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007139 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007141#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007142 }
7143 else
7144 ssl->state++;
7145
Paul Bakker48916f92012-09-16 19:57:18 +00007146 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007147 * Switch to our negotiated transform and session parameters for outbound
7148 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007153 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007154 {
7155 unsigned char i;
7156
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007157 /* Remember current epoch settings for resending */
7158 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007159 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007160
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007161 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007162 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007163
7164 /* Increment epoch */
7165 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007166 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007167 break;
7168
7169 /* The loop goes to its end iff the counter is wrapping */
7170 if( i == 0 )
7171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7173 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007174 }
7175 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007176 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007178#if defined(MBEDTLS_SSL_PROTO_TLS)
7179 {
7180 memset( ssl->cur_out_ctr, 0, 8 );
7181 }
7182#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007183
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007184 ssl->transform_out = ssl->transform_negotiate;
7185 ssl->session_out = ssl->session_negotiate;
7186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7188 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7193 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007194 }
7195 }
7196#endif
7197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007199 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007201#endif
7202
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007203 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007204 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007206 return( ret );
7207 }
7208
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007210 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007211 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7212 {
7213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7214 return( ret );
7215 }
7216#endif
7217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007219
7220 return( 0 );
7221}
7222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007224#define SSL_MAX_HASH_LEN 36
7225#else
7226#define SSL_MAX_HASH_LEN 12
7227#endif
7228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007230{
Paul Bakker23986e52011-04-24 08:57:21 +00007231 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007232 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007233 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007236
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007237 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007238
Hanno Becker327c93b2018-08-15 13:56:18 +01007239 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007242 return( ret );
7243 }
7244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007248 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7249 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007251 }
7252
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007253 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254#if defined(MBEDTLS_SSL_PROTO_SSL3)
7255 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007256 hash_len = 36;
7257 else
7258#endif
7259 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7262 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7266 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007268 }
7269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007271 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007274 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7275 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007277 }
7278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007280 ssl->verify_data_len = hash_len;
7281 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007282#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007283
Paul Bakker0a597072012-09-25 21:55:46 +00007284 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007287 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007289#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007291 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007293#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007294 }
7295 else
7296 ssl->state++;
7297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007299 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007301#endif
7302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007304
7305 return( 0 );
7306}
7307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7313 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7314 mbedtls_md5_init( &handshake->fin_md5 );
7315 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007316 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7317 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7320#if defined(MBEDTLS_SHA256_C)
7321 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007322 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007323#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_SHA512_C)
7325 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007326 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007327#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007329
7330 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007331
7332#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7333 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7334 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7335#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337#if defined(MBEDTLS_DHM_C)
7338 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007339#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340#if defined(MBEDTLS_ECDH_C)
7341 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007342#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007343#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007344 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007345#if defined(MBEDTLS_SSL_CLI_C)
7346 handshake->ecjpake_cache = NULL;
7347 handshake->ecjpake_cache_len = 0;
7348#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007349#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007350
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007351#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007352 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007353#endif
7354
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007355#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7356 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7357#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007358}
7359
Hanno Becker611a83b2018-01-03 14:27:32 +00007360void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7365 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007366
Hanno Becker92231322018-01-03 15:32:51 +00007367#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 mbedtls_md_init( &transform->md_ctx_enc );
7369 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007370#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007371}
7372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007374{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007376}
7377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007379{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007380 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007381 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007383 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007385 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007386 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007387
7388 /*
7389 * Either the pointers are now NULL or cleared properly and can be freed.
7390 * Now allocate missing structures.
7391 */
7392 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007393 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007394 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007395 }
Paul Bakker48916f92012-09-16 19:57:18 +00007396
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007397 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007398 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007399 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007400 }
Paul Bakker48916f92012-09-16 19:57:18 +00007401
Paul Bakker82788fb2014-10-20 13:59:19 +02007402 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007403 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007404 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007405 }
Paul Bakker48916f92012-09-16 19:57:18 +00007406
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007407 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007408 if( ssl->handshake == NULL ||
7409 ssl->transform_negotiate == NULL ||
7410 ssl->session_negotiate == NULL )
7411 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 mbedtls_free( ssl->handshake );
7415 mbedtls_free( ssl->transform_negotiate );
7416 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007417
7418 ssl->handshake = NULL;
7419 ssl->transform_negotiate = NULL;
7420 ssl->session_negotiate = NULL;
7421
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007422 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007423 }
7424
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007425 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007427 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007428 ssl_handshake_params_init( ssl->handshake );
7429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007431 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007432 {
7433 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007434
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007435 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7436 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7437 else
7438 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007439
7440 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007441 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007442#endif
7443
Paul Bakker48916f92012-09-16 19:57:18 +00007444 return( 0 );
7445}
7446
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007447#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007448/* Dummy cookie callbacks for defaults */
7449static int ssl_cookie_write_dummy( void *ctx,
7450 unsigned char **p, unsigned char *end,
7451 const unsigned char *cli_id, size_t cli_id_len )
7452{
7453 ((void) ctx);
7454 ((void) p);
7455 ((void) end);
7456 ((void) cli_id);
7457 ((void) cli_id_len);
7458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007460}
7461
7462static int ssl_cookie_check_dummy( void *ctx,
7463 const unsigned char *cookie, size_t cookie_len,
7464 const unsigned char *cli_id, size_t cli_id_len )
7465{
7466 ((void) ctx);
7467 ((void) cookie);
7468 ((void) cookie_len);
7469 ((void) cli_id);
7470 ((void) cli_id_len);
7471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007473}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007474#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007475
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007476/* Once ssl->out_hdr as the address of the beginning of the
7477 * next outgoing record is set, deduce the other pointers.
7478 *
7479 * Note: For TLS, we save the implicit record sequence number
7480 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7481 * and the caller has to make sure there's space for this.
7482 */
7483
7484static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7485 mbedtls_ssl_transform *transform )
7486{
7487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007488 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007489 {
7490 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007491#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007492 ssl->out_cid = ssl->out_ctr + 8;
7493 ssl->out_len = ssl->out_cid;
7494 if( transform != NULL )
7495 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007496#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007497 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007498#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007499 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007500 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007501 MBEDTLS_SSL_TRANSPORT_ELSE
7502#endif /* MBEDTLS_SSL_PROTO_DTLS */
7503#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007504 {
7505 ssl->out_ctr = ssl->out_hdr - 8;
7506 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007507#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007508 ssl->out_cid = ssl->out_len;
7509#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007510 ssl->out_iv = ssl->out_hdr + 5;
7511 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007512#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007513
7514 /* Adjust out_msg to make space for explicit IV, if used. */
7515 if( transform != NULL &&
7516 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7517 {
7518 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7519 }
7520 else
7521 ssl->out_msg = ssl->out_iv;
7522}
7523
7524/* Once ssl->in_hdr as the address of the beginning of the
7525 * next incoming record is set, deduce the other pointers.
7526 *
7527 * Note: For TLS, we save the implicit record sequence number
7528 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7529 * and the caller has to make sure there's space for this.
7530 */
7531
Hanno Beckerf5970a02019-05-08 09:38:41 +01007532static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007533{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007534 /* This function sets the pointers to match the case
7535 * of unprotected TLS/DTLS records, with both ssl->in_iv
7536 * and ssl->in_msg pointing to the beginning of the record
7537 * content.
7538 *
7539 * When decrypting a protected record, ssl->in_msg
7540 * will be shifted to point to the beginning of the
7541 * record plaintext.
7542 */
7543
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007544#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007545 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007546 {
Hanno Becker70e79282019-05-03 14:34:53 +01007547 /* This sets the header pointers to match records
7548 * without CID. When we receive a record containing
7549 * a CID, the fields are shifted accordingly in
7550 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007551 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007552#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007553 ssl->in_cid = ssl->in_ctr + 8;
7554 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007555#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007556 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007557#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007558 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007559 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007560 MBEDTLS_SSL_TRANSPORT_ELSE
7561#endif /* MBEDTLS_SSL_PROTO_DTLS */
7562#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007563 {
7564 ssl->in_ctr = ssl->in_hdr - 8;
7565 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007566#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007567 ssl->in_cid = ssl->in_len;
7568#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007569 ssl->in_iv = ssl->in_hdr + 5;
7570 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007571#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007572
Hanno Beckerf5970a02019-05-08 09:38:41 +01007573 /* This will be adjusted at record decryption time. */
7574 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007575}
7576
Paul Bakker5121ce52009-01-03 21:22:43 +00007577/*
7578 * Initialize an SSL context
7579 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007580void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7581{
7582 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7583}
7584
7585/*
7586 * Setup an SSL context
7587 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007588
7589static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7590{
7591 /* Set the incoming and outgoing record pointers. */
7592#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007593 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007594 {
7595 ssl->out_hdr = ssl->out_buf;
7596 ssl->in_hdr = ssl->in_buf;
7597 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007598 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007599#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007600#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007601 {
7602 ssl->out_hdr = ssl->out_buf + 8;
7603 ssl->in_hdr = ssl->in_buf + 8;
7604 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007605#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007606
7607 /* Derive other internal pointers. */
7608 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007609 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007610}
7611
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007612int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007613 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007614{
Paul Bakker48916f92012-09-16 19:57:18 +00007615 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007616
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007617 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007618
7619 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007620 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007621 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007622
7623 /* Set to NULL in case of an error condition */
7624 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007625
Angus Grattond8213d02016-05-25 20:56:48 +10007626 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7627 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007628 {
Angus Grattond8213d02016-05-25 20:56:48 +10007629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007630 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007631 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007632 }
7633
7634 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7635 if( ssl->out_buf == NULL )
7636 {
7637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007638 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007639 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007640 }
7641
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007642 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007643
Paul Bakker48916f92012-09-16 19:57:18 +00007644 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007645 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007646
7647 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007648
7649error:
7650 mbedtls_free( ssl->in_buf );
7651 mbedtls_free( ssl->out_buf );
7652
7653 ssl->conf = NULL;
7654
7655 ssl->in_buf = NULL;
7656 ssl->out_buf = NULL;
7657
7658 ssl->in_hdr = NULL;
7659 ssl->in_ctr = NULL;
7660 ssl->in_len = NULL;
7661 ssl->in_iv = NULL;
7662 ssl->in_msg = NULL;
7663
7664 ssl->out_hdr = NULL;
7665 ssl->out_ctr = NULL;
7666 ssl->out_len = NULL;
7667 ssl->out_iv = NULL;
7668 ssl->out_msg = NULL;
7669
k-stachowiak9f7798e2018-07-31 16:52:32 +02007670 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007671}
7672
7673/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007674 * Reset an initialized and used SSL context for re-use while retaining
7675 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007676 *
7677 * If partial is non-zero, keep data in the input buffer and client ID.
7678 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007679 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007680static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007681{
Paul Bakker48916f92012-09-16 19:57:18 +00007682 int ret;
7683
Hanno Becker7e772132018-08-10 12:38:21 +01007684#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7685 !defined(MBEDTLS_SSL_SRV_C)
7686 ((void) partial);
7687#endif
7688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007690
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007691 /* Cancel any possibly running timer */
7692 ssl_set_timer( ssl, 0 );
7693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694#if defined(MBEDTLS_SSL_RENEGOTIATION)
7695 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007696 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007697
7698 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7700 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007703
Paul Bakker7eb013f2011-10-06 12:37:39 +00007704 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007705 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007706
7707 ssl->in_msgtype = 0;
7708 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007710 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007711 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007714 ssl_dtls_replay_reset( ssl );
7715#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007716
7717 ssl->in_hslen = 0;
7718 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007719
7720 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007721
7722 ssl->out_msgtype = 0;
7723 ssl->out_msglen = 0;
7724 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7726 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007727 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007728#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007729
Hanno Becker19859472018-08-06 09:40:20 +01007730 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7731
Paul Bakker48916f92012-09-16 19:57:18 +00007732 ssl->transform_in = NULL;
7733 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007734
Hanno Becker78640902018-08-13 16:35:15 +01007735 ssl->session_in = NULL;
7736 ssl->session_out = NULL;
7737
Angus Grattond8213d02016-05-25 20:56:48 +10007738 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007739
7740#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007741 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007742#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7743 {
7744 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007745 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007746 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7749 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7752 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7755 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007756 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007757 }
7758#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007759
Paul Bakker48916f92012-09-16 19:57:18 +00007760 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 mbedtls_ssl_transform_free( ssl->transform );
7763 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007764 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007765 }
Paul Bakker48916f92012-09-16 19:57:18 +00007766
Paul Bakkerc0463502013-02-14 11:19:38 +01007767 if( ssl->session )
7768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 mbedtls_ssl_session_free( ssl->session );
7770 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007771 ssl->session = NULL;
7772 }
7773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007775 ssl->alpn_chosen = NULL;
7776#endif
7777
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007778#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007779#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007780 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007781#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007782 {
7783 mbedtls_free( ssl->cli_id );
7784 ssl->cli_id = NULL;
7785 ssl->cli_id_len = 0;
7786 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007787#endif
7788
Paul Bakker48916f92012-09-16 19:57:18 +00007789 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7790 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007791
7792 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007793}
7794
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007795/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007796 * Reset an initialized and used SSL context for re-use while retaining
7797 * all application-set variables, function pointers and data.
7798 */
7799int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7800{
7801 return( ssl_session_reset_int( ssl, 0 ) );
7802}
7803
7804/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007805 * SSL set accessors
7806 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007807void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007808{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007809 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007810}
7811
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007812void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007813{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007814 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007815}
7816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007818void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007819{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007820 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007821}
7822#endif
7823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007825void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007826{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007827 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007828}
7829#endif
7830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007832
Hanno Becker1841b0a2018-08-24 11:13:57 +01007833void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7834 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007835{
7836 ssl->disable_datagram_packing = !allow_packing;
7837}
7838
7839void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7840 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007841{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007842 conf->hs_timeout_min = min;
7843 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007844}
7845#endif
7846
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007847void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007848{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007849 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007850}
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007853void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007854 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007855 void *p_vrfy )
7856{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007857 conf->f_vrfy = f_vrfy;
7858 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007859}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007861
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007862void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007863 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007864 void *p_rng )
7865{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007866 conf->f_rng = f_rng;
7867 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007868}
7869
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007870void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007871 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007872 void *p_dbg )
7873{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007874 conf->f_dbg = f_dbg;
7875 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007876}
7877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007879 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007880 mbedtls_ssl_send_t *f_send,
7881 mbedtls_ssl_recv_t *f_recv,
7882 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007883{
7884 ssl->p_bio = p_bio;
7885 ssl->f_send = f_send;
7886 ssl->f_recv = f_recv;
7887 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007888}
7889
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007890#if defined(MBEDTLS_SSL_PROTO_DTLS)
7891void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7892{
7893 ssl->mtu = mtu;
7894}
7895#endif
7896
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007897void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007898{
7899 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007900}
7901
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007902void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7903 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007904 mbedtls_ssl_set_timer_t *f_set_timer,
7905 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007906{
7907 ssl->p_timer = p_timer;
7908 ssl->f_set_timer = f_set_timer;
7909 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007910
7911 /* Make sure we start with no timer running */
7912 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007913}
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007916void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007917 void *p_cache,
7918 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7919 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007920{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007921 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007923 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007924}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927#if defined(MBEDTLS_SSL_CLI_C)
7928int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007929{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007930 int ret;
7931
7932 if( ssl == NULL ||
7933 session == NULL ||
7934 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007935 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007938 }
7939
7940 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7941 return( ret );
7942
Paul Bakker0a597072012-09-25 21:55:46 +00007943 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007944
7945 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007946}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007948
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007949void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007950 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007951{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007952 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7953 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7954 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7955 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007956}
7957
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007958void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007959 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007960 int major, int minor )
7961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007963 return;
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007966 return;
7967
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007968 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007969}
7970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007972void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007973 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007974{
7975 conf->cert_profile = profile;
7976}
7977
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007978/* Append a new keycert entry to a (possibly empty) list */
7979static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7980 mbedtls_x509_crt *cert,
7981 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007982{
niisato8ee24222018-06-25 19:05:48 +09007983 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007984
niisato8ee24222018-06-25 19:05:48 +09007985 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7986 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007987 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007988
niisato8ee24222018-06-25 19:05:48 +09007989 new_cert->cert = cert;
7990 new_cert->key = key;
7991 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007992
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007993 /* Update head is the list was null, else add to the end */
7994 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007995 {
niisato8ee24222018-06-25 19:05:48 +09007996 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007997 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007998 else
7999 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008000 mbedtls_ssl_key_cert *cur = *head;
8001 while( cur->next != NULL )
8002 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008003 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008004 }
8005
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008006 return( 0 );
8007}
8008
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008009int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008010 mbedtls_x509_crt *own_cert,
8011 mbedtls_pk_context *pk_key )
8012{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008013 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008014}
8015
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008016void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008017 mbedtls_x509_crt *ca_chain,
8018 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008019{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008020 conf->ca_chain = ca_chain;
8021 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008024
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008025#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8026int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8027 mbedtls_x509_crt *own_cert,
8028 mbedtls_pk_context *pk_key )
8029{
8030 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8031 own_cert, pk_key ) );
8032}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008033
8034void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8035 mbedtls_x509_crt *ca_chain,
8036 mbedtls_x509_crl *ca_crl )
8037{
8038 ssl->handshake->sni_ca_chain = ca_chain;
8039 ssl->handshake->sni_ca_crl = ca_crl;
8040}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008041
8042void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8043 int authmode )
8044{
8045 ssl->handshake->sni_authmode = authmode;
8046}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008047#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8048
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008049#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008050/*
8051 * Set EC J-PAKE password for current handshake
8052 */
8053int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8054 const unsigned char *pw,
8055 size_t pw_len )
8056{
8057 mbedtls_ecjpake_role role;
8058
Janos Follath8eb64132016-06-03 15:40:57 +01008059 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8061
8062 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8063 role = MBEDTLS_ECJPAKE_SERVER;
8064 else
8065 role = MBEDTLS_ECJPAKE_CLIENT;
8066
8067 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8068 role,
8069 MBEDTLS_MD_SHA256,
8070 MBEDTLS_ECP_DP_SECP256R1,
8071 pw, pw_len ) );
8072}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008073#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008076int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008077 const unsigned char *psk, size_t psk_len,
8078 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008079{
Paul Bakker6db455e2013-09-18 17:29:31 +02008080 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008085
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008086 /* Identity len will be encoded on two bytes */
8087 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008088 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008089 {
8090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8091 }
8092
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008093 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008094 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008095 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008096
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008097 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008098 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008099 conf->psk_len = 0;
8100 }
8101 if( conf->psk_identity != NULL )
8102 {
8103 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008104 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008105 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008106 }
8107
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008108 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8109 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008110 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008111 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008112 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008113 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008114 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008115 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008116 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008117
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008118 conf->psk_len = psk_len;
8119 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008120
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008121 memcpy( conf->psk, psk, conf->psk_len );
8122 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008123
8124 return( 0 );
8125}
8126
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008127int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8128 const unsigned char *psk, size_t psk_len )
8129{
8130 if( psk == NULL || ssl->handshake == NULL )
8131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8132
8133 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8135
8136 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008137 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008138 mbedtls_platform_zeroize( ssl->handshake->psk,
8139 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008140 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008141 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008142 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008143
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008144 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008145 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008146
8147 ssl->handshake->psk_len = psk_len;
8148 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8149
8150 return( 0 );
8151}
8152
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008153void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008155 size_t),
8156 void *p_psk )
8157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008158 conf->f_psk = f_psk;
8159 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008162
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008163#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008164
8165#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008166int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008167{
8168 int ret;
8169
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008170 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8171 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8172 {
8173 mbedtls_mpi_free( &conf->dhm_P );
8174 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008175 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008176 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008177
8178 return( 0 );
8179}
Hanno Becker470a8c42017-10-04 15:28:46 +01008180#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008181
Hanno Beckera90658f2017-10-04 15:29:08 +01008182int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8183 const unsigned char *dhm_P, size_t P_len,
8184 const unsigned char *dhm_G, size_t G_len )
8185{
8186 int ret;
8187
8188 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8189 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8190 {
8191 mbedtls_mpi_free( &conf->dhm_P );
8192 mbedtls_mpi_free( &conf->dhm_G );
8193 return( ret );
8194 }
8195
8196 return( 0 );
8197}
Paul Bakker5121ce52009-01-03 21:22:43 +00008198
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008199int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008200{
8201 int ret;
8202
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008203 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8204 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8205 {
8206 mbedtls_mpi_free( &conf->dhm_P );
8207 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008208 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008209 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008210
8211 return( 0 );
8212}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008213#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008214
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008215#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8216/*
8217 * Set the minimum length for Diffie-Hellman parameters
8218 */
8219void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8220 unsigned int bitlen )
8221{
8222 conf->dhm_min_bitlen = bitlen;
8223}
8224#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8225
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008226#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008227/*
8228 * Set allowed/preferred hashes for handshake signatures
8229 */
8230void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8231 const int *hashes )
8232{
8233 conf->sig_hashes = hashes;
8234}
Hanno Becker947194e2017-04-07 13:25:49 +01008235#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008236
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008237#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008238/*
8239 * Set the allowed elliptic curves
8240 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008241void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008242 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008243{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008244 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008245}
Hanno Becker947194e2017-04-07 13:25:49 +01008246#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008247
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008248#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008250{
Hanno Becker947194e2017-04-07 13:25:49 +01008251 /* Initialize to suppress unnecessary compiler warning */
8252 size_t hostname_len = 0;
8253
8254 /* Check if new hostname is valid before
8255 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008256 if( hostname != NULL )
8257 {
8258 hostname_len = strlen( hostname );
8259
8260 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8262 }
8263
8264 /* Now it's clear that we will overwrite the old hostname,
8265 * so we can free it safely */
8266
8267 if( ssl->hostname != NULL )
8268 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008269 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008270 mbedtls_free( ssl->hostname );
8271 }
8272
8273 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008274
Paul Bakker5121ce52009-01-03 21:22:43 +00008275 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008276 {
8277 ssl->hostname = NULL;
8278 }
8279 else
8280 {
8281 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008282 if( ssl->hostname == NULL )
8283 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008284
Hanno Becker947194e2017-04-07 13:25:49 +01008285 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008286
Hanno Becker947194e2017-04-07 13:25:49 +01008287 ssl->hostname[hostname_len] = '\0';
8288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008289
8290 return( 0 );
8291}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008292#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008293
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008294#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008295void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008297 const unsigned char *, size_t),
8298 void *p_sni )
8299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008300 conf->f_sni = f_sni;
8301 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008306int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008307{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008308 size_t cur_len, tot_len;
8309 const char **p;
8310
8311 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008312 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8313 * MUST NOT be truncated."
8314 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008315 */
8316 tot_len = 0;
8317 for( p = protos; *p != NULL; p++ )
8318 {
8319 cur_len = strlen( *p );
8320 tot_len += cur_len;
8321
8322 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008324 }
8325
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008326 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008327
8328 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008329}
8330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008332{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008333 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008336
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008337void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008338{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008339 conf->max_major_ver = major;
8340 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008341}
8342
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008343void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008344{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008345 conf->min_major_ver = major;
8346 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008347}
8348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008350void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008351{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008352 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008353}
8354#endif
8355
Janos Follath088ce432017-04-10 12:42:31 +01008356#if defined(MBEDTLS_SSL_SRV_C)
8357void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8358 char cert_req_ca_list )
8359{
8360 conf->cert_req_ca_list = cert_req_ca_list;
8361}
8362#endif
8363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008365void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008366{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008367 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008368}
8369#endif
8370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008372void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008373{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008374 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008375}
8376#endif
8377
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008378#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008379void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008380{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008381 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008382}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008383#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008386int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008389 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008392 }
8393
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008394 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008395
8396 return( 0 );
8397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008401void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008402{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008403 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008408void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008409{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008410 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008411}
8412#endif
8413
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008414void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008415{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008416 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008417}
8418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008420void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008421{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008422 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008423}
8424
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008425void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008426{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008427 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008428}
8429
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008430void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008431 const unsigned char period[8] )
8432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008433 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008438#if defined(MBEDTLS_SSL_CLI_C)
8439void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008440{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008441 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008442}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008443#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008444
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008446void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8447 mbedtls_ssl_ticket_write_t *f_ticket_write,
8448 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8449 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008450{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008451 conf->f_ticket_write = f_ticket_write;
8452 conf->f_ticket_parse = f_ticket_parse;
8453 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008454}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008455#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008457
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008458#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8459void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8460 mbedtls_ssl_export_keys_t *f_export_keys,
8461 void *p_export_keys )
8462{
8463 conf->f_export_keys = f_export_keys;
8464 conf->p_export_keys = p_export_keys;
8465}
8466#endif
8467
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008468#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008469void mbedtls_ssl_conf_async_private_cb(
8470 mbedtls_ssl_config *conf,
8471 mbedtls_ssl_async_sign_t *f_async_sign,
8472 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8473 mbedtls_ssl_async_resume_t *f_async_resume,
8474 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008475 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008476{
8477 conf->f_async_sign_start = f_async_sign;
8478 conf->f_async_decrypt_start = f_async_decrypt;
8479 conf->f_async_resume = f_async_resume;
8480 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008481 conf->p_async_config_data = async_config_data;
8482}
8483
Gilles Peskine8f97af72018-04-26 11:46:10 +02008484void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8485{
8486 return( conf->p_async_config_data );
8487}
8488
Gilles Peskine1febfef2018-04-30 11:54:39 +02008489void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008490{
8491 if( ssl->handshake == NULL )
8492 return( NULL );
8493 else
8494 return( ssl->handshake->user_async_ctx );
8495}
8496
Gilles Peskine1febfef2018-04-30 11:54:39 +02008497void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008498 void *ctx )
8499{
8500 if( ssl->handshake != NULL )
8501 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008502}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008503#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008504
Paul Bakker5121ce52009-01-03 21:22:43 +00008505/*
8506 * SSL get accessors
8507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008509{
8510 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8511}
8512
Hanno Becker8b170a02017-10-10 11:51:19 +01008513int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8514{
8515 /*
8516 * Case A: We're currently holding back
8517 * a message for further processing.
8518 */
8519
8520 if( ssl->keep_current_message == 1 )
8521 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008523 return( 1 );
8524 }
8525
8526 /*
8527 * Case B: Further records are pending in the current datagram.
8528 */
8529
8530#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008531 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008532 ssl->in_left > ssl->next_record_offset )
8533 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008535 return( 1 );
8536 }
8537#endif /* MBEDTLS_SSL_PROTO_DTLS */
8538
8539 /*
8540 * Case C: A handshake message is being processed.
8541 */
8542
Hanno Becker8b170a02017-10-10 11:51:19 +01008543 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8544 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008546 return( 1 );
8547 }
8548
8549 /*
8550 * Case D: An application data message is being processed
8551 */
8552 if( ssl->in_offt != NULL )
8553 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008555 return( 1 );
8556 }
8557
8558 /*
8559 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008560 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008561 * we implement support for multiple alerts in single records.
8562 */
8563
8564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8565 return( 0 );
8566}
8567
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008568uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008569{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008570 if( ssl->session != NULL )
8571 return( ssl->session->verify_result );
8572
8573 if( ssl->session_negotiate != NULL )
8574 return( ssl->session_negotiate->verify_result );
8575
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008576 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008577}
8578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008580{
Paul Bakker926c8e42013-03-06 10:23:34 +01008581 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008582 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008585}
8586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008590 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008591 {
8592 switch( ssl->minor_ver )
8593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008595 return( "DTLSv1.0" );
8596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008598 return( "DTLSv1.2" );
8599
8600 default:
8601 return( "unknown (DTLS)" );
8602 }
8603 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008604 MBEDTLS_SSL_TRANSPORT_ELSE
8605#endif /* MBEDTLS_SSL_PROTO_DTLS */
8606#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008607 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008608 switch( ssl->minor_ver )
8609 {
8610 case MBEDTLS_SSL_MINOR_VERSION_0:
8611 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008612
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008613 case MBEDTLS_SSL_MINOR_VERSION_1:
8614 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008615
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008616 case MBEDTLS_SSL_MINOR_VERSION_2:
8617 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008618
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008619 case MBEDTLS_SSL_MINOR_VERSION_3:
8620 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008621
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008622 default:
8623 return( "unknown" );
8624 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008625 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008626#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008627}
8628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008630{
Hanno Becker3136ede2018-08-17 15:28:19 +01008631 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008633 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008634
Hanno Becker43395762019-05-03 14:46:38 +01008635 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8636
Hanno Becker78640902018-08-13 16:35:15 +01008637 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008638 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640#if defined(MBEDTLS_ZLIB_SUPPORT)
8641 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8642 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008643#endif
8644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 case MBEDTLS_MODE_GCM:
8648 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008649 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008651 transform_expansion = transform->minlen;
8652 break;
8653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008655
8656 block_size = mbedtls_cipher_get_block_size(
8657 &transform->cipher_ctx_enc );
8658
Hanno Becker3136ede2018-08-17 15:28:19 +01008659 /* Expansion due to the addition of the MAC. */
8660 transform_expansion += transform->maclen;
8661
8662 /* Expansion due to the addition of CBC padding;
8663 * Theoretically up to 256 bytes, but we never use
8664 * more than the block size of the underlying cipher. */
8665 transform_expansion += block_size;
8666
8667 /* For TLS 1.1 or higher, an explicit IV is added
8668 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008669#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8670 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008671 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008672#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008673
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008674 break;
8675
8676 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008679 }
8680
Hanno Beckera5a2b082019-05-15 14:03:01 +01008681#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008682 if( transform->out_cid_len != 0 )
8683 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008684#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008685
Hanno Becker43395762019-05-03 14:46:38 +01008686 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008687}
8688
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008689#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8690size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8691{
8692 size_t max_len;
8693
8694 /*
8695 * Assume mfl_code is correct since it was checked when set
8696 */
Angus Grattond8213d02016-05-25 20:56:48 +10008697 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008698
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008699 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008700 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008701 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008702 {
Angus Grattond8213d02016-05-25 20:56:48 +10008703 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008704 }
8705
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008706 /* During a handshake, use the value being negotiated */
8707 if( ssl->session_negotiate != NULL &&
8708 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8709 {
8710 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8711 }
8712
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008713 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008714}
8715#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8716
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008717#if defined(MBEDTLS_SSL_PROTO_DTLS)
8718static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8719{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008720 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8721 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8722 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8723 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8724 return ( 0 );
8725
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008726 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8727 return( ssl->mtu );
8728
8729 if( ssl->mtu == 0 )
8730 return( ssl->handshake->mtu );
8731
8732 return( ssl->mtu < ssl->handshake->mtu ?
8733 ssl->mtu : ssl->handshake->mtu );
8734}
8735#endif /* MBEDTLS_SSL_PROTO_DTLS */
8736
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008737int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8738{
8739 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8740
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008741#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8742 !defined(MBEDTLS_SSL_PROTO_DTLS)
8743 (void) ssl;
8744#endif
8745
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008746#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8747 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8748
8749 if( max_len > mfl )
8750 max_len = mfl;
8751#endif
8752
8753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008754 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008755 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008756 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008757 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8758 const size_t overhead = (size_t) ret;
8759
8760 if( ret < 0 )
8761 return( ret );
8762
8763 if( mtu <= overhead )
8764 {
8765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8766 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8767 }
8768
8769 if( max_len > mtu - overhead )
8770 max_len = mtu - overhead;
8771 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008772#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008773
Hanno Becker0defedb2018-08-10 12:35:02 +01008774#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8775 !defined(MBEDTLS_SSL_PROTO_DTLS)
8776 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008777#endif
8778
8779 return( (int) max_len );
8780}
8781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#if defined(MBEDTLS_X509_CRT_PARSE_C)
8783const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008784{
8785 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008786 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008787
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008788 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792#if defined(MBEDTLS_SSL_CLI_C)
8793int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008794{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008795 if( ssl == NULL ||
8796 dst == NULL ||
8797 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008798 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008801 }
8802
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008803 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008806
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008807const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8808{
8809 if( ssl == NULL )
8810 return( NULL );
8811
8812 return( ssl->session );
8813}
8814
Paul Bakker5121ce52009-01-03 21:22:43 +00008815/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008816 * Define ticket header determining Mbed TLS version
8817 * and structure of the ticket.
8818 */
8819
Hanno Becker41527622019-05-16 12:50:45 +01008820/*
Hanno Becker26829e92019-05-28 14:30:45 +01008821 * Define bitflag determining compile-time settings influencing
8822 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008823 */
8824
Hanno Becker26829e92019-05-28 14:30:45 +01008825#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008826#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008827#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008828#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008829#endif /* MBEDTLS_HAVE_TIME */
8830
8831#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008832#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008833#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008834#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008835#endif /* MBEDTLS_X509_CRT_PARSE_C */
8836
8837#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008838#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008839#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008840#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008841#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8842
8843#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008844#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008845#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008846#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008847#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8848
8849#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008850#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008851#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008852#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008853#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8854
8855#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008856#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008857#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008858#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008859#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8860
Hanno Becker41527622019-05-16 12:50:45 +01008861#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8862#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8863#else
8864#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8865#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8866
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008867#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8868#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8869#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8870#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8871#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8872#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8873#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008874
Hanno Becker26829e92019-05-28 14:30:45 +01008875#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008876 ( (uint16_t) ( \
8877 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8878 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8879 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8880 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8881 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8882 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008883 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008884
Hanno Becker557fe9f2019-05-16 12:41:07 +01008885static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008886 MBEDTLS_VERSION_MAJOR,
8887 MBEDTLS_VERSION_MINOR,
8888 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008889 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8890 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008891};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008892
8893/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008894 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008895 * (in the presentation language of TLS, RFC 8446 section 3)
8896 *
Hanno Becker26829e92019-05-28 14:30:45 +01008897 * opaque mbedtls_version[3]; // major, minor, patch
8898 * opaque session_format[2]; // version-specific 16-bit field determining
8899 * // the format of the remaining
8900 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008901 *
8902 * Note: When updating the format, remember to keep
8903 * these version+format bytes.
8904 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008905 * // In this version, `session_format` determines
8906 * // the setting of those compile-time
8907 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008908 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008909 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008910 * uint8 ciphersuite[2]; // defined by the standard
8911 * uint8 compression; // 0 or 1
8912 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008913 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008914 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008915 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008916 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8917 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008918 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008919 * uint8 mfl_code; // up to 255 according to standard
8920 * uint8 trunc_hmac; // 0 or 1
8921 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008922 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008923 * The order is the same as in the definition of the structure, except
8924 * verify_result is put before peer_cert so that all mandatory fields come
8925 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008926 */
8927int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8928 unsigned char *buf,
8929 size_t buf_len,
8930 size_t *olen )
8931{
8932 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008933 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008934#if defined(MBEDTLS_HAVE_TIME)
8935 uint64_t start;
8936#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008937#if defined(MBEDTLS_X509_CRT_PARSE_C)
8938 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008939#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008940
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008941 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008942 * Add version identifier
8943 */
8944
8945 used += sizeof( ssl_serialized_session_header );
8946
8947 if( used <= buf_len )
8948 {
8949 memcpy( p, ssl_serialized_session_header,
8950 sizeof( ssl_serialized_session_header ) );
8951 p += sizeof( ssl_serialized_session_header );
8952 }
8953
8954 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008955 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008956 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008957#if defined(MBEDTLS_HAVE_TIME)
8958 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008959
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008960 if( used <= buf_len )
8961 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008962 start = (uint64_t) session->start;
8963
8964 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
8965 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
8966 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
8967 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
8968 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
8969 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
8970 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
8971 *p++ = (unsigned char)( ( start ) & 0xFF );
8972 }
8973#endif /* MBEDTLS_HAVE_TIME */
8974
8975 /*
8976 * Basic mandatory fields
8977 */
8978 used += 2 /* ciphersuite */
8979 + 1 /* compression */
8980 + 1 /* id_len */
8981 + sizeof( session->id )
8982 + sizeof( session->master )
8983 + 4; /* verify_result */
8984
8985 if( used <= buf_len )
8986 {
8987 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
8988 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
8989
8990 *p++ = (unsigned char)( session->compression & 0xFF );
8991
8992 *p++ = (unsigned char)( session->id_len & 0xFF );
8993 memcpy( p, session->id, 32 );
8994 p += 32;
8995
8996 memcpy( p, session->master, 48 );
8997 p += 48;
8998
8999 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9000 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9001 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9002 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009003 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009004
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009005 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009006 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009007 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009008#if defined(MBEDTLS_X509_CRT_PARSE_C)
9009 if( session->peer_cert == NULL )
9010 cert_len = 0;
9011 else
9012 cert_len = session->peer_cert->raw.len;
9013
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009014 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009015
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009016 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009017 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009018 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9019 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9020 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9021
9022 if( session->peer_cert != NULL )
9023 {
9024 memcpy( p, session->peer_cert->raw.p, cert_len );
9025 p += cert_len;
9026 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009027 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009028#endif /* MBEDTLS_X509_CRT_PARSE_C */
9029
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009030 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009031 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009032 */
9033#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009034 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009035
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009036 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009037 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009038 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9039 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9040 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9041
9042 if( session->ticket != NULL )
9043 {
9044 memcpy( p, session->ticket, session->ticket_len );
9045 p += session->ticket_len;
9046 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009047
9048 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9049 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9050 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9051 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009052 }
9053#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9054
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009055 /*
9056 * Misc extension-related info
9057 */
9058#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9059 used += 1;
9060
9061 if( used <= buf_len )
9062 *p++ = session->mfl_code;
9063#endif
9064
9065#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9066 used += 1;
9067
9068 if( used <= buf_len )
9069 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9070#endif
9071
9072#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9073 used += 1;
9074
9075 if( used <= buf_len )
9076 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9077#endif
9078
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009079 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009080 *olen = used;
9081
9082 if( used > buf_len )
9083 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009084
9085 return( 0 );
9086}
9087
9088/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009089 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009090 *
9091 * This internal version is wrapped by a public function that cleans up in
9092 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009093 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009094static int ssl_session_load( mbedtls_ssl_session *session,
9095 const unsigned char *buf,
9096 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009097{
9098 const unsigned char *p = buf;
9099 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009100#if defined(MBEDTLS_HAVE_TIME)
9101 uint64_t start;
9102#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009103#if defined(MBEDTLS_X509_CRT_PARSE_C)
9104 size_t cert_len;
9105#endif /* MBEDTLS_X509_CRT_PARSE_C */
9106
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009107 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009108 * Check version identifier
9109 */
9110
9111 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009113
9114 if( memcmp( p, ssl_serialized_session_header,
9115 sizeof( ssl_serialized_session_header ) ) != 0 )
9116 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009117 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009118 }
9119 p += sizeof( ssl_serialized_session_header );
9120
9121 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009122 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009123 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009124#if defined(MBEDTLS_HAVE_TIME)
9125 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9127
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009128 start = ( (uint64_t) p[0] << 56 ) |
9129 ( (uint64_t) p[1] << 48 ) |
9130 ( (uint64_t) p[2] << 40 ) |
9131 ( (uint64_t) p[3] << 32 ) |
9132 ( (uint64_t) p[4] << 24 ) |
9133 ( (uint64_t) p[5] << 16 ) |
9134 ( (uint64_t) p[6] << 8 ) |
9135 ( (uint64_t) p[7] );
9136 p += 8;
9137
9138 session->start = (time_t) start;
9139#endif /* MBEDTLS_HAVE_TIME */
9140
9141 /*
9142 * Basic mandatory fields
9143 */
9144 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9146
9147 session->ciphersuite = ( p[0] << 8 ) | p[1];
9148 p += 2;
9149
9150 session->compression = *p++;
9151
9152 session->id_len = *p++;
9153 memcpy( session->id, p, 32 );
9154 p += 32;
9155
9156 memcpy( session->master, p, 48 );
9157 p += 48;
9158
9159 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9160 ( (uint32_t) p[1] << 16 ) |
9161 ( (uint32_t) p[2] << 8 ) |
9162 ( (uint32_t) p[3] );
9163 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009164
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009165 /* Immediately clear invalid pointer values that have been read, in case
9166 * we exit early before we replaced them with valid ones. */
9167#if defined(MBEDTLS_X509_CRT_PARSE_C)
9168 session->peer_cert = NULL;
9169#endif
9170#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9171 session->ticket = NULL;
9172#endif
9173
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009174 /*
9175 * Peer certificate
9176 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009177#if defined(MBEDTLS_X509_CRT_PARSE_C)
9178 if( 3 > (size_t)( end - p ) )
9179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9180
9181 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9182 p += 3;
9183
9184 if( cert_len == 0 )
9185 {
9186 session->peer_cert = NULL;
9187 }
9188 else
9189 {
9190 int ret;
9191
9192 if( cert_len > (size_t)( end - p ) )
9193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9194
9195 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9196
9197 if( session->peer_cert == NULL )
9198 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9199
9200 mbedtls_x509_crt_init( session->peer_cert );
9201
9202 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9203 p, cert_len ) ) != 0 )
9204 {
9205 mbedtls_x509_crt_free( session->peer_cert );
9206 mbedtls_free( session->peer_cert );
9207 session->peer_cert = NULL;
9208 return( ret );
9209 }
9210
9211 p += cert_len;
9212 }
9213#endif /* MBEDTLS_X509_CRT_PARSE_C */
9214
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009215 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009216 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009217 */
9218#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9219 if( 3 > (size_t)( end - p ) )
9220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9221
9222 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9223 p += 3;
9224
9225 if( session->ticket_len != 0 )
9226 {
9227 if( session->ticket_len > (size_t)( end - p ) )
9228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9229
9230 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9231 if( session->ticket == NULL )
9232 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9233
9234 memcpy( session->ticket, p, session->ticket_len );
9235 p += session->ticket_len;
9236 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009237
9238 if( 4 > (size_t)( end - p ) )
9239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9240
9241 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9242 ( (uint32_t) p[1] << 16 ) |
9243 ( (uint32_t) p[2] << 8 ) |
9244 ( (uint32_t) p[3] );
9245 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009246#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9247
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009248 /*
9249 * Misc extension-related info
9250 */
9251#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9252 if( 1 > (size_t)( end - p ) )
9253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9254
9255 session->mfl_code = *p++;
9256#endif
9257
9258#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9259 if( 1 > (size_t)( end - p ) )
9260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9261
9262 session->trunc_hmac = *p++;
9263#endif
9264
9265#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9266 if( 1 > (size_t)( end - p ) )
9267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9268
9269 session->encrypt_then_mac = *p++;
9270#endif
9271
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009272 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009273 if( p != end )
9274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9275
9276 return( 0 );
9277}
9278
9279/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009280 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009281 */
9282int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9283 const unsigned char *buf,
9284 size_t len )
9285{
9286 int ret = ssl_session_load( session, buf, len );
9287
9288 if( ret != 0 )
9289 mbedtls_ssl_session_free( session );
9290
9291 return( ret );
9292}
9293
9294/*
Paul Bakker1961b702013-01-25 14:49:24 +01009295 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009300
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009301 if( ssl == NULL || ssl->conf == NULL )
9302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009305 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009309 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009311#endif
9312
Paul Bakker1961b702013-01-25 14:49:24 +01009313 return( ret );
9314}
9315
9316/*
9317 * Perform the SSL handshake
9318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009320{
9321 int ret = 0;
9322
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009323 if( ssl == NULL || ssl->conf == NULL )
9324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009331
9332 if( ret != 0 )
9333 break;
9334 }
9335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009337
9338 return( ret );
9339}
9340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341#if defined(MBEDTLS_SSL_RENEGOTIATION)
9342#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009343/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009344 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009347{
9348 int ret;
9349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009351
9352 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9354 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009355
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009356 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009357 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009359 return( ret );
9360 }
9361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009363
9364 return( 0 );
9365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009367
9368/*
9369 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370 * - any side: calling mbedtls_ssl_renegotiate(),
9371 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9372 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009373 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009374 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009378{
9379 int ret;
9380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009382
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009383 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9384 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009385
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009386 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9387 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009389 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009391 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009392 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009393 ssl->handshake->out_msg_seq = 1;
9394 else
9395 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009396 }
9397#endif
9398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9400 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009405 return( ret );
9406 }
9407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009409
9410 return( 0 );
9411}
9412
9413/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009414 * Renegotiate current connection on client,
9415 * or request renegotiation on server
9416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009420
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009421 if( ssl == NULL || ssl->conf == NULL )
9422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009425 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009426 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009432
9433 /* Did we already try/start sending HelloRequest? */
9434 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009436
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009437 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009442 /*
9443 * On client, either start the renegotiation process or,
9444 * if already in progress, continue the handshake
9445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009450
9451 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009454 return( ret );
9455 }
9456 }
9457 else
9458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009462 return( ret );
9463 }
9464 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009466
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009467 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009468}
9469
9470/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009471 * Check record counters and renegotiate if they're above the limit.
9472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009474{
Andres AG2196c7f2016-12-15 17:01:16 +00009475 size_t ep_len = ssl_ep_len( ssl );
9476 int in_ctr_cmp;
9477 int out_ctr_cmp;
9478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9480 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009481 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009482 {
9483 return( 0 );
9484 }
9485
Andres AG2196c7f2016-12-15 17:01:16 +00009486 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9487 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009488 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009489 ssl->conf->renego_period + ep_len, 8 - ep_len );
9490
9491 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009492 {
9493 return( 0 );
9494 }
9495
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009500
9501/*
9502 * Receive application data decrypted from the SSL layer
9503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009505{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009506 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009507 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009508
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009509 if( ssl == NULL || ssl->conf == NULL )
9510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009515 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009518 return( ret );
9519
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009520 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009522 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009523 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009524 return( ret );
9525 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009526 }
9527#endif
9528
Hanno Becker4a810fb2017-05-24 16:27:30 +01009529 /*
9530 * Check if renegotiation is necessary and/or handshake is
9531 * in process. If yes, perform/continue, and fall through
9532 * if an unexpected packet is received while the client
9533 * is waiting for the ServerHello.
9534 *
9535 * (There is no equivalent to the last condition on
9536 * the server-side as it is not treated as within
9537 * a handshake while waiting for the ClientHello
9538 * after a renegotiation request.)
9539 */
9540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009542 ret = ssl_check_ctr_renegotiate( ssl );
9543 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9544 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009547 return( ret );
9548 }
9549#endif
9550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009554 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9555 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009558 return( ret );
9559 }
9560 }
9561
Hanno Beckere41158b2017-10-23 13:30:32 +01009562 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009563 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009564 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009565 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009566 if( ssl->f_get_timer != NULL &&
9567 ssl->f_get_timer( ssl->p_timer ) == -1 )
9568 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009569 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009570 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009571
Hanno Becker327c93b2018-08-15 13:56:18 +01009572 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009573 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009574 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9575 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009576
Hanno Becker4a810fb2017-05-24 16:27:30 +01009577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9578 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009579 }
9580
9581 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009583 {
9584 /*
9585 * OpenSSL sends empty messages to randomize the IV
9586 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009587 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009590 return( 0 );
9591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009593 return( ret );
9594 }
9595 }
9596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009600
Hanno Becker4a810fb2017-05-24 16:27:30 +01009601 /*
9602 * - For client-side, expect SERVER_HELLO_REQUEST.
9603 * - For server-side, expect CLIENT_HELLO.
9604 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9605 */
9606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009608 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009610 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009613
9614 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009616 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009617 {
9618 continue;
9619 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009620 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009621#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009622#if defined(MBEDTLS_SSL_PROTO_TLS)
9623 {
9624 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9625 }
9626#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009627 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009628#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009629
Hanno Becker4a810fb2017-05-24 16:27:30 +01009630#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009635
9636 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009638 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009639 {
9640 continue;
9641 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009642 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009643#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009644#if defined(MBEDTLS_SSL_PROTO_TLS)
9645 {
9646 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9647 }
9648#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009649 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009650#endif /* MBEDTLS_SSL_SRV_C */
9651
Hanno Becker21df7f92017-10-17 11:03:26 +01009652#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009653 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009654 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9655 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9656 ssl->conf->allow_legacy_renegotiation ==
9657 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9658 {
9659 /*
9660 * Accept renegotiation request
9661 */
Paul Bakker48916f92012-09-16 19:57:18 +00009662
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009663 /* DTLS clients need to know renego is server-initiated */
9664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009665 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009666 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9667 {
9668 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9669 }
9670#endif
9671 ret = ssl_start_renegotiation( ssl );
9672 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9673 ret != 0 )
9674 {
9675 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9676 return( ret );
9677 }
9678 }
9679 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009680#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009681 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009682 /*
9683 * Refuse renegotiation
9684 */
9685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688#if defined(MBEDTLS_SSL_PROTO_SSL3)
9689 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009690 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009691 /* SSLv3 does not have a "no_renegotiation" warning, so
9692 we send a fatal alert and abort the connection. */
9693 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9694 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9695 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009696 }
9697 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9699#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9700 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9701 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9704 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9705 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009706 {
9707 return( ret );
9708 }
Paul Bakker48916f92012-09-16 19:57:18 +00009709 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009710 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9712 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009716 }
Paul Bakker48916f92012-09-16 19:57:18 +00009717 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009718
Hanno Becker90333da2017-10-10 11:27:13 +01009719 /* At this point, we don't know whether the renegotiation has been
9720 * completed or not. The cases to consider are the following:
9721 * 1) The renegotiation is complete. In this case, no new record
9722 * has been read yet.
9723 * 2) The renegotiation is incomplete because the client received
9724 * an application data record while awaiting the ServerHello.
9725 * 3) The renegotiation is incomplete because the client received
9726 * a non-handshake, non-application data message while awaiting
9727 * the ServerHello.
9728 * In each of these case, looping will be the proper action:
9729 * - For 1), the next iteration will read a new record and check
9730 * if it's application data.
9731 * - For 2), the loop condition isn't satisfied as application data
9732 * is present, hence continue is the same as break
9733 * - For 3), the loop condition is satisfied and read_record
9734 * will re-deliver the message that was held back by the client
9735 * when expecting the ServerHello.
9736 */
9737 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009738 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009739#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009741 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009742 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009743 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009744 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009747 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009749 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009750 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009751 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9755 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009758 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009759 }
9760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9764 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009765 }
9766
9767 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009768
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009769 /* We're going to return something now, cancel timer,
9770 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009772 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009773
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009774#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009775 /* If we requested renego but received AppData, resend HelloRequest.
9776 * Do it now, after setting in_offt, to avoid taking this branch
9777 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009779 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009781 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009782 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009785 return( ret );
9786 }
9787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009789#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009790 }
9791
9792 n = ( len < ssl->in_msglen )
9793 ? len : ssl->in_msglen;
9794
9795 memcpy( buf, ssl->in_offt, n );
9796 ssl->in_msglen -= n;
9797
9798 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009799 {
9800 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009801 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009802 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009803 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009804 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009805 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009806 /* more data available */
9807 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009811
Paul Bakker23986e52011-04-24 08:57:21 +00009812 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009813}
9814
9815/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009816 * Send application data to be encrypted by the SSL layer, taking care of max
9817 * fragment length and buffer size.
9818 *
9819 * According to RFC 5246 Section 6.2.1:
9820 *
9821 * Zero-length fragments of Application data MAY be sent as they are
9822 * potentially useful as a traffic analysis countermeasure.
9823 *
9824 * Therefore, it is possible that the input message length is 0 and the
9825 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009826 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009827static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009828 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009829{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009830 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9831 const size_t max_len = (size_t) ret;
9832
9833 if( ret < 0 )
9834 {
9835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9836 return( ret );
9837 }
9838
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009839 if( len > max_len )
9840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009842 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009845 "maximum fragment length: %d > %d",
9846 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009848 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009849 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009850#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009851#if defined(MBEDTLS_SSL_PROTO_TLS)
9852 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009853 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009854 }
9855#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009856 }
Paul Bakker887bd502011-06-08 13:10:54 +00009857
Paul Bakker5121ce52009-01-03 21:22:43 +00009858 if( ssl->out_left != 0 )
9859 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009860 /*
9861 * The user has previously tried to send the data and
9862 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9863 * written. In this case, we expect the high-level write function
9864 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009869 return( ret );
9870 }
9871 }
Paul Bakker887bd502011-06-08 13:10:54 +00009872 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009873 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009874 /*
9875 * The user is trying to send a message the first time, so we need to
9876 * copy the data into the internal buffers and setup the data structure
9877 * to keep track of partial writes
9878 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009879 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009881 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009882
Hanno Becker67bc7c32018-08-06 11:33:50 +01009883 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009886 return( ret );
9887 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009888 }
9889
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009890 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009891}
9892
9893/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009894 * Write application data, doing 1/n-1 splitting if necessary.
9895 *
9896 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009897 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009898 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009901static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009902 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009903{
9904 int ret;
9905
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009906 if( ssl->conf->cbc_record_splitting ==
9907 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009908 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9910 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9911 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009912 {
9913 return( ssl_write_real( ssl, buf, len ) );
9914 }
9915
9916 if( ssl->split_done == 0 )
9917 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009918 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009919 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009920 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009921 }
9922
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009923 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9924 return( ret );
9925 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009926
9927 return( ret + 1 );
9928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009930
9931/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009932 * Write application data (public-facing wrapper)
9933 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009934int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009935{
9936 int ret;
9937
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009939
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009940 if( ssl == NULL || ssl->conf == NULL )
9941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9942
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009943#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009944 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9945 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009946 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009947 return( ret );
9948 }
9949#endif
9950
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009951 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009952 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009953 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009954 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009956 return( ret );
9957 }
9958 }
9959
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009960#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009961 ret = ssl_write_split( ssl, buf, len );
9962#else
9963 ret = ssl_write_real( ssl, buf, len );
9964#endif
9965
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009967
9968 return( ret );
9969}
9970
9971/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009972 * Notify the peer that the connection is being closed
9973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009975{
9976 int ret;
9977
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009978 if( ssl == NULL || ssl->conf == NULL )
9979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009982
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009983 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9989 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9990 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009993 return( ret );
9994 }
9995 }
9996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009998
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009999 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010000}
10001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010003{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010004 if( transform == NULL )
10005 return;
10006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010008 deflateEnd( &transform->ctx_deflate );
10009 inflateEnd( &transform->ctx_inflate );
10010#endif
10011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10013 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010014
Hanno Becker92231322018-01-03 15:32:51 +000010015#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016 mbedtls_md_free( &transform->md_ctx_enc );
10017 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010018#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010019
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010020 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010021}
10022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023#if defined(MBEDTLS_X509_CRT_PARSE_C)
10024static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010027
10028 while( cur != NULL )
10029 {
10030 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010031 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010032 cur = next;
10033 }
10034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010036
Hanno Becker0271f962018-08-16 13:23:47 +010010037#if defined(MBEDTLS_SSL_PROTO_DTLS)
10038
10039static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10040{
10041 unsigned offset;
10042 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10043
10044 if( hs == NULL )
10045 return;
10046
Hanno Becker283f5ef2018-08-24 09:34:47 +010010047 ssl_free_buffered_record( ssl );
10048
Hanno Becker0271f962018-08-16 13:23:47 +010010049 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010050 ssl_buffering_free_slot( ssl, offset );
10051}
10052
10053static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10054 uint8_t slot )
10055{
10056 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10057 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010058
10059 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10060 return;
10061
Hanno Beckere605b192018-08-21 15:59:07 +010010062 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010063 {
Hanno Beckere605b192018-08-21 15:59:07 +010010064 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010065 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010066 mbedtls_free( hs_buf->data );
10067 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010068 }
10069}
10070
10071#endif /* MBEDTLS_SSL_PROTO_DTLS */
10072
Gilles Peskine9b562d52018-04-25 20:32:43 +020010073void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010074{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010075 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10076
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010077 if( handshake == NULL )
10078 return;
10079
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010080#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10081 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10082 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010083 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010084 handshake->async_in_progress = 0;
10085 }
10086#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10087
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010088#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10089 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10090 mbedtls_md5_free( &handshake->fin_md5 );
10091 mbedtls_sha1_free( &handshake->fin_sha1 );
10092#endif
10093#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10094#if defined(MBEDTLS_SHA256_C)
10095 mbedtls_sha256_free( &handshake->fin_sha256 );
10096#endif
10097#if defined(MBEDTLS_SHA512_C)
10098 mbedtls_sha512_free( &handshake->fin_sha512 );
10099#endif
10100#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102#if defined(MBEDTLS_DHM_C)
10103 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105#if defined(MBEDTLS_ECDH_C)
10106 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010107#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010108#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010109 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010110#if defined(MBEDTLS_SSL_CLI_C)
10111 mbedtls_free( handshake->ecjpake_cache );
10112 handshake->ecjpake_cache = NULL;
10113 handshake->ecjpake_cache_len = 0;
10114#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010115#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010116
Janos Follath4ae5c292016-02-10 11:27:43 +000010117#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10118 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010119 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010121#endif
10122
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010123#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10124 if( handshake->psk != NULL )
10125 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010126 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010127 mbedtls_free( handshake->psk );
10128 }
10129#endif
10130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010131#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10132 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010133 /*
10134 * Free only the linked list wrapper, not the keys themselves
10135 * since the belong to the SNI callback
10136 */
10137 if( handshake->sni_key_cert != NULL )
10138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010140
10141 while( cur != NULL )
10142 {
10143 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010145 cur = next;
10146 }
10147 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010149
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010150#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010151 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010152#endif
10153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154#if defined(MBEDTLS_SSL_PROTO_DTLS)
10155 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010156 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010157 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010158#endif
10159
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010160 mbedtls_platform_zeroize( handshake,
10161 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010162}
10163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010164void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010165{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010166 if( session == NULL )
10167 return;
10168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000010170 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +000010171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010172 mbedtls_x509_crt_free( session->peer_cert );
10173 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +000010174 }
Paul Bakkered27a042013-04-18 22:46:23 +020010175#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010176
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010177#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010179#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010180
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010181 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010182}
10183
Paul Bakker5121ce52009-01-03 21:22:43 +000010184/*
10185 * Free an SSL context
10186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010188{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010189 if( ssl == NULL )
10190 return;
10191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010193
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010194 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010195 {
Angus Grattond8213d02016-05-25 20:56:48 +100010196 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010198 }
10199
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010200 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010201 {
Angus Grattond8213d02016-05-25 20:56:48 +100010202 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010204 }
10205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010207 if( ssl->compress_buf != NULL )
10208 {
Angus Grattond8213d02016-05-25 20:56:48 +100010209 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010211 }
10212#endif
10213
Paul Bakker48916f92012-09-16 19:57:18 +000010214 if( ssl->transform )
10215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 mbedtls_ssl_transform_free( ssl->transform );
10217 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010218 }
10219
10220 if( ssl->handshake )
10221 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010222 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10224 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 mbedtls_free( ssl->handshake );
10227 mbedtls_free( ssl->transform_negotiate );
10228 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010229 }
10230
Paul Bakkerc0463502013-02-14 11:19:38 +010010231 if( ssl->session )
10232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 mbedtls_ssl_session_free( ssl->session );
10234 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010235 }
10236
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010237#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010238 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010239 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010240 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010242 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010243#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10246 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10249 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010250 }
10251#endif
10252
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010253#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010254 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010255#endif
10256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010258
Paul Bakker86f04f42013-02-14 11:20:09 +010010259 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010260 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010261}
10262
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010263/*
10264 * Initialze mbedtls_ssl_config
10265 */
10266void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10267{
10268 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010269
10270#if !defined(MBEDTLS_SSL_PROTO_TLS)
10271 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10272#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010273}
10274
Simon Butcherc97b6972015-12-27 23:48:17 +000010275#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010276static int ssl_preset_default_hashes[] = {
10277#if defined(MBEDTLS_SHA512_C)
10278 MBEDTLS_MD_SHA512,
10279 MBEDTLS_MD_SHA384,
10280#endif
10281#if defined(MBEDTLS_SHA256_C)
10282 MBEDTLS_MD_SHA256,
10283 MBEDTLS_MD_SHA224,
10284#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010285#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010286 MBEDTLS_MD_SHA1,
10287#endif
10288 MBEDTLS_MD_NONE
10289};
Simon Butcherc97b6972015-12-27 23:48:17 +000010290#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010291
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010292static int ssl_preset_suiteb_ciphersuites[] = {
10293 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10294 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10295 0
10296};
10297
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010298#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010299static int ssl_preset_suiteb_hashes[] = {
10300 MBEDTLS_MD_SHA256,
10301 MBEDTLS_MD_SHA384,
10302 MBEDTLS_MD_NONE
10303};
10304#endif
10305
10306#if defined(MBEDTLS_ECP_C)
10307static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10308 MBEDTLS_ECP_DP_SECP256R1,
10309 MBEDTLS_ECP_DP_SECP384R1,
10310 MBEDTLS_ECP_DP_NONE
10311};
10312#endif
10313
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010314/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010315 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010316 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010317int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010318 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010319{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010320#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010321 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010322#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010323
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010324 /* Use the functions here so that they are covered in tests,
10325 * but otherwise access member directly for efficiency */
10326 mbedtls_ssl_conf_endpoint( conf, endpoint );
10327 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010328
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010329 /*
10330 * Things that are common to all presets
10331 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010332#if defined(MBEDTLS_SSL_CLI_C)
10333 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10334 {
10335 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10336#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10337 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10338#endif
10339 }
10340#endif
10341
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010342#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010343 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010344#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010345
10346#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10347 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10348#endif
10349
10350#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10351 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10352#endif
10353
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010354#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10355 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10356#endif
10357
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010358#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010359 conf->f_cookie_write = ssl_cookie_write_dummy;
10360 conf->f_cookie_check = ssl_cookie_check_dummy;
10361#endif
10362
10363#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10364 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10365#endif
10366
Janos Follath088ce432017-04-10 12:42:31 +010010367#if defined(MBEDTLS_SSL_SRV_C)
10368 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10369#endif
10370
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010371#if defined(MBEDTLS_SSL_PROTO_DTLS)
10372 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10373 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10374#endif
10375
10376#if defined(MBEDTLS_SSL_RENEGOTIATION)
10377 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010378 memset( conf->renego_period, 0x00, 2 );
10379 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010380#endif
10381
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010382#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10383 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10384 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010385 const unsigned char dhm_p[] =
10386 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10387 const unsigned char dhm_g[] =
10388 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10389
Hanno Beckera90658f2017-10-04 15:29:08 +010010390 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10391 dhm_p, sizeof( dhm_p ),
10392 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010393 {
10394 return( ret );
10395 }
10396 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010397#endif
10398
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010399 /*
10400 * Preset-specific defaults
10401 */
10402 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010403 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010404 /*
10405 * NSA Suite B
10406 */
10407 case MBEDTLS_SSL_PRESET_SUITEB:
10408 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10409 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10410 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10411 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10412
10413 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10414 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10415 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10416 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10417 ssl_preset_suiteb_ciphersuites;
10418
10419#if defined(MBEDTLS_X509_CRT_PARSE_C)
10420 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010421#endif
10422
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010423#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010424 conf->sig_hashes = ssl_preset_suiteb_hashes;
10425#endif
10426
10427#if defined(MBEDTLS_ECP_C)
10428 conf->curve_list = ssl_preset_suiteb_curves;
10429#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010430 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010431
10432 /*
10433 * Default
10434 */
10435 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010436 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10437 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10438 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10439 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10440 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10441 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10442 MBEDTLS_SSL_MIN_MINOR_VERSION :
10443 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010444 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10445 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10446
10447#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010448 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010449 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10450#endif
10451
10452 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10453 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10454 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10455 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10456 mbedtls_ssl_list_ciphersuites();
10457
10458#if defined(MBEDTLS_X509_CRT_PARSE_C)
10459 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10460#endif
10461
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010462#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010463 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010464#endif
10465
10466#if defined(MBEDTLS_ECP_C)
10467 conf->curve_list = mbedtls_ecp_grp_id_list();
10468#endif
10469
10470#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10471 conf->dhm_min_bitlen = 1024;
10472#endif
10473 }
10474
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010475 return( 0 );
10476}
10477
10478/*
10479 * Free mbedtls_ssl_config
10480 */
10481void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10482{
10483#if defined(MBEDTLS_DHM_C)
10484 mbedtls_mpi_free( &conf->dhm_P );
10485 mbedtls_mpi_free( &conf->dhm_G );
10486#endif
10487
10488#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10489 if( conf->psk != NULL )
10490 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010491 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010492 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010493 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010494 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010495 }
10496
10497 if( conf->psk_identity != NULL )
10498 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010499 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010500 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010501 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010502 conf->psk_identity_len = 0;
10503 }
10504#endif
10505
10506#if defined(MBEDTLS_X509_CRT_PARSE_C)
10507 ssl_key_cert_free( conf->key_cert );
10508#endif
10509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010510 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010511}
10512
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010513#if defined(MBEDTLS_PK_C) && \
10514 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010515/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010518unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520#if defined(MBEDTLS_RSA_C)
10521 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10522 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010524#if defined(MBEDTLS_ECDSA_C)
10525 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10526 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010528 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010529}
10530
Hanno Becker7e5437a2017-04-28 17:15:26 +010010531unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10532{
10533 switch( type ) {
10534 case MBEDTLS_PK_RSA:
10535 return( MBEDTLS_SSL_SIG_RSA );
10536 case MBEDTLS_PK_ECDSA:
10537 case MBEDTLS_PK_ECKEY:
10538 return( MBEDTLS_SSL_SIG_ECDSA );
10539 default:
10540 return( MBEDTLS_SSL_SIG_ANON );
10541 }
10542}
10543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010544mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010545{
10546 switch( sig )
10547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010548#if defined(MBEDTLS_RSA_C)
10549 case MBEDTLS_SSL_SIG_RSA:
10550 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010551#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010552#if defined(MBEDTLS_ECDSA_C)
10553 case MBEDTLS_SSL_SIG_ECDSA:
10554 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010555#endif
10556 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010557 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010558 }
10559}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010560#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010561
Hanno Becker7e5437a2017-04-28 17:15:26 +010010562#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10563 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10564
10565/* Find an entry in a signature-hash set matching a given hash algorithm. */
10566mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10567 mbedtls_pk_type_t sig_alg )
10568{
10569 switch( sig_alg )
10570 {
10571 case MBEDTLS_PK_RSA:
10572 return( set->rsa );
10573 case MBEDTLS_PK_ECDSA:
10574 return( set->ecdsa );
10575 default:
10576 return( MBEDTLS_MD_NONE );
10577 }
10578}
10579
10580/* Add a signature-hash-pair to a signature-hash set */
10581void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10582 mbedtls_pk_type_t sig_alg,
10583 mbedtls_md_type_t md_alg )
10584{
10585 switch( sig_alg )
10586 {
10587 case MBEDTLS_PK_RSA:
10588 if( set->rsa == MBEDTLS_MD_NONE )
10589 set->rsa = md_alg;
10590 break;
10591
10592 case MBEDTLS_PK_ECDSA:
10593 if( set->ecdsa == MBEDTLS_MD_NONE )
10594 set->ecdsa = md_alg;
10595 break;
10596
10597 default:
10598 break;
10599 }
10600}
10601
10602/* Allow exactly one hash algorithm for each signature. */
10603void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10604 mbedtls_md_type_t md_alg )
10605{
10606 set->rsa = md_alg;
10607 set->ecdsa = md_alg;
10608}
10609
10610#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10611 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10612
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010613/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010614 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010617{
10618 switch( hash )
10619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620#if defined(MBEDTLS_MD5_C)
10621 case MBEDTLS_SSL_HASH_MD5:
10622 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010623#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624#if defined(MBEDTLS_SHA1_C)
10625 case MBEDTLS_SSL_HASH_SHA1:
10626 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010628#if defined(MBEDTLS_SHA256_C)
10629 case MBEDTLS_SSL_HASH_SHA224:
10630 return( MBEDTLS_MD_SHA224 );
10631 case MBEDTLS_SSL_HASH_SHA256:
10632 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010633#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010634#if defined(MBEDTLS_SHA512_C)
10635 case MBEDTLS_SSL_HASH_SHA384:
10636 return( MBEDTLS_MD_SHA384 );
10637 case MBEDTLS_SSL_HASH_SHA512:
10638 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010639#endif
10640 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010642 }
10643}
10644
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010645/*
10646 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10647 */
10648unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10649{
10650 switch( md )
10651 {
10652#if defined(MBEDTLS_MD5_C)
10653 case MBEDTLS_MD_MD5:
10654 return( MBEDTLS_SSL_HASH_MD5 );
10655#endif
10656#if defined(MBEDTLS_SHA1_C)
10657 case MBEDTLS_MD_SHA1:
10658 return( MBEDTLS_SSL_HASH_SHA1 );
10659#endif
10660#if defined(MBEDTLS_SHA256_C)
10661 case MBEDTLS_MD_SHA224:
10662 return( MBEDTLS_SSL_HASH_SHA224 );
10663 case MBEDTLS_MD_SHA256:
10664 return( MBEDTLS_SSL_HASH_SHA256 );
10665#endif
10666#if defined(MBEDTLS_SHA512_C)
10667 case MBEDTLS_MD_SHA384:
10668 return( MBEDTLS_SSL_HASH_SHA384 );
10669 case MBEDTLS_MD_SHA512:
10670 return( MBEDTLS_SSL_HASH_SHA512 );
10671#endif
10672 default:
10673 return( MBEDTLS_SSL_HASH_NONE );
10674 }
10675}
10676
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010677#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010678/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010679 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010680 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010681 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010682int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010685
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010686 if( ssl->conf->curve_list == NULL )
10687 return( -1 );
10688
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010689 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010690 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010691 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010692
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010693 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010694}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010695#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010696
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010697#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010698/*
10699 * Check if a hash proposed by the peer is in our list.
10700 * Return 0 if we're willing to use it, -1 otherwise.
10701 */
10702int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10703 mbedtls_md_type_t md )
10704{
10705 const int *cur;
10706
10707 if( ssl->conf->sig_hashes == NULL )
10708 return( -1 );
10709
10710 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10711 if( *cur == (int) md )
10712 return( 0 );
10713
10714 return( -1 );
10715}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010716#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718#if defined(MBEDTLS_X509_CRT_PARSE_C)
10719int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10720 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010721 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010722 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010723{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010724 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010726 int usage = 0;
10727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010729 const char *ext_oid;
10730 size_t ext_len;
10731#endif
10732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10734 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010735 ((void) cert);
10736 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010737 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010738#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10741 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010742 {
10743 /* Server part of the key exchange */
10744 switch( ciphersuite->key_exchange )
10745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746 case MBEDTLS_KEY_EXCHANGE_RSA:
10747 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010748 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010749 break;
10750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10752 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10753 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10754 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010755 break;
10756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10758 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010759 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010760 break;
10761
10762 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763 case MBEDTLS_KEY_EXCHANGE_NONE:
10764 case MBEDTLS_KEY_EXCHANGE_PSK:
10765 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10766 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010767 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010768 usage = 0;
10769 }
10770 }
10771 else
10772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10774 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010775 }
10776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010777 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010778 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010779 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010780 ret = -1;
10781 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010782#else
10783 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010784#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010786#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10787 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010789 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10790 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010791 }
10792 else
10793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10795 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010796 }
10797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010799 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010800 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010801 ret = -1;
10802 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010803#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010804
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010805 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010807#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010808
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010809/*
10810 * Convert version numbers to/from wire format
10811 * and, for DTLS, to/from TLS equivalent.
10812 *
10813 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010814 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010815 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10816 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010818void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010819 unsigned char ver[2] )
10820{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010821#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10822 ((void) transport);
10823#endif
10824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010826 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010828 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010829 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10830
10831 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10832 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10833 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010834 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010835#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010836#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010837 {
10838 ver[0] = (unsigned char) major;
10839 ver[1] = (unsigned char) minor;
10840 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010841#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010842}
10843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010844void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010845 const unsigned char ver[2] )
10846{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010847#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10848 ((void) transport);
10849#endif
10850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010852 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010853 {
10854 *major = 255 - ver[0] + 2;
10855 *minor = 255 - ver[1] + 1;
10856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010858 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10859 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010860 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020010861#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010862#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010863 {
10864 *major = ver[0];
10865 *minor = ver[1];
10866 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020010867#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010868}
10869
Simon Butcher99000142016-10-13 17:21:01 +010010870int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10871{
10872#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10873 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10874 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10875
10876 switch( md )
10877 {
10878#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10879#if defined(MBEDTLS_MD5_C)
10880 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010881 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010882#endif
10883#if defined(MBEDTLS_SHA1_C)
10884 case MBEDTLS_SSL_HASH_SHA1:
10885 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10886 break;
10887#endif
10888#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10889#if defined(MBEDTLS_SHA512_C)
10890 case MBEDTLS_SSL_HASH_SHA384:
10891 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10892 break;
10893#endif
10894#if defined(MBEDTLS_SHA256_C)
10895 case MBEDTLS_SSL_HASH_SHA256:
10896 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10897 break;
10898#endif
10899 default:
10900 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10901 }
10902
10903 return 0;
10904#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10905 (void) ssl;
10906 (void) md;
10907
10908 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10909#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10910}
10911
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010912#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10913 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10914int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10915 unsigned char *output,
10916 unsigned char *data, size_t data_len )
10917{
10918 int ret = 0;
10919 mbedtls_md5_context mbedtls_md5;
10920 mbedtls_sha1_context mbedtls_sha1;
10921
10922 mbedtls_md5_init( &mbedtls_md5 );
10923 mbedtls_sha1_init( &mbedtls_sha1 );
10924
10925 /*
10926 * digitally-signed struct {
10927 * opaque md5_hash[16];
10928 * opaque sha_hash[20];
10929 * };
10930 *
10931 * md5_hash
10932 * MD5(ClientHello.random + ServerHello.random
10933 * + ServerParams);
10934 * sha_hash
10935 * SHA(ClientHello.random + ServerHello.random
10936 * + ServerParams);
10937 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010938 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010939 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010941 goto exit;
10942 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010943 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010944 ssl->handshake->randbytes, 64 ) ) != 0 )
10945 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010947 goto exit;
10948 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010949 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010950 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010952 goto exit;
10953 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010954 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010955 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010957 goto exit;
10958 }
10959
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010960 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010961 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010963 goto exit;
10964 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010965 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010966 ssl->handshake->randbytes, 64 ) ) != 0 )
10967 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010969 goto exit;
10970 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010971 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010972 data_len ) ) != 0 )
10973 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010975 goto exit;
10976 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010977 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010978 output + 16 ) ) != 0 )
10979 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010981 goto exit;
10982 }
10983
10984exit:
10985 mbedtls_md5_free( &mbedtls_md5 );
10986 mbedtls_sha1_free( &mbedtls_sha1 );
10987
10988 if( ret != 0 )
10989 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10990 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10991
10992 return( ret );
10993
10994}
10995#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10996 MBEDTLS_SSL_PROTO_TLS1_1 */
10997
10998#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10999 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11000int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011001 unsigned char *hash, size_t *hashlen,
11002 unsigned char *data, size_t data_len,
11003 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011004{
11005 int ret = 0;
11006 mbedtls_md_context_t ctx;
11007 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011008 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011009
11010 mbedtls_md_init( &ctx );
11011
11012 /*
11013 * digitally-signed struct {
11014 * opaque client_random[32];
11015 * opaque server_random[32];
11016 * ServerDHParams params;
11017 * };
11018 */
11019 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11020 {
11021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11022 goto exit;
11023 }
11024 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11025 {
11026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11027 goto exit;
11028 }
11029 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11030 {
11031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11032 goto exit;
11033 }
11034 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11035 {
11036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11037 goto exit;
11038 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011039 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011040 {
11041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11042 goto exit;
11043 }
11044
11045exit:
11046 mbedtls_md_free( &ctx );
11047
11048 if( ret != 0 )
11049 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11050 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11051
11052 return( ret );
11053}
11054#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11055 MBEDTLS_SSL_PROTO_TLS1_2 */
11056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011057#endif /* MBEDTLS_SSL_TLS_C */