blob: 7d56f667619c90ffca35f61b6df5f1037e3acb8a [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 );
71#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010072 return( 0 );
73}
74
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020075/*
76 * Start a timer.
77 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020081 if( ssl->f_set_timer == NULL )
82 return;
83
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
85 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086}
87
88/*
89 * Return -1 is timer is expired, 0 if it isn't.
90 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020093 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020094 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095
96 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020097 {
98 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020099 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101
102 return( 0 );
103}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100105static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
106 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100107static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100108
109#define SSL_DONT_FORCE_FLUSH 0
110#define SSL_FORCE_FLUSH 1
111
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100113
Hanno Beckera5a2b082019-05-15 14:03:01 +0100114#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100115/* Top-level Connection ID API */
116
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100117int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
118 size_t len,
119 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100120{
121 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
123
Hanno Becker791ec6b2019-05-14 11:45:26 +0100124 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
125 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
126 {
127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
128 }
129
130 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100131 conf->cid_len = len;
132 return( 0 );
133}
134
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100135int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
136 int enable,
137 unsigned char const *own_cid,
138 size_t own_cid_len )
139{
Hanno Becker78c43022019-05-03 14:38:32 +0100140 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
142
Hanno Becker07489862019-04-25 16:01:49 +0100143 ssl->negotiate_cid = enable;
144 if( enable == MBEDTLS_SSL_CID_DISABLED )
145 {
146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
147 return( 0 );
148 }
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100150 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100151
Hanno Beckereec2be92019-05-03 13:06:44 +0100152 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100153 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
155 (unsigned) own_cid_len,
156 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
158 }
159
160 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100161 /* Truncation is not an issue here because
162 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
163 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100164
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100165 return( 0 );
166}
167
168int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
169 int *enabled,
170 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
171 size_t *peer_cid_len )
172{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100173 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100174
Hanno Becker78c43022019-05-03 14:38:32 +0100175 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
176 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
177 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100179 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100180
Hanno Beckercb063f52019-05-03 12:54:52 +0100181 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
182 * were used, but client and server requested the empty CID.
183 * This is indistinguishable from not using the CID extension
184 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100185 if( ssl->transform_in->in_cid_len == 0 &&
186 ssl->transform_in->out_cid_len == 0 )
187 {
188 return( 0 );
189 }
190
Hanno Becker633d6042019-05-22 16:50:35 +0100191 if( peer_cid_len != NULL )
192 {
193 *peer_cid_len = ssl->transform_in->out_cid_len;
194 if( peer_cid != NULL )
195 {
196 memcpy( peer_cid, ssl->transform_in->out_cid,
197 ssl->transform_in->out_cid_len );
198 }
199 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100200
201 *enabled = MBEDTLS_SSL_CID_ENABLED;
202
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100203 return( 0 );
204}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100205#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206
Hanno Beckerd5847772018-08-28 10:09:23 +0100207/* Forward declarations for functions related to message buffering. */
208static void ssl_buffering_free( mbedtls_ssl_context *ssl );
209static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
210 uint8_t slot );
211static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
212static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
213static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
214static int ssl_buffer_message( mbedtls_ssl_context *ssl );
215static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100216static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100217
Hanno Beckera67dee22018-08-22 10:05:20 +0100218static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100219static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100220{
Hanno Becker11682cc2018-08-22 14:41:02 +0100221 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100222
223 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
227}
228
Hanno Becker67bc7c32018-08-06 11:33:50 +0100229static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
230{
Hanno Becker11682cc2018-08-22 14:41:02 +0100231 size_t const bytes_written = ssl->out_left;
232 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233
234 /* Double-check that the write-index hasn't gone
235 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100236 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100237 {
238 /* Should never happen... */
239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
240 }
241
242 return( (int) ( mtu - bytes_written ) );
243}
244
245static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
246{
247 int ret;
248 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400249 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100250
251#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
252 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
253
254 if( max_len > mfl )
255 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100256
257 /* By the standard (RFC 6066 Sect. 4), the MFL extension
258 * only limits the maximum record payload size, so in theory
259 * we would be allowed to pack multiple records of payload size
260 * MFL into a single datagram. However, this would mean that there's
261 * no way to explicitly communicate MTU restrictions to the peer.
262 *
263 * The following reduction of max_len makes sure that we never
264 * write datagrams larger than MFL + Record Expansion Overhead.
265 */
266 if( max_len <= ssl->out_left )
267 return( 0 );
268
269 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100270#endif
271
272 ret = ssl_get_remaining_space_in_datagram( ssl );
273 if( ret < 0 )
274 return( ret );
275 remaining = (size_t) ret;
276
277 ret = mbedtls_ssl_get_record_expansion( ssl );
278 if( ret < 0 )
279 return( ret );
280 expansion = (size_t) ret;
281
282 if( remaining <= expansion )
283 return( 0 );
284
285 remaining -= expansion;
286 if( remaining >= max_len )
287 remaining = max_len;
288
289 return( (int) remaining );
290}
291
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200292/*
293 * Double the retransmit timeout value, within the allowed range,
294 * returning -1 if the maximum value has already been reached.
295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200297{
298 uint32_t new_timeout;
299
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200300 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200301 return( -1 );
302
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200303 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
304 * in the following way: after the initial transmission and a first
305 * retransmission, back off to a temporary estimated MTU of 508 bytes.
306 * This value is guaranteed to be deliverable (if not guaranteed to be
307 * delivered) of any compliant IPv4 (and IPv6) network, and should work
308 * on most non-IP stacks too. */
309 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400310 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200311 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
313 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200315 new_timeout = 2 * ssl->handshake->retransmit_timeout;
316
317 /* Avoid arithmetic overflow and range overflow */
318 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200319 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200320 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200321 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200322 }
323
324 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200326 ssl->handshake->retransmit_timeout ) );
327
328 return( 0 );
329}
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200332{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200333 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335 ssl->handshake->retransmit_timeout ) );
336}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200340/*
341 * Convert max_fragment_length codes to length.
342 * RFC 6066 says:
343 * enum{
344 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
345 * } MaxFragmentLength;
346 * and we add 0 -> extension unused
347 */
Angus Grattond8213d02016-05-25 20:56:48 +1000348static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200349{
Angus Grattond8213d02016-05-25 20:56:48 +1000350 switch( mfl )
351 {
352 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
353 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
354 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
355 return 512;
356 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
357 return 1024;
358 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
359 return 2048;
360 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
361 return 4096;
362 default:
363 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
364 }
365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200367
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200368#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_ssl_session_free( dst );
372 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200375 if( src->peer_cert != NULL )
376 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200377 int ret;
378
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200379 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200380 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200381 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200386 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200389 dst->peer_cert = NULL;
390 return( ret );
391 }
392 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200395#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200396 if( src->ticket != NULL )
397 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200398 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200399 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200400 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200401
402 memcpy( dst->ticket, src->ticket, src->ticket_len );
403 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200404#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200405
406 return( 0 );
407}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200408#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
411int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200412 const unsigned char *key_enc, const unsigned char *key_dec,
413 size_t keylen,
414 const unsigned char *iv_enc, const unsigned char *iv_dec,
415 size_t ivlen,
416 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200417 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
419int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
420int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
421int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
422int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
423#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000424
Paul Bakker5121ce52009-01-03 21:22:43 +0000425/*
426 * Key material generation
427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200429static int ssl3_prf( const unsigned char *secret, size_t slen,
430 const char *label,
431 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000432 unsigned char *dstbuf, size_t dlen )
433{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100434 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000435 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200436 mbedtls_md5_context md5;
437 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000438 unsigned char padding[16];
439 unsigned char sha1sum[20];
440 ((void)label);
441
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200442 mbedtls_md5_init( &md5 );
443 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200444
Paul Bakker5f70b252012-09-13 14:23:06 +0000445 /*
446 * SSLv3:
447 * block =
448 * MD5( secret + SHA1( 'A' + secret + random ) ) +
449 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
450 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
451 * ...
452 */
453 for( i = 0; i < dlen / 16; i++ )
454 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200455 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000456
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100458 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100464 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100465 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100466 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000467
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100470 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100471 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100473 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100474 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100475 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000476 }
477
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200479 mbedtls_md5_free( &md5 );
480 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000481
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500482 mbedtls_platform_zeroize( padding, sizeof( padding ) );
483 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000484
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100485 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200490static int tls1_prf( const unsigned char *secret, size_t slen,
491 const char *label,
492 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000493 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000494{
Paul Bakker23986e52011-04-24 08:57:21 +0000495 size_t nb, hs;
496 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200497 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 unsigned char tmp[128];
499 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 const mbedtls_md_info_t *md_info;
501 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100502 int ret;
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
506 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 hs = ( slen + 1 ) / 2;
510 S1 = secret;
511 S2 = secret + slen - hs;
512
513 nb = strlen( label );
514 memcpy( tmp + 20, label, nb );
515 memcpy( tmp + 20 + nb, random, rlen );
516 nb += rlen;
517
518 /*
519 * First compute P_md5(secret,label+random)[0..dlen]
520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
522 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100525 return( ret );
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
528 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
529 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531 for( i = 0; i < dlen; i += 16 )
532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_md_hmac_reset ( &md_ctx );
534 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
535 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_md_hmac_reset ( &md_ctx );
538 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
539 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
541 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
542
543 for( j = 0; j < k; j++ )
544 dstbuf[i + j] = h_i[j];
545 }
546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100548
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 /*
550 * XOR out with P_sha1(secret,label+random)[0..dlen]
551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100556 return( ret );
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
559 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
560 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
562 for( i = 0; i < dlen; i += 20 )
563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_md_hmac_reset ( &md_ctx );
565 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
566 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_md_hmac_reset ( &md_ctx );
569 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
570 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
573
574 for( j = 0; j < k; j++ )
575 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
576 }
577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100579
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500580 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
581 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583 return( 0 );
584}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
588static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100589 const unsigned char *secret, size_t slen,
590 const char *label,
591 const unsigned char *random, size_t rlen,
592 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000593{
594 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100595 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000596 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
598 const mbedtls_md_info_t *md_info;
599 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100600 int ret;
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608
609 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611
612 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 memcpy( tmp + md_len, label, nb );
614 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 nb += rlen;
616
617 /*
618 * Compute P_<hash>(secret, label + random)[0..dlen]
619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621 return( ret );
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
624 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
625 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100626
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_hmac_reset ( &md_ctx );
630 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
631 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_md_hmac_reset ( &md_ctx );
634 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
635 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000638
639 for( j = 0; j < k; j++ )
640 dstbuf[i + j] = h_i[j];
641 }
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
646 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647
648 return( 0 );
649}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100652static int tls_prf_sha256( const unsigned char *secret, size_t slen,
653 const char *label,
654 const unsigned char *random, size_t rlen,
655 unsigned char *dstbuf, size_t dlen )
656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100658 label, random, rlen, dstbuf, dlen ) );
659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200663static int tls_prf_sha384( const unsigned char *secret, size_t slen,
664 const char *label,
665 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666 unsigned char *dstbuf, size_t dlen )
667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_SHA512_C */
672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
677 defined(MBEDTLS_SSL_PROTO_TLS1_1)
678static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200679#endif
Paul Bakker380da532012-04-18 16:10:25 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200682static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200684#endif
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200687static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200689#endif
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
692#if defined(MBEDTLS_SHA256_C)
693static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200694static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200696#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_SHA512_C)
699static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200700static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000704
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200705/* Type for the TLS PRF */
706typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
707 const unsigned char *, size_t,
708 unsigned char *, size_t);
709
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200710/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200711 * Populate a transform structure with session keys and all the other
712 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200713 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200714 * Parameters:
715 * - [in/out]: transform: structure to populate
716 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200717 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200718 * - [in] ciphersuite
719 * - [in] master
720 * - [in] encrypt_then_mac
721 * - [in] trunc_hmac
722 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200723 * - [in] tls_prf: pointer to PRF to use for key derivation
724 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200725 * - [in] minor_ver: SSL/TLS minor version
726 * - [in] endpoint: client or server
727 * - [in] ssl: optionally used for:
728 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
729 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
730 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200731 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200732static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200733 int ciphersuite,
734 const unsigned char master[48],
735#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
736 int encrypt_then_mac,
737#endif
738#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
739 int trunc_hmac,
740#endif
741#if defined(MBEDTLS_ZLIB_SUPPORT)
742 int compression,
743#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200744 ssl_tls_prf_t tls_prf,
745 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200746 int minor_ver,
747 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200748 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000749{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200750 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 unsigned char keyblk[256];
752 unsigned char *key1;
753 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100754 unsigned char *mac_enc;
755 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000756 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200757 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000758 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000759 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 const mbedtls_cipher_info_t *cipher_info;
761 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100762
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200763#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
764 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
765 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200766 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200767 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000768#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000769
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200770 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200772 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000773#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200774 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200776 /*
777 * Get various info structures
778 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200779 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200780 if( ciphersuite_info == NULL )
781 {
782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200783 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
785 }
786
Hanno Becker8759e162017-12-27 21:34:08 +0000787 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100788 if( cipher_info == NULL )
789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000791 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100793 }
794
Hanno Becker8759e162017-12-27 21:34:08 +0000795 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100796 if( md_info == NULL )
797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000799 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100801 }
802
Hanno Beckera5a2b082019-05-15 14:03:01 +0100803#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100804 /* Copy own and peer's CID if the use of the CID
805 * extension has been negotiated. */
806 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
807 {
808 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100809
Hanno Becker4932f9f2019-05-03 15:23:51 +0100810 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100811 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100812 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100813 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100814
815 transform->out_cid_len = ssl->handshake->peer_cid_len;
816 memcpy( transform->out_cid, ssl->handshake->peer_cid,
817 ssl->handshake->peer_cid_len );
818 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
819 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100820 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100821#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100822
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200824 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200826 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100827 if( ret != 0 )
828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100830 return( ret );
831 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200834 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200835 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200836 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 /*
840 * Determine the appropriate key, IV and MAC length.
841 */
Paul Bakker68884e32013-01-07 18:20:04 +0100842
Hanno Beckere7f2df02017-12-27 08:17:40 +0000843 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200844
Hanno Beckerf1229442018-01-03 15:32:31 +0000845#if defined(MBEDTLS_GCM_C) || \
846 defined(MBEDTLS_CCM_C) || \
847 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200849 cipher_info->mode == MBEDTLS_MODE_CCM ||
850 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000851 {
Hanno Becker8759e162017-12-27 21:34:08 +0000852 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200853
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200854 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000855 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000856 transform->taglen =
857 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200858
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200859 /* All modes haves 96-bit IVs;
860 * GCM and CCM has 4 implicit and 8 explicit bytes
861 * ChachaPoly has all 12 bytes implicit
862 */
Paul Bakker68884e32013-01-07 18:20:04 +0100863 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200864 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
865 transform->fixed_ivlen = 12;
866 else
867 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200868
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200869 /* Minimum length of encrypted record */
870 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000871 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100872 }
873 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000874#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
875#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
876 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
877 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100878 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200879 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
881 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200884 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100885 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200887 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000888 mac_key_len = mbedtls_md_get_size( md_info );
889 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200892 /*
893 * If HMAC is to be truncated, we shall keep the leftmost bytes,
894 * (rfc 6066 page 13 or rfc 2104 section 4),
895 * so we only need to adjust the length here.
896 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200897 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000900
901#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
902 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000903 * HMAC implementation which also truncates the key
904 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000905 mac_key_len = transform->maclen;
906#endif
907 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909
910 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100911 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200913 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200915 transform->minlen = transform->maclen;
916 else
Paul Bakker68884e32013-01-07 18:20:04 +0100917 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200918 /*
919 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100920 * 1. if EtM is in use: one block plus MAC
921 * otherwise: * first multiple of blocklen greater than maclen
922 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200925 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100926 {
927 transform->minlen = transform->maclen
928 + cipher_info->block_size;
929 }
930 else
931#endif
932 {
933 transform->minlen = transform->maclen
934 + cipher_info->block_size
935 - transform->maclen % cipher_info->block_size;
936 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200939 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
940 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200941 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100942 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200943#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200945 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
946 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200947 {
948 transform->minlen += transform->ivlen;
949 }
950 else
951#endif
952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200955 }
Paul Bakker68884e32013-01-07 18:20:04 +0100956 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000958 else
959#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
960 {
961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
963 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Hanno Beckere7f2df02017-12-27 08:17:40 +0000965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
966 (unsigned) keylen,
967 (unsigned) transform->minlen,
968 (unsigned) transform->ivlen,
969 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
971 /*
972 * Finally setup the cipher contexts, IVs and MAC secrets.
973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200975 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000977 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000978 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Paul Bakker68884e32013-01-07 18:20:04 +0100980 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000981 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000983 /*
984 * This is not used in TLS v1.1.
985 */
Paul Bakker48916f92012-09-16 19:57:18 +0000986 iv_copy_len = ( transform->fixed_ivlen ) ?
987 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000988 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
989 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000990 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 }
992 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#endif /* MBEDTLS_SSL_CLI_C */
994#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200995 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 {
Hanno Beckere7f2df02017-12-27 08:17:40 +0000997 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +0000998 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000999
Hanno Becker81c7b182017-11-09 18:39:33 +00001000 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001001 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001003 /*
1004 * This is not used in TLS v1.1.
1005 */
Paul Bakker48916f92012-09-16 19:57:18 +00001006 iv_copy_len = ( transform->fixed_ivlen ) ?
1007 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001008 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1009 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001010 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001012 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1016 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001017 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Hanno Becker92231322018-01-03 15:32:51 +00001019#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001021 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001022 {
Hanno Becker92231322018-01-03 15:32:51 +00001023 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001027 }
1028
Hanno Becker81c7b182017-11-09 18:39:33 +00001029 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1030 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001031 }
1032 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1034#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1035 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001036 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001037 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001038 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1039 For AEAD-based ciphersuites, there is nothing to do here. */
1040 if( mac_key_len != 0 )
1041 {
1042 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1043 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1044 }
Paul Bakker68884e32013-01-07 18:20:04 +01001045 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001046 else
1047#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1050 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001051 }
Hanno Becker92231322018-01-03 15:32:51 +00001052#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1055 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001056 {
1057 int ret = 0;
1058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001060
Hanno Beckere7f2df02017-12-27 08:17:40 +00001061 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001062 transform->iv_enc, transform->iv_dec,
1063 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001064 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001065 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1068 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001069 }
1070 }
Hanno Becker92231322018-01-03 15:32:51 +00001071#else
1072 ((void) mac_dec);
1073 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001075
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001076#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1077 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001078 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001079 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001080 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001081 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001082 iv_copy_len );
1083 }
1084#endif
1085
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001086 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001087 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090 return( ret );
1091 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001092
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001093 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001094 cipher_info ) ) != 0 )
1095 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001097 return( ret );
1098 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001101 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001105 return( ret );
1106 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001109 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001113 return( ret );
1114 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#if defined(MBEDTLS_CIPHER_MODE_CBC)
1117 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1120 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001123 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001124 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1127 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001130 return( ret );
1131 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001135 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001137 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001139 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001142
Paul Bakker48916f92012-09-16 19:57:18 +00001143 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1144 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001145
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001146 if( deflateInit( &transform->ctx_deflate,
1147 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001148 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1151 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001152 }
1153 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001155
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 return( 0 );
1157}
1158
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001159/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001160 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001161 *
1162 * Inputs:
1163 * - SSL/TLS minor version
1164 * - hash associated with the ciphersuite (only used by TLS 1.2)
1165 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001166 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001167 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1168 */
1169static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1170 int minor_ver,
1171 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001172{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001173#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1174 (void) hash;
1175#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001176
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001177#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001178 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001179 {
1180 handshake->tls_prf = ssl3_prf;
1181 handshake->calc_verify = ssl_calc_verify_ssl;
1182 handshake->calc_finished = ssl_calc_finished_ssl;
1183 }
1184 else
1185#endif
1186#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001187 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001188 {
1189 handshake->tls_prf = tls1_prf;
1190 handshake->calc_verify = ssl_calc_verify_tls;
1191 handshake->calc_finished = ssl_calc_finished_tls;
1192 }
1193 else
1194#endif
1195#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1196#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001197 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1198 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001199 {
1200 handshake->tls_prf = tls_prf_sha384;
1201 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1202 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1203 }
1204 else
1205#endif
1206#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001207 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001208 {
1209 handshake->tls_prf = tls_prf_sha256;
1210 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1211 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1212 }
1213 else
1214#endif
1215#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1216 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1218 }
1219
1220 return( 0 );
1221}
1222
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001223/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001224 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001225 *
1226 * Parameters:
1227 * [in/out] handshake
1228 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1229 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001230 * [out] master
1231 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001232 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001233static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001234 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001235 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001236{
1237 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001238
1239#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001240 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001241 (void) ssl;
1242#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001243
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001244 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001245 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1247 return( 0 );
1248 }
1249
1250 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1251 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001252
1253#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001254 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001255 {
1256 unsigned char session_hash[48];
1257 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001258
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001259 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001260
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001261 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1262 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001264 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001265 "extended master secret",
1266 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001267 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001268 }
1269 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001270#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001271 {
1272 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1273 "master secret",
1274 handshake->randbytes, 64,
1275 master, 48 );
1276 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001277 if( ret != 0 )
1278 {
1279 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1280 return( ret );
1281 }
1282
1283 mbedtls_platform_zeroize( handshake->premaster,
1284 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001285
1286 return( 0 );
1287}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001288
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001289int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1290{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001291 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001292 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1293 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001294
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1296
1297 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001298 ret = ssl_set_handshake_prfs( ssl->handshake,
1299 ssl->minor_ver,
1300 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001301 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001302 {
1303 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001304 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001305 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001306
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001307 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001308 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001309 ssl->session_negotiate->master,
1310 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001311 if( ret != 0 )
1312 {
1313 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1314 return( ret );
1315 }
1316
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001317 /* Swap the client and server random values:
1318 * - MS derivation wanted client+server (RFC 5246 8.1)
1319 * - key derivation wants server+client (RFC 5246 6.3) */
1320 {
1321 unsigned char tmp[64];
1322 memcpy( tmp, ssl->handshake->randbytes, 64 );
1323 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1324 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1325 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1326 }
1327
1328 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001329 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001330 ssl->session_negotiate->ciphersuite,
1331 ssl->session_negotiate->master,
1332#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1333 ssl->session_negotiate->encrypt_then_mac,
1334#endif
1335#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1336 ssl->session_negotiate->trunc_hmac,
1337#endif
1338#if defined(MBEDTLS_ZLIB_SUPPORT)
1339 ssl->session_negotiate->compression,
1340#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001341 ssl->handshake->tls_prf,
1342 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001343 ssl->minor_ver,
1344 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001345 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001346 if( ret != 0 )
1347 {
1348 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1349 return( ret );
1350 }
1351
1352 /* We no longer need Server/ClientHello.random values */
1353 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1354 sizeof( ssl->handshake->randbytes ) );
1355
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001356 /* Allocate compression buffer */
1357#if defined(MBEDTLS_ZLIB_SUPPORT)
1358 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1359 ssl->compress_buf == NULL )
1360 {
1361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1362 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1363 if( ssl->compress_buf == NULL )
1364 {
1365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001366 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001367 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1368 }
1369 }
1370#endif
1371
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1373
1374 return( 0 );
1375}
1376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001378void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1379 unsigned char hash[36],
1380 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001381{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001382 mbedtls_md5_context md5;
1383 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001384 unsigned char pad_1[48];
1385 unsigned char pad_2[48];
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001389 mbedtls_md5_init( &md5 );
1390 mbedtls_sha1_init( &sha1 );
1391
1392 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1393 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Paul Bakker380da532012-04-18 16:10:25 +00001395 memset( pad_1, 0x36, 48 );
1396 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001398 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1399 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1400 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001402 mbedtls_md5_starts_ret( &md5 );
1403 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1404 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1405 mbedtls_md5_update_ret( &md5, hash, 16 );
1406 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001408 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1409 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1410 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001411
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001412 mbedtls_sha1_starts_ret( &sha1 );
1413 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1414 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1415 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1416 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001417
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001418 *hlen = 36;
1419
1420 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001422
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001423 mbedtls_md5_free( &md5 );
1424 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001425
Paul Bakker380da532012-04-18 16:10:25 +00001426 return;
1427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001431void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1432 unsigned char hash[36],
1433 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001434{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001435 mbedtls_md5_context md5;
1436 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001439
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001440 mbedtls_md5_init( &md5 );
1441 mbedtls_sha1_init( &sha1 );
1442
1443 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1444 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001445
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001446 mbedtls_md5_finish_ret( &md5, hash );
1447 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001448
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001449 *hlen = 36;
1450
1451 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001453
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001454 mbedtls_md5_free( &md5 );
1455 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001456
Paul Bakker380da532012-04-18 16:10:25 +00001457 return;
1458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1462#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001463void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1464 unsigned char hash[32],
1465 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001466{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001467 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001468
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001469 mbedtls_sha256_init( &sha256 );
1470
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001473 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001474 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001475
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001476 *hlen = 32;
1477
1478 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001480
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001481 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001482
Paul Bakker380da532012-04-18 16:10:25 +00001483 return;
1484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001488void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1489 unsigned char hash[48],
1490 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001491{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001492 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001494 mbedtls_sha512_init( &sha512 );
1495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001497
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001498 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001499 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001500
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001501 *hlen = 48;
1502
1503 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001505
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001506 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001507
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 return;
1509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_SHA512_C */
1511#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1514int 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 +02001515{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001516 unsigned char *p = ssl->handshake->premaster;
1517 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001518 const unsigned char *psk = ssl->conf->psk;
1519 size_t psk_len = ssl->conf->psk_len;
1520
1521 /* If the psk callback was called, use its result */
1522 if( ssl->handshake->psk != NULL )
1523 {
1524 psk = ssl->handshake->psk;
1525 psk_len = ssl->handshake->psk_len;
1526 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001527
1528 /*
1529 * PMS = struct {
1530 * opaque other_secret<0..2^16-1>;
1531 * opaque psk<0..2^16-1>;
1532 * };
1533 * with "other_secret" depending on the particular key exchange
1534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1536 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001537 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001538 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001540
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001541 *(p++) = (unsigned char)( psk_len >> 8 );
1542 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001543
1544 if( end < p || (size_t)( end - p ) < psk_len )
1545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1546
1547 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001548 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001549 }
1550 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1552#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1553 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001554 {
1555 /*
1556 * other_secret already set by the ClientKeyExchange message,
1557 * and is 48 bytes long
1558 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001559 if( end - p < 2 )
1560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1561
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001562 *p++ = 0;
1563 *p++ = 48;
1564 p += 48;
1565 }
1566 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1568#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1569 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001570 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001571 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001572 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001573
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001574 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001576 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001577 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001580 return( ret );
1581 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001582 *(p++) = (unsigned char)( len >> 8 );
1583 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001584 p += len;
1585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001587 }
1588 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1590#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1591 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001592 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001593 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001594 size_t zlen;
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001597 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001598 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001601 return( ret );
1602 }
1603
1604 *(p++) = (unsigned char)( zlen >> 8 );
1605 *(p++) = (unsigned char)( zlen );
1606 p += zlen;
1607
Janos Follath3fbdada2018-08-15 10:26:53 +01001608 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1609 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 }
1611 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 }
1617
1618 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001619 if( end - p < 2 )
1620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001621
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001622 *(p++) = (unsigned char)( psk_len >> 8 );
1623 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001624
1625 if( end < p || (size_t)( end - p ) < psk_len )
1626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1627
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001628 memcpy( p, psk, psk_len );
1629 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001630
1631 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1632
1633 return( 0 );
1634}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001638/*
1639 * SSLv3.0 MAC functions
1640 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001641#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001642static void ssl_mac( mbedtls_md_context_t *md_ctx,
1643 const unsigned char *secret,
1644 const unsigned char *buf, size_t len,
1645 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001646 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001647{
1648 unsigned char header[11];
1649 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001650 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1652 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001653
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001654 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001656 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001657 else
Paul Bakker68884e32013-01-07 18:20:04 +01001658 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
1660 memcpy( header, ctr, 8 );
1661 header[ 8] = (unsigned char) type;
1662 header[ 9] = (unsigned char)( len >> 8 );
1663 header[10] = (unsigned char)( len );
1664
Paul Bakker68884e32013-01-07 18:20:04 +01001665 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 mbedtls_md_starts( md_ctx );
1667 mbedtls_md_update( md_ctx, secret, md_size );
1668 mbedtls_md_update( md_ctx, padding, padlen );
1669 mbedtls_md_update( md_ctx, header, 11 );
1670 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001671 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
Paul Bakker68884e32013-01-07 18:20:04 +01001673 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 mbedtls_md_starts( md_ctx );
1675 mbedtls_md_update( md_ctx, secret, md_size );
1676 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001677 mbedtls_md_update( md_ctx, out, md_size );
1678 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001679}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001681
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001682/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001683 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001684#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001685 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1686 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1687 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1688/* This function makes sure every byte in the memory region is accessed
1689 * (in ascending addresses order) */
1690static void ssl_read_memory( unsigned char *p, size_t len )
1691{
1692 unsigned char acc = 0;
1693 volatile unsigned char force;
1694
1695 for( ; len != 0; p++, len-- )
1696 acc ^= *p;
1697
1698 force = acc;
1699 (void) force;
1700}
1701#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1702
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001703/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001705 */
Hanno Becker3307b532017-12-27 21:37:21 +00001706
Hanno Beckera5a2b082019-05-15 14:03:01 +01001707#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001708/* This functions transforms a DTLS plaintext fragment and a record content
1709 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001710 *
1711 * struct {
1712 * opaque content[DTLSPlaintext.length];
1713 * ContentType real_type;
1714 * uint8 zeros[length_of_padding];
1715 * } DTLSInnerPlaintext;
1716 *
1717 * Input:
1718 * - `content`: The beginning of the buffer holding the
1719 * plaintext to be wrapped.
1720 * - `*content_size`: The length of the plaintext in Bytes.
1721 * - `max_len`: The number of Bytes available starting from
1722 * `content`. This must be `>= *content_size`.
1723 * - `rec_type`: The desired record content type.
1724 *
1725 * Output:
1726 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1727 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1728 *
1729 * Returns:
1730 * - `0` on success.
1731 * - A negative error code if `max_len` didn't offer enough space
1732 * for the expansion.
1733 */
1734static int ssl_cid_build_inner_plaintext( unsigned char *content,
1735 size_t *content_size,
1736 size_t remaining,
1737 uint8_t rec_type )
1738{
1739 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001740 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1741 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1742 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001743
1744 /* Write real content type */
1745 if( remaining == 0 )
1746 return( -1 );
1747 content[ len ] = rec_type;
1748 len++;
1749 remaining--;
1750
1751 if( remaining < pad )
1752 return( -1 );
1753 memset( content + len, 0, pad );
1754 len += pad;
1755 remaining -= pad;
1756
1757 *content_size = len;
1758 return( 0 );
1759}
1760
Hanno Becker7dc25772019-05-20 15:08:01 +01001761/* This function parses a DTLSInnerPlaintext structure.
1762 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001763static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1764 size_t *content_size,
1765 uint8_t *rec_type )
1766{
1767 size_t remaining = *content_size;
1768
1769 /* Determine length of padding by skipping zeroes from the back. */
1770 do
1771 {
1772 if( remaining == 0 )
1773 return( -1 );
1774 remaining--;
1775 } while( content[ remaining ] == 0 );
1776
1777 *content_size = remaining;
1778 *rec_type = content[ remaining ];
1779
1780 return( 0 );
1781}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001782#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001783
Hanno Becker99abf512019-05-20 14:50:53 +01001784/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001785 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001786static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001787 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001788 mbedtls_record *rec )
1789{
Hanno Becker99abf512019-05-20 14:50:53 +01001790 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001791 *
1792 * additional_data = seq_num + TLSCompressed.type +
1793 * TLSCompressed.version + TLSCompressed.length;
1794 *
Hanno Becker99abf512019-05-20 14:50:53 +01001795 * For the CID extension, this is extended as follows
1796 * (quoting draft-ietf-tls-dtls-connection-id-05,
1797 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001798 *
1799 * additional_data = seq_num + DTLSPlaintext.type +
1800 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001801 * cid +
1802 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001803 * length_of_DTLSInnerPlaintext;
1804 */
1805
Hanno Becker3307b532017-12-27 21:37:21 +00001806 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1807 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001808 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001809
Hanno Beckera5a2b082019-05-15 14:03:01 +01001810#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001811 if( rec->cid_len != 0 )
1812 {
1813 memcpy( add_data + 11, rec->cid, rec->cid_len );
1814 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1815 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1816 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1817 *add_data_len = 13 + 1 + rec->cid_len;
1818 }
1819 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001820#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001821 {
1822 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1823 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1824 *add_data_len = 13;
1825 }
Hanno Becker3307b532017-12-27 21:37:21 +00001826}
1827
Hanno Becker611a83b2018-01-03 14:27:32 +00001828int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1829 mbedtls_ssl_transform *transform,
1830 mbedtls_record *rec,
1831 int (*f_rng)(void *, unsigned char *, size_t),
1832 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001835 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001836 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001837 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001838 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001839 size_t post_avail;
1840
1841 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001842#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001843 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001844 ((void) ssl);
1845#endif
1846
1847 /* The PRNG is used for dynamic IV generation that's used
1848 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1849#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1850 ( defined(MBEDTLS_AES_C) || \
1851 defined(MBEDTLS_ARIA_C) || \
1852 defined(MBEDTLS_CAMELLIA_C) ) && \
1853 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1854 ((void) f_rng);
1855 ((void) p_rng);
1856#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001859
Hanno Becker3307b532017-12-27 21:37:21 +00001860 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001861 {
Hanno Becker3307b532017-12-27 21:37:21 +00001862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1864 }
Hanno Becker505089d2019-05-01 09:45:57 +01001865 if( rec == NULL
1866 || rec->buf == NULL
1867 || rec->buf_len < rec->data_offset
1868 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001869#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001870 || rec->cid_len != 0
1871#endif
1872 )
Hanno Becker3307b532017-12-27 21:37:21 +00001873 {
1874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001876 }
1877
Hanno Becker3307b532017-12-27 21:37:21 +00001878 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001879 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001881 data, rec->data_len );
1882
1883 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1884
1885 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1886 {
1887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1888 (unsigned) rec->data_len,
1889 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1891 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001892
Hanno Beckera5a2b082019-05-15 14:03:01 +01001893#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001894 /*
1895 * Add CID information
1896 */
1897 rec->cid_len = transform->out_cid_len;
1898 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1899 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001900
1901 if( rec->cid_len != 0 )
1902 {
1903 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001904 * Wrap plaintext into DTLSInnerPlaintext structure.
1905 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001906 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001907 * Note that this changes `rec->data_len`, and hence
1908 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001909 */
1910 if( ssl_cid_build_inner_plaintext( data,
1911 &rec->data_len,
1912 post_avail,
1913 rec->type ) != 0 )
1914 {
1915 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1916 }
1917
1918 rec->type = MBEDTLS_SSL_MSG_CID;
1919 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001920#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001921
Hanno Becker92c930f2019-04-29 17:31:37 +01001922 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1923
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001925 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001927#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 if( mode == MBEDTLS_MODE_STREAM ||
1929 ( mode == MBEDTLS_MODE_CBC
1930#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001931 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001932#endif
1933 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 {
Hanno Becker3307b532017-12-27 21:37:21 +00001935 if( post_avail < transform->maclen )
1936 {
1937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1938 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1939 }
1940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001942 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001943 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001944 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001945 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1946 data, rec->data_len, rec->ctr, rec->type, mac );
1947 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001948 }
1949 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001950#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1952 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001953 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001954 {
Hanno Becker992b6872017-11-09 18:57:39 +00001955 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1956
Hanno Beckere83efe62019-04-29 13:52:53 +01001957 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001958
Hanno Becker3307b532017-12-27 21:37:21 +00001959 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001960 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001961 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1962 data, rec->data_len );
1963 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1964 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1965
1966 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001967 }
1968 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001969#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 }
1974
Hanno Becker3307b532017-12-27 21:37:21 +00001975 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1976 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001977
Hanno Becker3307b532017-12-27 21:37:21 +00001978 rec->data_len += transform->maclen;
1979 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001980 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001981 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001982#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001983
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001984 /*
1985 * Encrypt
1986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1988 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001990 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001991 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001993 "including %d bytes of padding",
1994 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001995
Hanno Becker3307b532017-12-27 21:37:21 +00001996 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1997 transform->iv_enc, transform->ivlen,
1998 data, rec->data_len,
1999 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002002 return( ret );
2003 }
2004
Hanno Becker3307b532017-12-27 21:37:21 +00002005 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2008 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 }
Paul Bakker68884e32013-01-07 18:20:04 +01002011 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002013
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002014#if defined(MBEDTLS_GCM_C) || \
2015 defined(MBEDTLS_CCM_C) || \
2016 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002018 mode == MBEDTLS_MODE_CCM ||
2019 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002020 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002021 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002022 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002023 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002024
Hanno Becker3307b532017-12-27 21:37:21 +00002025 /* Check that there's space for both the authentication tag
2026 * and the explicit IV before and after the record content. */
2027 if( post_avail < transform->taglen ||
2028 rec->data_offset < explicit_iv_len )
2029 {
2030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2031 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2032 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002033
Paul Bakker68884e32013-01-07 18:20:04 +01002034 /*
2035 * Generate IV
2036 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002037 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2038 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002039 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002041 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2042 explicit_iv_len );
2043 /* Prefix record content with explicit IV. */
2044 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002045 }
2046 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2047 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002048 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002049 unsigned char i;
2050
2051 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2052
2053 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002054 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002055 }
2056 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002057 {
2058 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002061 }
2062
Hanno Beckere83efe62019-04-29 13:52:53 +01002063 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002064
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002065 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2066 iv, transform->ivlen );
2067 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002068 data - explicit_iv_len, explicit_iv_len );
2069 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002070 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002072 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002073 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002074
Paul Bakker68884e32013-01-07 18:20:04 +01002075 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002076 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002077 */
Hanno Becker3307b532017-12-27 21:37:21 +00002078
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002079 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002080 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002081 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002082 data, rec->data_len, /* source */
2083 data, &rec->data_len, /* destination */
2084 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002087 return( ret );
2088 }
2089
Hanno Becker3307b532017-12-27 21:37:21 +00002090 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2091 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002092
Hanno Becker3307b532017-12-27 21:37:21 +00002093 rec->data_len += transform->taglen + explicit_iv_len;
2094 rec->data_offset -= explicit_iv_len;
2095 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002096 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002097 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2100#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002101 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002104 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002105 size_t padlen, i;
2106 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002107
Hanno Becker3307b532017-12-27 21:37:21 +00002108 /* Currently we're always using minimal padding
2109 * (up to 255 bytes would be allowed). */
2110 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2111 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 padlen = 0;
2113
Hanno Becker3307b532017-12-27 21:37:21 +00002114 /* Check there's enough space in the buffer for the padding. */
2115 if( post_avail < padlen + 1 )
2116 {
2117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2118 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2119 }
2120
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002122 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002123
Hanno Becker3307b532017-12-27 21:37:21 +00002124 rec->data_len += padlen + 1;
2125 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002128 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002129 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2130 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131 */
Hanno Becker3307b532017-12-27 21:37:21 +00002132 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002133 {
Hanno Becker3307b532017-12-27 21:37:21 +00002134 if( f_rng == NULL )
2135 {
2136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2138 }
2139
2140 if( rec->data_offset < transform->ivlen )
2141 {
2142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2143 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2144 }
2145
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002146 /*
2147 * Generate IV
2148 */
Hanno Becker3307b532017-12-27 21:37:21 +00002149 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002150 if( ret != 0 )
2151 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002152
Hanno Becker3307b532017-12-27 21:37:21 +00002153 memcpy( data - transform->ivlen, transform->iv_enc,
2154 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002160 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002161 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002162 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163
Hanno Becker3307b532017-12-27 21:37:21 +00002164 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2165 transform->iv_enc,
2166 transform->ivlen,
2167 data, rec->data_len,
2168 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002171 return( ret );
2172 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002173
Hanno Becker3307b532017-12-27 21:37:21 +00002174 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002178 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002181 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002182 {
2183 /*
2184 * Save IV in SSL3 and TLS1
2185 */
Hanno Becker3307b532017-12-27 21:37:21 +00002186 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2187 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 }
Hanno Becker3307b532017-12-27 21:37:21 +00002189 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002190#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002191 {
2192 data -= transform->ivlen;
2193 rec->data_offset -= transform->ivlen;
2194 rec->data_len += transform->ivlen;
2195 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002198 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002199 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002200 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2201
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002202 /*
2203 * MAC(MAC_write_key, seq_num +
2204 * TLSCipherText.type +
2205 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002206 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002207 * IV + // except for TLS 1.0
2208 * ENC(content + padding + padding_length));
2209 */
Hanno Becker3307b532017-12-27 21:37:21 +00002210
2211 if( post_avail < transform->maclen)
2212 {
2213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2214 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2215 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002216
Hanno Beckere83efe62019-04-29 13:52:53 +01002217 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002220 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002221 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002222
Hanno Becker3307b532017-12-27 21:37:21 +00002223 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002224 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002225 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2226 data, rec->data_len );
2227 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2228 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002229
Hanno Becker3307b532017-12-27 21:37:21 +00002230 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002231
Hanno Becker3307b532017-12-27 21:37:21 +00002232 rec->data_len += transform->maclen;
2233 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002234 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002235 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002238 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002240 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002244 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002246 /* Make extra sure authentication was performed, exactly once */
2247 if( auth_done != 1 )
2248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002251 }
2252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
2255 return( 0 );
2256}
2257
Hanno Becker611a83b2018-01-03 14:27:32 +00002258int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2259 mbedtls_ssl_transform *transform,
2260 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002261{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002262 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002264 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002265#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002266 size_t padlen = 0, correct = 1;
2267#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002268 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002269 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002270 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002271
Hanno Becker611a83b2018-01-03 14:27:32 +00002272#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002273 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002274 ((void) ssl);
2275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002278 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002279 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2282 }
2283 if( rec == NULL ||
2284 rec->buf == NULL ||
2285 rec->buf_len < rec->data_offset ||
2286 rec->buf_len - rec->data_offset < rec->data_len )
2287 {
2288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002290 }
2291
Hanno Becker4c6876b2017-12-27 21:28:58 +00002292 data = rec->buf + rec->data_offset;
2293 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Hanno Beckera5a2b082019-05-15 14:03:01 +01002295#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002296 /*
2297 * Match record's CID with incoming CID.
2298 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002299 if( rec->cid_len != transform->in_cid_len ||
2300 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2301 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002302 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002303 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002304#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2307 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002308 {
2309 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002310 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2311 transform->iv_dec,
2312 transform->ivlen,
2313 data, rec->data_len,
2314 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002317 return( ret );
2318 }
2319
Hanno Becker4c6876b2017-12-27 21:28:58 +00002320 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 }
Paul Bakker68884e32013-01-07 18:20:04 +01002326 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002328#if defined(MBEDTLS_GCM_C) || \
2329 defined(MBEDTLS_CCM_C) || \
2330 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002332 mode == MBEDTLS_MODE_CCM ||
2333 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002334 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002336 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002337
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 /*
2339 * Compute and update sizes
2340 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002341 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002344 "+ taglen (%d)", rec->data_len,
2345 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002347 }
Paul Bakker68884e32013-01-07 18:20:04 +01002348
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002349 /*
2350 * Prepare IV
2351 */
2352 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2353 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002354 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002355 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002356 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002357
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002358 }
2359 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2360 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002361 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002362 unsigned char i;
2363
2364 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2365
2366 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002367 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002368 }
2369 else
2370 {
2371 /* Reminder if we ever add an AEAD mode with a different size */
2372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2373 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2374 }
2375
Hanno Becker4c6876b2017-12-27 21:28:58 +00002376 data += explicit_iv_len;
2377 rec->data_offset += explicit_iv_len;
2378 rec->data_len -= explicit_iv_len + transform->taglen;
2379
Hanno Beckere83efe62019-04-29 13:52:53 +01002380 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002381 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002382 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002383
2384 memcpy( transform->iv_dec + transform->fixed_ivlen,
2385 data - explicit_iv_len, explicit_iv_len );
2386
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002387 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002388 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002389 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002390
Paul Bakker68884e32013-01-07 18:20:04 +01002391
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002392 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002393 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002394 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002395 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2396 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002397 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002398 data, rec->data_len,
2399 data, &olen,
2400 data + rec->data_len,
2401 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2406 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002407
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002408 return( ret );
2409 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002410 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002411
Hanno Becker4c6876b2017-12-27 21:28:58 +00002412 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002416 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2420#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002421 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002424 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002425
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 /*
Paul Bakker45829992013-01-03 14:52:21 +01002427 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002430 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2431 {
2432 /* The ciphertext is prefixed with the CBC IV. */
2433 minlen += transform->ivlen;
2434 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002435#endif
Paul Bakker45829992013-01-03 14:52:21 +01002436
Hanno Becker4c6876b2017-12-27 21:28:58 +00002437 /* Size considerations:
2438 *
2439 * - The CBC cipher text must not be empty and hence
2440 * at least of size transform->ivlen.
2441 *
2442 * Together with the potential IV-prefix, this explains
2443 * the first of the two checks below.
2444 *
2445 * - The record must contain a MAC, either in plain or
2446 * encrypted, depending on whether Encrypt-then-MAC
2447 * is used or not.
2448 * - If it is, the message contains the IV-prefix,
2449 * the CBC ciphertext, and the MAC.
2450 * - If it is not, the padded plaintext, and hence
2451 * the CBC ciphertext, has at least length maclen + 1
2452 * because there is at least the padding length byte.
2453 *
2454 * As the CBC ciphertext is not empty, both cases give the
2455 * lower bound minlen + maclen + 1 on the record size, which
2456 * we test for in the second check below.
2457 */
2458 if( rec->data_len < minlen + transform->ivlen ||
2459 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002462 "+ 1 ) ( + expl IV )", rec->data_len,
2463 transform->ivlen,
2464 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002466 }
2467
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002468 /*
2469 * Authenticate before decrypt if enabled
2470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002472 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002473 {
Hanno Becker992b6872017-11-09 18:57:39 +00002474 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002477
Hanno Becker4c6876b2017-12-27 21:28:58 +00002478 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2479 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002480
Hanno Beckere83efe62019-04-29 13:52:53 +01002481 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002482
Hanno Beckere83efe62019-04-29 13:52:53 +01002483 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2484 add_data_len );
2485 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2486 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002487 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2488 data, rec->data_len );
2489 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2490 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002491
Hanno Becker4c6876b2017-12-27 21:28:58 +00002492 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2493 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002494 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002495 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002496
Hanno Becker4c6876b2017-12-27 21:28:58 +00002497 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2498 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002503 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002506
2507 /*
2508 * Check length sanity
2509 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002510 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002513 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002515 }
2516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002518 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002519 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002520 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002521 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002522 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002523 /* This is safe because data_len >= minlen + maclen + 1 initially,
2524 * and at this point we have at most subtracted maclen (note that
2525 * minlen == transform->ivlen here). */
2526 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002527
Hanno Becker4c6876b2017-12-27 21:28:58 +00002528 data += transform->ivlen;
2529 rec->data_offset += transform->ivlen;
2530 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002533
Hanno Becker4c6876b2017-12-27 21:28:58 +00002534 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2535 transform->iv_dec, transform->ivlen,
2536 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002539 return( ret );
2540 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002541
Hanno Becker4c6876b2017-12-27 21:28:58 +00002542 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002546 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002549 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002550 {
2551 /*
2552 * Save IV in SSL3 and TLS1
2553 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002554 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2555 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002557#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Hanno Becker4c6876b2017-12-27 21:28:58 +00002559 /* Safe since data_len >= minlen + maclen + 1, so after having
2560 * subtracted at most minlen and maclen up to this point,
2561 * data_len > 0. */
2562 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002563
Hanno Becker4c6876b2017-12-27 21:28:58 +00002564 if( auth_done == 1 )
2565 {
2566 correct *= ( rec->data_len >= padlen + 1 );
2567 padlen *= ( rec->data_len >= padlen + 1 );
2568 }
2569 else
Paul Bakker45829992013-01-03 14:52:21 +01002570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002572 if( rec->data_len < transform->maclen + padlen + 1 )
2573 {
2574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2575 rec->data_len,
2576 transform->maclen,
2577 padlen + 1 ) );
2578 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002579#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002580
2581 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2582 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Hanno Becker4c6876b2017-12-27 21:28:58 +00002585 padlen++;
2586
2587 /* Regardless of the validity of the padding,
2588 * we have data_len >= padlen here. */
2589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002593 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_DEBUG_ALL)
2596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002597 "should be no more than %d",
2598 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002599#endif
Paul Bakker45829992013-01-03 14:52:21 +01002600 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602 }
2603 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2605#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2606 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002607 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002609 /* The padding check involves a series of up to 256
2610 * consecutive memory reads at the end of the record
2611 * plaintext buffer. In order to hide the length and
2612 * validity of the padding, always perform exactly
2613 * `min(256,plaintext_len)` reads (but take into account
2614 * only the last `padlen` bytes for the padding check). */
2615 size_t pad_count = 0;
2616 size_t real_count = 0;
2617 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002618
Hanno Becker4c6876b2017-12-27 21:28:58 +00002619 /* Index of first padding byte; it has been ensured above
2620 * that the subtraction is safe. */
2621 size_t const padding_idx = rec->data_len - padlen;
2622 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2623 size_t const start_idx = rec->data_len - num_checks;
2624 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002625
Hanno Becker4c6876b2017-12-27 21:28:58 +00002626 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002627 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002628 real_count |= ( idx >= padding_idx );
2629 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002630 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002631 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002634 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002636#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002637 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002639 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2641 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002645 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002646
Hanno Becker4c6876b2017-12-27 21:28:58 +00002647 /* If the padding was found to be invalid, padlen == 0
2648 * and the subtraction is safe. If the padding was found valid,
2649 * padlen hasn't been changed and the previous assertion
2650 * data_len >= padlen still holds. */
2651 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002653 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002655 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002659 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002661#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002663 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002664#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
2666 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002667 * Authenticate if not done yet.
2668 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002670#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002671 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002672 {
Hanno Becker992b6872017-11-09 18:57:39 +00002673 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002674
Hanno Becker4c6876b2017-12-27 21:28:58 +00002675 /* If the initial value of padlen was such that
2676 * data_len < maclen + padlen + 1, then padlen
2677 * got reset to 1, and the initial check
2678 * data_len >= minlen + maclen + 1
2679 * guarantees that at this point we still
2680 * have at least data_len >= maclen.
2681 *
2682 * If the initial value of padlen was such that
2683 * data_len >= maclen + padlen + 1, then we have
2684 * subtracted either padlen + 1 (if the padding was correct)
2685 * or 0 (if the padding was incorrect) since then,
2686 * hence data_len >= maclen in any case.
2687 */
2688 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Hanno Beckere83efe62019-04-29 13:52:53 +01002690 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002693 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002694 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002695 ssl_mac( &transform->md_ctx_dec,
2696 transform->mac_dec,
2697 data, rec->data_len,
2698 rec->ctr, rec->type,
2699 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002700 }
2701 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2703#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2704 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002705 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002706 {
2707 /*
2708 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002709 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002710 *
2711 * Known timing attacks:
2712 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2713 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002714 * To compensate for different timings for the MAC calculation
2715 * depending on how much padding was removed (which is determined
2716 * by padlen), process extra_run more blocks through the hash
2717 * function.
2718 *
2719 * The formula in the paper is
2720 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2721 * where L1 is the size of the header plus the decrypted message
2722 * plus CBC padding and L2 is the size of the header plus the
2723 * decrypted message. This is for an underlying hash function
2724 * with 64-byte blocks.
2725 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2726 * correctly. We round down instead of up, so -56 is the correct
2727 * value for our calculations instead of -55.
2728 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002729 * Repeat the formula rather than defining a block_size variable.
2730 * This avoids requiring division by a variable at runtime
2731 * (which would be marginally less efficient and would require
2732 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002733 */
2734 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002735 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002736
2737 /*
2738 * The next two sizes are the minimum and maximum values of
2739 * in_msglen over all padlen values.
2740 *
2741 * They're independent of padlen, since we previously did
2742 * in_msglen -= padlen.
2743 *
2744 * Note that max_len + maclen is never more than the buffer
2745 * length, as we previously did in_msglen -= maclen too.
2746 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002747 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002748 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2749
Hanno Becker4c6876b2017-12-27 21:28:58 +00002750 memset( tmp, 0, sizeof( tmp ) );
2751
2752 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002753 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002754#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2755 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 case MBEDTLS_MD_MD5:
2757 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002758 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002759 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002760 extra_run =
2761 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2762 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002763 break;
2764#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002765#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002766 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002767 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002768 extra_run =
2769 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2770 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002771 break;
2772#endif
2773 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002775 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2776 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002777
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002778 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002779
Hanno Beckere83efe62019-04-29 13:52:53 +01002780 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2781 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002782 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2783 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002784 /* Make sure we access everything even when padlen > 0. This
2785 * makes the synchronisation requirements for just-in-time
2786 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002787 ssl_read_memory( data + rec->data_len, padlen );
2788 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002789
2790 /* Call mbedtls_md_process at least once due to cache attacks
2791 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002792 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002793 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002794
Hanno Becker4c6876b2017-12-27 21:28:58 +00002795 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002796
2797 /* Make sure we access all the memory that could contain the MAC,
2798 * before we check it in the next code block. This makes the
2799 * synchronisation requirements for just-in-time Prime+Probe
2800 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002801 ssl_read_memory( data + min_len,
2802 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002803 }
2804 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2806 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2809 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002810 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002812#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002813 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2814 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002815#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
Hanno Becker4c6876b2017-12-27 21:28:58 +00002817 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2818 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#if defined(MBEDTLS_SSL_DEBUG_ALL)
2821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002822#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002823 correct = 0;
2824 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002825 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002826 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002827
2828 /*
2829 * Finally check the correct flag
2830 */
2831 if( correct == 0 )
2832 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002833#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002834
2835 /* Make extra sure authentication was performed, exactly once */
2836 if( auth_done != 1 )
2837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2839 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002840 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Hanno Beckera5a2b082019-05-15 14:03:01 +01002842#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002843 if( rec->cid_len != 0 )
2844 {
2845 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2846 &rec->type );
2847 if( ret != 0 )
2848 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2849 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002850#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
2854 return( 0 );
2855}
2856
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002857#undef MAC_NONE
2858#undef MAC_PLAINTEXT
2859#undef MAC_CIPHERTEXT
2860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002862/*
2863 * Compression/decompression functions
2864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002866{
2867 int ret;
2868 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002869 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002870 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002871 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002874
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002875 if( len_pre == 0 )
2876 return( 0 );
2877
Paul Bakker2770fbd2012-07-03 13:30:23 +00002878 memcpy( msg_pre, ssl->out_msg, len_pre );
2879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002881 ssl->out_msglen ) );
2882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002884 ssl->out_msg, ssl->out_msglen );
2885
Paul Bakker48916f92012-09-16 19:57:18 +00002886 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2887 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2888 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002889 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890
Paul Bakker48916f92012-09-16 19:57:18 +00002891 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002892 if( ret != Z_OK )
2893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2895 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002896 }
2897
Angus Grattond8213d02016-05-25 20:56:48 +10002898 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002899 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902 ssl->out_msglen ) );
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905 ssl->out_msg, ssl->out_msglen );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908
2909 return( 0 );
2910}
2911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002913{
2914 int ret;
2915 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002916 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002918 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002921
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002922 if( len_pre == 0 )
2923 return( 0 );
2924
Paul Bakker2770fbd2012-07-03 13:30:23 +00002925 memcpy( msg_pre, ssl->in_msg, len_pre );
2926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002928 ssl->in_msglen ) );
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931 ssl->in_msg, ssl->in_msglen );
2932
Paul Bakker48916f92012-09-16 19:57:18 +00002933 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2934 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2935 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002936 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002937 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938
Paul Bakker48916f92012-09-16 19:57:18 +00002939 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940 if( ret != Z_OK )
2941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2943 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002944 }
2945
Angus Grattond8213d02016-05-25 20:56:48 +10002946 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002947 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950 ssl->in_msglen ) );
2951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953 ssl->in_msg, ssl->in_msglen );
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956
2957 return( 0 );
2958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2962static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_PROTO_DTLS)
2965static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002966{
2967 /* If renegotiation is not enforced, retransmit until we would reach max
2968 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002969 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002970 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002971 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002972 unsigned char doublings = 1;
2973
2974 while( ratio != 0 )
2975 {
2976 ++doublings;
2977 ratio >>= 1;
2978 }
2979
2980 if( ++ssl->renego_records_seen > doublings )
2981 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002983 return( 0 );
2984 }
2985 }
2986
2987 return( ssl_write_hello_request( ssl ) );
2988}
2989#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002991
Paul Bakker5121ce52009-01-03 21:22:43 +00002992/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002993 * Fill the input message buffer by appending data to it.
2994 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002995 *
2996 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2997 * available (from this read and/or a previous one). Otherwise, an error code
2998 * is returned (possibly EOF or WANT_READ).
2999 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003000 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3001 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3002 * since we always read a whole datagram at once.
3003 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003004 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003005 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003008{
Paul Bakker23986e52011-04-24 08:57:21 +00003009 int ret;
3010 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003013
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003014 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003017 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003019 }
3020
Angus Grattond8213d02016-05-25 20:56:48 +10003021 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003025 }
3026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003028 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003029 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003030 uint32_t timeout;
3031
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003032 /* Just to be sure */
3033 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3034 {
3035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3036 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3038 }
3039
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003040 /*
3041 * The point is, we need to always read a full datagram at once, so we
3042 * sometimes read more then requested, and handle the additional data.
3043 * It could be the rest of the current record (while fetching the
3044 * header) and/or some other records in the same datagram.
3045 */
3046
3047 /*
3048 * Move to the next record in the already read datagram if applicable
3049 */
3050 if( ssl->next_record_offset != 0 )
3051 {
3052 if( ssl->in_left < ssl->next_record_offset )
3053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003056 }
3057
3058 ssl->in_left -= ssl->next_record_offset;
3059
3060 if( ssl->in_left != 0 )
3061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003063 ssl->next_record_offset ) );
3064 memmove( ssl->in_hdr,
3065 ssl->in_hdr + ssl->next_record_offset,
3066 ssl->in_left );
3067 }
3068
3069 ssl->next_record_offset = 0;
3070 }
3071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003073 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003074
3075 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003076 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003077 */
3078 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003081 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003082 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003083
3084 /*
3085 * A record can't be split accross datagrams. If we need to read but
3086 * are not at the beginning of a new record, the caller did something
3087 * wrong.
3088 */
3089 if( ssl->in_left != 0 )
3090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003093 }
3094
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003095 /*
3096 * Don't even try to read if time's out already.
3097 * This avoids by-passing the timer when repeatedly receiving messages
3098 * that will end up being dropped.
3099 */
3100 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003101 {
3102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003103 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003104 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003105 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003106 {
Angus Grattond8213d02016-05-25 20:56:48 +10003107 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003110 timeout = ssl->handshake->retransmit_timeout;
3111 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003112 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003115
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003116 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003117 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3118 timeout );
3119 else
3120 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003123
3124 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126 }
3127
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003128 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003131 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003134 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003135 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003138 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003139 }
3140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003144 return( ret );
3145 }
3146
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003147 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003148 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003150 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003152 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003153 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003156 return( ret );
3157 }
3158
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003159 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003160 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003162 }
3163
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 if( ret < 0 )
3165 return( ret );
3166
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003167 ssl->in_left = ret;
3168 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003169 MBEDTLS_SSL_TRANSPORT_ELSE
3170#endif /* MBEDTLS_SSL_PROTO_DTLS */
3171#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003174 ssl->in_left, nb_want ) );
3175
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003176 while( ssl->in_left < nb_want )
3177 {
3178 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003179
3180 if( ssl_check_timer( ssl ) != 0 )
3181 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3182 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003183 {
3184 if( ssl->f_recv_timeout != NULL )
3185 {
3186 ret = ssl->f_recv_timeout( ssl->p_bio,
3187 ssl->in_hdr + ssl->in_left, len,
3188 ssl->conf->read_timeout );
3189 }
3190 else
3191 {
3192 ret = ssl->f_recv( ssl->p_bio,
3193 ssl->in_hdr + ssl->in_left, len );
3194 }
3195 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003198 ssl->in_left, nb_want ) );
3199 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003200
3201 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003203
3204 if( ret < 0 )
3205 return( ret );
3206
mohammad160352aecb92018-03-28 23:41:40 -07003207 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003208 {
Darryl Green11999bb2018-03-13 15:22:58 +00003209 MBEDTLS_SSL_DEBUG_MSG( 1,
3210 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003211 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3213 }
3214
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003215 ssl->in_left += ret;
3216 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003218#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
3222 return( 0 );
3223}
3224
3225/*
3226 * Flush any data not yet written
3227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003229{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003230 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003231 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003234
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003235 if( ssl->f_send == NULL )
3236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003238 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003240 }
3241
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003242 /* Avoid incrementing counter if data is flushed */
3243 if( ssl->out_left == 0 )
3244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003246 return( 0 );
3247 }
3248
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 while( ssl->out_left > 0 )
3250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003252 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Hanno Becker2b1e3542018-08-06 11:19:13 +01003254 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003255 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
3259 if( ret <= 0 )
3260 return( ret );
3261
mohammad160352aecb92018-03-28 23:41:40 -07003262 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003263 {
Darryl Green11999bb2018-03-13 15:22:58 +00003264 MBEDTLS_SSL_DEBUG_MSG( 1,
3265 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003266 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3268 }
3269
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 ssl->out_left -= ret;
3271 }
3272
Hanno Becker2b1e3542018-08-06 11:19:13 +01003273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003274 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003275 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003276 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003277 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003278 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003279#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003280#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003281 {
3282 ssl->out_hdr = ssl->out_buf + 8;
3283 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003284#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003285 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
3289 return( 0 );
3290}
3291
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003292/*
3293 * Functions to handle the DTLS retransmission state machine
3294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003296/*
3297 * Append current handshake message to current outgoing flight
3298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3303 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3304 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003305
3306 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003307 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003308 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003311 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003312 }
3313
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003314 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003315 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003318 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319 }
3320
3321 /* Copy current handshake message with headers */
3322 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3323 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003324 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325 msg->next = NULL;
3326
3327 /* Append to the current flight */
3328 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003329 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003330 else
3331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 while( cur->next != NULL )
3334 cur = cur->next;
3335 cur->next = msg;
3336 }
3337
Hanno Becker3b235902018-08-06 09:54:53 +01003338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339 return( 0 );
3340}
3341
3342/*
3343 * Free the current flight of handshake messages
3344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003346{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 mbedtls_ssl_flight_item *cur = flight;
3348 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349
3350 while( cur != NULL )
3351 {
3352 next = cur->next;
3353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 mbedtls_free( cur->p );
3355 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003356
3357 cur = next;
3358 }
3359}
3360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3362static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003363#endif
3364
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003365/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003366 * Swap transform_out and out_ctr with the alternative ones
3367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003371 unsigned char tmp_out_ctr[8];
3372
3373 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003376 return;
3377 }
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003380
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003381 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003382 tmp_transform = ssl->transform_out;
3383 ssl->transform_out = ssl->handshake->alt_transform_out;
3384 ssl->handshake->alt_transform_out = tmp_transform;
3385
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003386 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003387 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3388 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003389 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003390
3391 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003392 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3395 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3400 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003401 }
3402 }
3403#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003404}
3405
3406/*
3407 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003408 */
3409int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3410{
3411 int ret = 0;
3412
3413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3414
3415 ret = mbedtls_ssl_flight_transmit( ssl );
3416
3417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3418
3419 return( ret );
3420}
3421
3422/*
3423 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003424 *
3425 * Need to remember the current message in case flush_output returns
3426 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003427 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003428 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003429int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003430{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003431 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003435 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003437
3438 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003439 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003440 ssl_swap_epochs( ssl );
3441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003443 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003444
3445 while( ssl->handshake->cur_msg != NULL )
3446 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003447 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003448 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003449
Hanno Beckere1dcb032018-08-17 16:47:58 +01003450 int const is_finished =
3451 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3452 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3453
Hanno Becker04da1892018-08-14 13:22:10 +01003454 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3455 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3456
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003457 /* Swap epochs before sending Finished: we can't do it after
3458 * sending ChangeCipherSpec, in case write returns WANT_READ.
3459 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003460 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003461 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003463 ssl_swap_epochs( ssl );
3464 }
3465
Hanno Becker67bc7c32018-08-06 11:33:50 +01003466 ret = ssl_get_remaining_payload_in_datagram( ssl );
3467 if( ret < 0 )
3468 return( ret );
3469 max_frag_len = (size_t) ret;
3470
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003471 /* CCS is copied as is, while HS messages may need fragmentation */
3472 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3473 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003474 if( max_frag_len == 0 )
3475 {
3476 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3477 return( ret );
3478
3479 continue;
3480 }
3481
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003482 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003483 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003484 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003485
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003486 /* Update position inside current message */
3487 ssl->handshake->cur_msg_p += cur->len;
3488 }
3489 else
3490 {
3491 const unsigned char * const p = ssl->handshake->cur_msg_p;
3492 const size_t hs_len = cur->len - 12;
3493 const size_t frag_off = p - ( cur->p + 12 );
3494 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003495 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003496
Hanno Beckere1dcb032018-08-17 16:47:58 +01003497 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003498 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003499 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003500 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003501
Hanno Becker67bc7c32018-08-06 11:33:50 +01003502 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3503 return( ret );
3504
3505 continue;
3506 }
3507 max_hs_frag_len = max_frag_len - 12;
3508
3509 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3510 max_hs_frag_len : rem_len;
3511
3512 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003513 {
3514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003515 (unsigned) cur_hs_frag_len,
3516 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003517 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003518
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003519 /* Messages are stored with handshake headers as if not fragmented,
3520 * copy beginning of headers then fill fragmentation fields.
3521 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3522 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003523
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003524 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3525 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3526 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3527
Hanno Becker67bc7c32018-08-06 11:33:50 +01003528 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3529 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3530 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003531
3532 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3533
Hanno Becker3f7b9732018-08-28 09:53:25 +01003534 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003535 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3536 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003537 ssl->out_msgtype = cur->type;
3538
3539 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003540 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003541 }
3542
3543 /* If done with the current message move to the next one if any */
3544 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3545 {
3546 if( cur->next != NULL )
3547 {
3548 ssl->handshake->cur_msg = cur->next;
3549 ssl->handshake->cur_msg_p = cur->next->p + 12;
3550 }
3551 else
3552 {
3553 ssl->handshake->cur_msg = NULL;
3554 ssl->handshake->cur_msg_p = NULL;
3555 }
3556 }
3557
3558 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003559 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003562 return( ret );
3563 }
3564 }
3565
Hanno Becker67bc7c32018-08-06 11:33:50 +01003566 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3567 return( ret );
3568
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003569 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3571 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003572 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003575 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3576 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003577
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579
3580 return( 0 );
3581}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003582
3583/*
3584 * To be called when the last message of an incoming flight is received.
3585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003587{
3588 /* We won't need to resend that one any more */
3589 ssl_flight_free( ssl->handshake->flight );
3590 ssl->handshake->flight = NULL;
3591 ssl->handshake->cur_msg = NULL;
3592
3593 /* The next incoming flight will start with this msg_seq */
3594 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3595
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003596 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003597 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003598
Hanno Becker0271f962018-08-16 13:23:47 +01003599 /* Clear future message buffering structure. */
3600 ssl_buffering_free( ssl );
3601
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003602 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003603 ssl_set_timer( ssl, 0 );
3604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3606 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003609 }
3610 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003612}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003613
3614/*
3615 * To be called when the last message of an outgoing flight is send.
3616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003618{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003619 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003620 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3623 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003626 }
3627 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003631
Paul Bakker5121ce52009-01-03 21:22:43 +00003632/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003633 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003635
3636/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003637 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003638 *
3639 * - fill in handshake headers
3640 * - update handshake checksum
3641 * - DTLS: save message for resending
3642 * - then pass to the record layer
3643 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003644 * DTLS: except for HelloRequest, messages are only queued, and will only be
3645 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003646 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003647 * Inputs:
3648 * - ssl->out_msglen: 4 + actual handshake message len
3649 * (4 is the size of handshake headers for TLS)
3650 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3651 * - ssl->out_msg + 4: the handshake message body
3652 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003653 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003654 * - ssl->out_msglen: the length of the record contents
3655 * (including handshake headers but excluding record headers)
3656 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003657 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003658int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003659{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003660 int ret;
3661 const size_t hs_len = ssl->out_msglen - 4;
3662 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003663
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3665
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003666 /*
3667 * Sanity checks
3668 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003669 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003670 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3671 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003672 /* In SSLv3, the client might send a NoCertificate alert. */
3673#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3674 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3675 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3676 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3677#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3678 {
3679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3680 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3681 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003683
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003684 /* Whenever we send anything different from a
3685 * HelloRequest we should be in a handshake - double check. */
3686 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3687 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003688 ssl->handshake == NULL )
3689 {
3690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003696 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3700 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003702#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003703
Hanno Beckerb50a2532018-08-06 11:52:54 +01003704 /* Double-check that we did not exceed the bounds
3705 * of the outgoing record buffer.
3706 * This should never fail as the various message
3707 * writing functions must obey the bounds of the
3708 * outgoing record buffer, but better be safe.
3709 *
3710 * Note: We deliberately do not check for the MTU or MFL here.
3711 */
3712 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3713 {
3714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3715 "size %u, maximum %u",
3716 (unsigned) ssl->out_msglen,
3717 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3719 }
3720
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003721 /*
3722 * Fill handshake headers
3723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003725 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003726 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3727 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3728 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003729
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003730 /*
3731 * DTLS has additional fields in the Handshake layer,
3732 * between the length field and the actual payload:
3733 * uint16 message_seq;
3734 * uint24 fragment_offset;
3735 * uint24 fragment_length;
3736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003739 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003740 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003741 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003742 {
3743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3744 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003745 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003746 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3748 }
3749
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003750 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003751 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003752
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003753 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003754 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003755 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003756 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3757 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3758 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003759 }
3760 else
3761 {
3762 ssl->out_msg[4] = 0;
3763 ssl->out_msg[5] = 0;
3764 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003765
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003766 /* Handshake hashes are computed without fragmentation,
3767 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003768 memset( ssl->out_msg + 6, 0x00, 3 );
3769 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003772
Hanno Becker0207e532018-08-28 10:28:28 +01003773 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003774 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3775 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 }
3777
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003778 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003781 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3782 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003783 {
3784 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003787 return( ret );
3788 }
3789 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003790 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003791#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003792 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003793 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003794 {
3795 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3796 return( ret );
3797 }
3798 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003799
3800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3801
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003802 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003803}
3804
3805/*
3806 * Record layer functions
3807 */
3808
3809/*
3810 * Write current record.
3811 *
3812 * Uses:
3813 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3814 * - ssl->out_msglen: length of the record content (excl headers)
3815 * - ssl->out_msg: record content
3816 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003817int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003818{
3819 int ret, done = 0;
3820 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003821 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003822
3823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003826 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003828 {
3829 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003832 return( ret );
3833 }
3834
3835 len = ssl->out_msglen;
3836 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3840 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 ret = mbedtls_ssl_hw_record_write( ssl );
3845 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3848 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003849 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003850
3851 if( ret == 0 )
3852 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003853 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003855 if( !done )
3856 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003857 unsigned i;
3858 size_t protected_record_size;
3859
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003860 /* Skip writing the record content type to after the encryption,
3861 * as it may change when using the CID extension. */
3862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003864 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003865
Hanno Becker19859472018-08-06 09:40:20 +01003866 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003867 ssl->out_len[0] = (unsigned char)( len >> 8 );
3868 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003869
Paul Bakker48916f92012-09-16 19:57:18 +00003870 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003871 {
Hanno Becker3307b532017-12-27 21:37:21 +00003872 mbedtls_record rec;
3873
3874 rec.buf = ssl->out_iv;
3875 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3876 ( ssl->out_iv - ssl->out_buf );
3877 rec.data_len = ssl->out_msglen;
3878 rec.data_offset = ssl->out_msg - rec.buf;
3879
3880 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3881 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3882 ssl->conf->transport, rec.ver );
3883 rec.type = ssl->out_msgtype;
3884
Hanno Beckera5a2b082019-05-15 14:03:01 +01003885#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003886 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003887 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003888#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003889
Hanno Becker611a83b2018-01-03 14:27:32 +00003890 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003891 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003894 return( ret );
3895 }
3896
Hanno Becker3307b532017-12-27 21:37:21 +00003897 if( rec.data_offset != 0 )
3898 {
3899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3901 }
3902
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003903 /* Update the record content type and CID. */
3904 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003905#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003906 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003907#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003908 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003909 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3910 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003911 }
3912
Hanno Becker43395762019-05-03 14:46:38 +01003913 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003914
3915#if defined(MBEDTLS_SSL_PROTO_DTLS)
3916 /* In case of DTLS, double-check that we don't exceed
3917 * the remaining space in the datagram. */
3918 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3919 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003920 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003921 if( ret < 0 )
3922 return( ret );
3923
3924 if( protected_record_size > (size_t) ret )
3925 {
3926 /* Should never happen */
3927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3928 }
3929 }
3930#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003931
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003932 /* Now write the potentially updated record content type. */
3933 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003936 "version = [%d:%d], msglen = %d",
3937 ssl->out_hdr[0], ssl->out_hdr[1],
3938 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003941 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003942
3943 ssl->out_left += protected_record_size;
3944 ssl->out_hdr += protected_record_size;
3945 ssl_update_out_pointers( ssl, ssl->transform_out );
3946
Hanno Becker04484622018-08-06 09:49:38 +01003947 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3948 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3949 break;
3950
3951 /* The loop goes to its end iff the counter is wrapping */
3952 if( i == ssl_ep_len( ssl ) )
3953 {
3954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3955 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003957 }
3958
Hanno Becker67bc7c32018-08-06 11:33:50 +01003959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003960 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3961 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003962 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003963 size_t remaining;
3964 ret = ssl_get_remaining_payload_in_datagram( ssl );
3965 if( ret < 0 )
3966 {
3967 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3968 ret );
3969 return( ret );
3970 }
3971
3972 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003973 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003974 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003975 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003976 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003977 else
3978 {
Hanno Becker513815a2018-08-20 11:56:09 +01003979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003980 }
3981 }
3982#endif /* MBEDTLS_SSL_PROTO_DTLS */
3983
3984 if( ( flush == SSL_FORCE_FLUSH ) &&
3985 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003988 return( ret );
3989 }
3990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003992
3993 return( 0 );
3994}
3995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003997
3998static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3999{
4000 if( ssl->in_msglen < ssl->in_hslen ||
4001 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4002 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4003 {
4004 return( 1 );
4005 }
4006 return( 0 );
4007}
Hanno Becker44650b72018-08-16 12:51:11 +01004008
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004009static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004010{
4011 return( ( ssl->in_msg[9] << 16 ) |
4012 ( ssl->in_msg[10] << 8 ) |
4013 ssl->in_msg[11] );
4014}
4015
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004016static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004017{
4018 return( ( ssl->in_msg[6] << 16 ) |
4019 ( ssl->in_msg[7] << 8 ) |
4020 ssl->in_msg[8] );
4021}
4022
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004023static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004024{
4025 uint32_t msg_len, frag_off, frag_len;
4026
4027 msg_len = ssl_get_hs_total_len( ssl );
4028 frag_off = ssl_get_hs_frag_off( ssl );
4029 frag_len = ssl_get_hs_frag_len( ssl );
4030
4031 if( frag_off > msg_len )
4032 return( -1 );
4033
4034 if( frag_len > msg_len - frag_off )
4035 return( -1 );
4036
4037 if( frag_len + 12 > ssl->in_msglen )
4038 return( -1 );
4039
4040 return( 0 );
4041}
4042
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004043/*
4044 * Mark bits in bitmask (used for DTLS HS reassembly)
4045 */
4046static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4047{
4048 unsigned int start_bits, end_bits;
4049
4050 start_bits = 8 - ( offset % 8 );
4051 if( start_bits != 8 )
4052 {
4053 size_t first_byte_idx = offset / 8;
4054
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004055 /* Special case */
4056 if( len <= start_bits )
4057 {
4058 for( ; len != 0; len-- )
4059 mask[first_byte_idx] |= 1 << ( start_bits - len );
4060
4061 /* Avoid potential issues with offset or len becoming invalid */
4062 return;
4063 }
4064
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004065 offset += start_bits; /* Now offset % 8 == 0 */
4066 len -= start_bits;
4067
4068 for( ; start_bits != 0; start_bits-- )
4069 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4070 }
4071
4072 end_bits = len % 8;
4073 if( end_bits != 0 )
4074 {
4075 size_t last_byte_idx = ( offset + len ) / 8;
4076
4077 len -= end_bits; /* Now len % 8 == 0 */
4078
4079 for( ; end_bits != 0; end_bits-- )
4080 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4081 }
4082
4083 memset( mask + offset / 8, 0xFF, len / 8 );
4084}
4085
4086/*
4087 * Check that bitmask is full
4088 */
4089static int ssl_bitmask_check( unsigned char *mask, size_t len )
4090{
4091 size_t i;
4092
4093 for( i = 0; i < len / 8; i++ )
4094 if( mask[i] != 0xFF )
4095 return( -1 );
4096
4097 for( i = 0; i < len % 8; i++ )
4098 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4099 return( -1 );
4100
4101 return( 0 );
4102}
4103
Hanno Becker56e205e2018-08-16 09:06:12 +01004104/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004105static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004106 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004107{
Hanno Becker56e205e2018-08-16 09:06:12 +01004108 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004109
Hanno Becker56e205e2018-08-16 09:06:12 +01004110 alloc_len = 12; /* Handshake header */
4111 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004112
Hanno Beckerd07df862018-08-16 09:14:58 +01004113 if( add_bitmap )
4114 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004115
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004116 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004117}
Hanno Becker56e205e2018-08-16 09:06:12 +01004118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004120
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004121static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004122{
4123 return( ( ssl->in_msg[1] << 16 ) |
4124 ( ssl->in_msg[2] << 8 ) |
4125 ssl->in_msg[3] );
4126}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004127
Simon Butcher99000142016-10-13 17:21:01 +01004128int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004133 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004134 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004135 }
4136
Hanno Becker12555c62018-08-16 12:47:53 +01004137 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004140 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004141 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004144 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004145 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004146 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004147 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004148
Hanno Becker44650b72018-08-16 12:51:11 +01004149 if( ssl_check_hs_header( ssl ) != 0 )
4150 {
4151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4152 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4153 }
4154
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004155 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004156 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4157 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4158 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4159 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004160 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004161 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4162 {
4163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4164 recv_msg_seq,
4165 ssl->handshake->in_msg_seq ) );
4166 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4167 }
4168
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004169 /* Retransmit only on last message from previous flight, to avoid
4170 * too many retransmissions.
4171 * Besides, No sane server ever retransmits HelloVerifyRequest */
4172 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004176 "message_seq = %d, start_of_flight = %d",
4177 recv_msg_seq,
4178 ssl->handshake->in_flight_start_seq ) );
4179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004183 return( ret );
4184 }
4185 }
4186 else
4187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004189 "message_seq = %d, expected = %d",
4190 recv_msg_seq,
4191 ssl->handshake->in_msg_seq ) );
4192 }
4193
Hanno Becker90333da2017-10-10 11:27:13 +01004194 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004195 }
4196 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004197
Hanno Becker6d97ef52018-08-16 13:09:04 +01004198 /* Message reassembly is handled alongside buffering of future
4199 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004200 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004201 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004202 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004205 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004206 }
4207 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004208 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004210#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004211 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004212 /* With TLS we don't handle fragmentation (for now) */
4213 if( ssl->in_msglen < ssl->in_hslen )
4214 {
4215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4216 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4217 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004218 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004219#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004220
Simon Butcher99000142016-10-13 17:21:01 +01004221 return( 0 );
4222}
4223
4224void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4225{
Hanno Becker0271f962018-08-16 13:23:47 +01004226 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004227
Hanno Becker0271f962018-08-16 13:23:47 +01004228 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004229 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004230 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004231 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004232
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004233 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004235 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004236 ssl->handshake != NULL )
4237 {
Hanno Becker0271f962018-08-16 13:23:47 +01004238 unsigned offset;
4239 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004240
Hanno Becker0271f962018-08-16 13:23:47 +01004241 /* Increment handshake sequence number */
4242 hs->in_msg_seq++;
4243
4244 /*
4245 * Clear up handshake buffering and reassembly structure.
4246 */
4247
4248 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004249 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004250
4251 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004252 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4253 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004254 offset++, hs_buf++ )
4255 {
4256 *hs_buf = *(hs_buf + 1);
4257 }
4258
4259 /* Create a fresh last entry */
4260 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004261 }
4262#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004263}
4264
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004265/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004266 * DTLS anti-replay: RFC 6347 4.1.2.6
4267 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004268 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4269 * Bit n is set iff record number in_window_top - n has been seen.
4270 *
4271 * Usually, in_window_top is the last record number seen and the lsb of
4272 * in_window is set. The only exception is the initial state (record number 0
4273 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4276static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004277{
4278 ssl->in_window_top = 0;
4279 ssl->in_window = 0;
4280}
4281
4282static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4283{
4284 return( ( (uint64_t) buf[0] << 40 ) |
4285 ( (uint64_t) buf[1] << 32 ) |
4286 ( (uint64_t) buf[2] << 24 ) |
4287 ( (uint64_t) buf[3] << 16 ) |
4288 ( (uint64_t) buf[4] << 8 ) |
4289 ( (uint64_t) buf[5] ) );
4290}
4291
4292/*
4293 * Return 0 if sequence number is acceptable, -1 otherwise
4294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004296{
4297 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4298 uint64_t bit;
4299
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004300 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004301 return( 0 );
4302
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004303 if( rec_seqnum > ssl->in_window_top )
4304 return( 0 );
4305
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004306 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004307
4308 if( bit >= 64 )
4309 return( -1 );
4310
4311 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4312 return( -1 );
4313
4314 return( 0 );
4315}
4316
4317/*
4318 * Update replay window on new validated record
4319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321{
4322 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4323
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004324 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004325 return;
4326
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004327 if( rec_seqnum > ssl->in_window_top )
4328 {
4329 /* Update window_top and the contents of the window */
4330 uint64_t shift = rec_seqnum - ssl->in_window_top;
4331
4332 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004333 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004334 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004335 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004336 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004337 ssl->in_window |= 1;
4338 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339
4340 ssl->in_window_top = rec_seqnum;
4341 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004342 else
4343 {
4344 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004345 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004346
4347 if( bit < 64 ) /* Always true, but be extra sure */
4348 ssl->in_window |= (uint64_t) 1 << bit;
4349 }
4350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004352
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004353#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004354/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004355static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4356
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004357/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004358 * Without any SSL context, check if a datagram looks like a ClientHello with
4359 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004360 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004361 *
4362 * - if cookie is valid, return 0
4363 * - if ClientHello looks superficially valid but cookie is not,
4364 * fill obuf and set olen, then
4365 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4366 * - otherwise return a specific error code
4367 */
4368static int ssl_check_dtls_clihlo_cookie(
4369 mbedtls_ssl_cookie_write_t *f_cookie_write,
4370 mbedtls_ssl_cookie_check_t *f_cookie_check,
4371 void *p_cookie,
4372 const unsigned char *cli_id, size_t cli_id_len,
4373 const unsigned char *in, size_t in_len,
4374 unsigned char *obuf, size_t buf_len, size_t *olen )
4375{
4376 size_t sid_len, cookie_len;
4377 unsigned char *p;
4378
4379 if( f_cookie_write == NULL || f_cookie_check == NULL )
4380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4381
4382 /*
4383 * Structure of ClientHello with record and handshake headers,
4384 * and expected values. We don't need to check a lot, more checks will be
4385 * done when actually parsing the ClientHello - skipping those checks
4386 * avoids code duplication and does not make cookie forging any easier.
4387 *
4388 * 0-0 ContentType type; copied, must be handshake
4389 * 1-2 ProtocolVersion version; copied
4390 * 3-4 uint16 epoch; copied, must be 0
4391 * 5-10 uint48 sequence_number; copied
4392 * 11-12 uint16 length; (ignored)
4393 *
4394 * 13-13 HandshakeType msg_type; (ignored)
4395 * 14-16 uint24 length; (ignored)
4396 * 17-18 uint16 message_seq; copied
4397 * 19-21 uint24 fragment_offset; copied, must be 0
4398 * 22-24 uint24 fragment_length; (ignored)
4399 *
4400 * 25-26 ProtocolVersion client_version; (ignored)
4401 * 27-58 Random random; (ignored)
4402 * 59-xx SessionID session_id; 1 byte len + sid_len content
4403 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4404 * ...
4405 *
4406 * Minimum length is 61 bytes.
4407 */
4408 if( in_len < 61 ||
4409 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4410 in[3] != 0 || in[4] != 0 ||
4411 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4412 {
4413 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4414 }
4415
4416 sid_len = in[59];
4417 if( sid_len > in_len - 61 )
4418 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4419
4420 cookie_len = in[60 + sid_len];
4421 if( cookie_len > in_len - 60 )
4422 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4423
4424 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4425 cli_id, cli_id_len ) == 0 )
4426 {
4427 /* Valid cookie */
4428 return( 0 );
4429 }
4430
4431 /*
4432 * If we get here, we've got an invalid cookie, let's prepare HVR.
4433 *
4434 * 0-0 ContentType type; copied
4435 * 1-2 ProtocolVersion version; copied
4436 * 3-4 uint16 epoch; copied
4437 * 5-10 uint48 sequence_number; copied
4438 * 11-12 uint16 length; olen - 13
4439 *
4440 * 13-13 HandshakeType msg_type; hello_verify_request
4441 * 14-16 uint24 length; olen - 25
4442 * 17-18 uint16 message_seq; copied
4443 * 19-21 uint24 fragment_offset; copied
4444 * 22-24 uint24 fragment_length; olen - 25
4445 *
4446 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4447 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4448 *
4449 * Minimum length is 28.
4450 */
4451 if( buf_len < 28 )
4452 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4453
4454 /* Copy most fields and adapt others */
4455 memcpy( obuf, in, 25 );
4456 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4457 obuf[25] = 0xfe;
4458 obuf[26] = 0xff;
4459
4460 /* Generate and write actual cookie */
4461 p = obuf + 28;
4462 if( f_cookie_write( p_cookie,
4463 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4464 {
4465 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4466 }
4467
4468 *olen = p - obuf;
4469
4470 /* Go back and fill length fields */
4471 obuf[27] = (unsigned char)( *olen - 28 );
4472
4473 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4474 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4475 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4476
4477 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4478 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4479
4480 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4481}
4482
4483/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004484 * Handle possible client reconnect with the same UDP quadruplet
4485 * (RFC 6347 Section 4.2.8).
4486 *
4487 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4488 * that looks like a ClientHello.
4489 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004490 * - if the input looks like a ClientHello without cookies,
4491 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004492 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004493 * - if the input looks like a ClientHello with a valid cookie,
4494 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004495 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004496 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004497 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004498 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004499 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4500 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004501 */
4502static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4503{
4504 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004505 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004506
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004507 ret = ssl_check_dtls_clihlo_cookie(
4508 ssl->conf->f_cookie_write,
4509 ssl->conf->f_cookie_check,
4510 ssl->conf->p_cookie,
4511 ssl->cli_id, ssl->cli_id_len,
4512 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004513 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004514
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004515 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4516
4517 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004518 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004519 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004520 * If the error is permanent we'll catch it later,
4521 * if it's not, then hopefully it'll work next time. */
4522 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4523
4524 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004525 }
4526
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004527 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004528 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004529 /* Got a valid cookie, partially reset context */
4530 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4531 {
4532 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4533 return( ret );
4534 }
4535
4536 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004537 }
4538
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004539 return( ret );
4540}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004541#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004542
Hanno Becker46483f12019-05-03 13:25:54 +01004543static int ssl_check_record_type( uint8_t record_type )
4544{
4545 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4546 record_type != MBEDTLS_SSL_MSG_ALERT &&
4547 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4548 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4549 {
4550 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4551 }
4552
4553 return( 0 );
4554}
4555
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004556/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004557 * ContentType type;
4558 * ProtocolVersion version;
4559 * uint16 epoch; // DTLS only
4560 * uint48 sequence_number; // DTLS only
4561 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004562 *
4563 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004564 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004565 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4566 *
4567 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004568 * 1. proceed with the record if this function returns 0
4569 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4570 * 3. return CLIENT_RECONNECT if this function return that value
4571 * 4. drop the whole datagram if this function returns anything else.
4572 * Point 2 is needed when the peer is resending, and we have already received
4573 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004577 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004578 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004579
Hanno Becker8b09b732019-05-08 12:03:28 +01004580 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004581
Paul Bakker5121ce52009-01-03 21:22:43 +00004582 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004583 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004584
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004585 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004586#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b09b732019-05-08 12:03:28 +01004587 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4588 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4589 ssl->conf->cid_len != 0 )
4590 {
4591 /* Shift pointers to account for record header including CID
4592 * struct {
4593 * ContentType special_type = tls12_cid;
4594 * ProtocolVersion version;
4595 * uint16 epoch;
4596 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004597 * opaque cid[cid_length]; // Additional field compared to
4598 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004599 * uint16 length;
4600 * opaque enc_content[DTLSCiphertext.length];
4601 * } DTLSCiphertext;
4602 */
4603
4604 /* So far, we only support static CID lengths
4605 * fixed in the configuration. */
4606 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4607 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4608 }
4609 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004610#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004611 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004614
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004615#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004616 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004617 * Section 4.1.2.7, that is, send alert only with TLS */
4618 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4619 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004620 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4621 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004622 }
4623#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004626 }
4627
4628 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004629 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4632 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 }
4634
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004635 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4638 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 }
4640
Hanno Becker8b09b732019-05-08 12:03:28 +01004641 /* Now that the total length of the record header is known, ensure
4642 * that the current datagram is large enough to hold it.
4643 * This would fail, for example, if we received a datagram of
4644 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4645 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4646 if( ret != 0 )
4647 {
4648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4649 return( ret );
4650 }
4651 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4652
4653 /* Parse and validate record length
4654 * This must happen after the CID parsing because
4655 * its position in the record header depends on
4656 * the presence of a CID. */
4657
4658 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004659 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004660 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4663 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004664 }
4665
Hanno Becker8b09b732019-05-08 12:03:28 +01004666 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004667 "version = [%d:%d], msglen = %d",
4668 ssl->in_msgtype,
4669 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004670
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004671 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004672 * DTLS-related tests.
4673 * Check epoch before checking length constraint because
4674 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4675 * message gets duplicated before the corresponding Finished message,
4676 * the second ChangeCipherSpec should be discarded because it belongs
4677 * to an old epoch, but not because its length is shorter than
4678 * the minimum record length for packets using the new record transform.
4679 * Note that these two kinds of failures are handled differently,
4680 * as an unexpected record is silently skipped but an invalid
4681 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004682 */
4683#if defined(MBEDTLS_SSL_PROTO_DTLS)
4684 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4685 {
4686 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4687
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004688 /* Check epoch (and sequence number) with DTLS */
4689 if( rec_epoch != ssl->in_epoch )
4690 {
4691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4692 "expected %d, received %d",
4693 ssl->in_epoch, rec_epoch ) );
4694
4695#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4696 /*
4697 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4698 * access the first byte of record content (handshake type), as we
4699 * have an active transform (possibly iv_len != 0), so use the
4700 * fact that the record header len is 13 instead.
4701 */
4702 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4703 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4704 rec_epoch == 0 &&
4705 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4706 ssl->in_left > 13 &&
4707 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4708 {
4709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4710 "from the same port" ) );
4711 return( ssl_handle_possible_reconnect( ssl ) );
4712 }
4713 else
4714#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004715 {
4716 /* Consider buffering the record. */
4717 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4718 {
4719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4720 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4721 }
4722
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004723 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004724 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004725 }
4726
4727#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4728 /* Replay detection only works for the current epoch */
4729 if( rec_epoch == ssl->in_epoch &&
4730 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4731 {
4732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4733 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4734 }
4735#endif
4736 }
4737#endif /* MBEDTLS_SSL_PROTO_DTLS */
4738
Hanno Becker52c6dc62017-05-26 16:07:36 +01004739
4740 /* Check length against bounds of the current transform and version */
4741 if( ssl->transform_in == NULL )
4742 {
4743 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004744 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004745 {
4746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4747 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4748 }
4749 }
4750 else
4751 {
4752 if( ssl->in_msglen < ssl->transform_in->minlen )
4753 {
4754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4755 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4756 }
4757
4758#if defined(MBEDTLS_SSL_PROTO_SSL3)
4759 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004760 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004761 {
4762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4763 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4764 }
4765#endif
4766#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4767 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4768 /*
4769 * TLS encrypted messages can have up to 256 bytes of padding
4770 */
4771 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4772 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004773 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004774 {
4775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4776 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4777 }
4778#endif
4779 }
4780
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004781 return( 0 );
4782}
Paul Bakker5121ce52009-01-03 21:22:43 +00004783
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004784/*
4785 * If applicable, decrypt (and decompress) record content
4786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004788{
4789 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004792 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4795 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 ret = mbedtls_ssl_hw_record_read( ssl );
4800 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4803 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004804 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004805
4806 if( ret == 0 )
4807 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004808 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004810 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004811 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004812 mbedtls_record rec;
4813
4814 rec.buf = ssl->in_iv;
4815 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4816 - ( ssl->in_iv - ssl->in_buf );
4817 rec.data_len = ssl->in_msglen;
4818 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004819#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004820 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004821 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004822#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004823
4824 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4825 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4826 ssl->conf->transport, rec.ver );
4827 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004828 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4829 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004832
Hanno Beckera5a2b082019-05-15 14:03:01 +01004833#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004834 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4835 ssl->conf->ignore_unexpected_cid
4836 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4837 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004838 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004839 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004840 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004841#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004842
Paul Bakker5121ce52009-01-03 21:22:43 +00004843 return( ret );
4844 }
4845
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004846 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004847 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004848 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4849 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004851
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004852 /* The record content type may change during decryption,
4853 * so re-read it. */
4854 ssl->in_msgtype = rec.type;
4855 /* Also update the input buffer, because unfortunately
4856 * the server-side ssl_parse_client_hello() reparses the
4857 * record header when receiving a ClientHello initiating
4858 * a renegotiation. */
4859 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004860 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004861 ssl->in_msglen = rec.data_len;
4862 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4863 ssl->in_len[1] = (unsigned char)( rec.data_len );
4864
Paul Bakker5121ce52009-01-03 21:22:43 +00004865 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4866 ssl->in_msg, ssl->in_msglen );
4867
Hanno Beckera5a2b082019-05-15 14:03:01 +01004868#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004869 /* We have already checked the record content type
4870 * in ssl_parse_record_header(), failing or silently
4871 * dropping the record in the case of an unknown type.
4872 *
4873 * Since with the use of CIDs, the record content type
4874 * might change during decryption, re-check the record
4875 * content type, but treat a failure as fatal this time. */
4876 if( ssl_check_record_type( ssl->in_msgtype ) )
4877 {
4878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4879 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4880 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004881#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004882
Angus Grattond8213d02016-05-25 20:56:48 +10004883 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4886 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004887 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004888 else if( ssl->in_msglen == 0 )
4889 {
4890#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4891 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4892 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4893 {
4894 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4896 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4897 }
4898#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4899
4900 ssl->nb_zero++;
4901
4902 /*
4903 * Three or more empty messages may be a DoS attack
4904 * (excessive CPU consumption).
4905 */
4906 if( ssl->nb_zero > 3 )
4907 {
4908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004909 "messages, possible DoS attack" ) );
4910 /* Treat the records as if they were not properly authenticated,
4911 * thereby failing the connection if we see more than allowed
4912 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004913 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4914 }
4915 }
4916 else
4917 ssl->nb_zero = 0;
4918
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004919 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4920#if defined(MBEDTLS_SSL_PROTO_TLS)
4921 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004922 {
4923 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004924 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004925 if( ++ssl->in_ctr[i - 1] != 0 )
4926 break;
4927
4928 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004929 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004930 {
4931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4932 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4933 }
4934 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004935#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004936
Paul Bakker5121ce52009-01-03 21:22:43 +00004937 }
4938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004940 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004942 {
4943 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004946 return( ret );
4947 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004948 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004952 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004955 }
4956#endif
4957
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004958 return( 0 );
4959}
4960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004962
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004963/*
4964 * Read a record.
4965 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004966 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4967 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4968 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004969 */
Hanno Becker1097b342018-08-15 14:09:41 +01004970
4971/* Helper functions for mbedtls_ssl_read_record(). */
4972static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004973static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4974static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004975
Hanno Becker327c93b2018-08-15 13:56:18 +01004976int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004977 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004978{
4979 int ret;
4980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004982
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004983 if( ssl->keep_current_message == 0 )
4984 {
4985 do {
Simon Butcher99000142016-10-13 17:21:01 +01004986
Hanno Becker26994592018-08-15 14:14:59 +01004987 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004988 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004989 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004990
Hanno Beckere74d5562018-08-15 14:26:08 +01004991 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004992 {
Hanno Becker40f50842018-08-15 14:48:01 +01004993#if defined(MBEDTLS_SSL_PROTO_DTLS)
4994 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004995
Hanno Becker40f50842018-08-15 14:48:01 +01004996 /* We only check for buffered messages if the
4997 * current datagram is fully consumed. */
4998 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004999 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005000 {
Hanno Becker40f50842018-08-15 14:48:01 +01005001 if( ssl_load_buffered_message( ssl ) == 0 )
5002 have_buffered = 1;
5003 }
5004
5005 if( have_buffered == 0 )
5006#endif /* MBEDTLS_SSL_PROTO_DTLS */
5007 {
5008 ret = ssl_get_next_record( ssl );
5009 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5010 continue;
5011
5012 if( ret != 0 )
5013 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005014 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005015 return( ret );
5016 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005017 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005018 }
5019
5020 ret = mbedtls_ssl_handle_message_type( ssl );
5021
Hanno Becker40f50842018-08-15 14:48:01 +01005022#if defined(MBEDTLS_SSL_PROTO_DTLS)
5023 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5024 {
5025 /* Buffer future message */
5026 ret = ssl_buffer_message( ssl );
5027 if( ret != 0 )
5028 return( ret );
5029
5030 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5031 }
5032#endif /* MBEDTLS_SSL_PROTO_DTLS */
5033
Hanno Becker90333da2017-10-10 11:27:13 +01005034 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5035 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005036
5037 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005038 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005039 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005040 return( ret );
5041 }
5042
Hanno Becker327c93b2018-08-15 13:56:18 +01005043 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005044 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005045 {
5046 mbedtls_ssl_update_handshake_status( ssl );
5047 }
Simon Butcher99000142016-10-13 17:21:01 +01005048 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005049 else
Simon Butcher99000142016-10-13 17:21:01 +01005050 {
Hanno Becker02f59072018-08-15 14:00:24 +01005051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005052 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005053 }
5054
5055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5056
5057 return( 0 );
5058}
5059
Hanno Becker40f50842018-08-15 14:48:01 +01005060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005061static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005062{
Hanno Becker40f50842018-08-15 14:48:01 +01005063 if( ssl->in_left > ssl->next_record_offset )
5064 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005065
Hanno Becker40f50842018-08-15 14:48:01 +01005066 return( 0 );
5067}
5068
5069static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5070{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005071 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005072 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005073 int ret = 0;
5074
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005075 if( hs == NULL )
5076 return( -1 );
5077
Hanno Beckere00ae372018-08-20 09:39:42 +01005078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5079
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005080 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5081 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5082 {
5083 /* Check if we have seen a ChangeCipherSpec before.
5084 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005085 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005086 {
5087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5088 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005089 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005090 }
5091
Hanno Becker39b8bc92018-08-28 17:17:13 +01005092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5094 ssl->in_msglen = 1;
5095 ssl->in_msg[0] = 1;
5096
5097 /* As long as they are equal, the exact value doesn't matter. */
5098 ssl->in_left = 0;
5099 ssl->next_record_offset = 0;
5100
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005101 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005102 goto exit;
5103 }
Hanno Becker37f95322018-08-16 13:55:32 +01005104
Hanno Beckerb8f50142018-08-28 10:01:34 +01005105#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005106 /* Debug only */
5107 {
5108 unsigned offset;
5109 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5110 {
5111 hs_buf = &hs->buffering.hs[offset];
5112 if( hs_buf->is_valid == 1 )
5113 {
5114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5115 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005116 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005117 }
5118 }
5119 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005120#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005121
5122 /* Check if we have buffered and/or fully reassembled the
5123 * next handshake message. */
5124 hs_buf = &hs->buffering.hs[0];
5125 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5126 {
5127 /* Synthesize a record containing the buffered HS message. */
5128 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5129 ( hs_buf->data[2] << 8 ) |
5130 hs_buf->data[3];
5131
5132 /* Double-check that we haven't accidentally buffered
5133 * a message that doesn't fit into the input buffer. */
5134 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5135 {
5136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5138 }
5139
5140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5141 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5142 hs_buf->data, msg_len + 12 );
5143
5144 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5145 ssl->in_hslen = msg_len + 12;
5146 ssl->in_msglen = msg_len + 12;
5147 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5148
5149 ret = 0;
5150 goto exit;
5151 }
5152 else
5153 {
5154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5155 hs->in_msg_seq ) );
5156 }
5157
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005158 ret = -1;
5159
5160exit:
5161
5162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5163 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005164}
5165
Hanno Beckera02b0b42018-08-21 17:20:27 +01005166static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5167 size_t desired )
5168{
5169 int offset;
5170 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5172 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005173
Hanno Becker01315ea2018-08-21 17:22:17 +01005174 /* Get rid of future records epoch first, if such exist. */
5175 ssl_free_buffered_record( ssl );
5176
5177 /* Check if we have enough space available now. */
5178 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5179 hs->buffering.total_bytes_buffered ) )
5180 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005182 return( 0 );
5183 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005184
Hanno Becker4f432ad2018-08-28 10:02:32 +01005185 /* We don't have enough space to buffer the next expected handshake
5186 * message. Remove buffers used for future messages to gain space,
5187 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005188 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5189 offset >= 0; offset-- )
5190 {
5191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5192 offset ) );
5193
Hanno Beckerb309b922018-08-23 13:18:05 +01005194 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005195
5196 /* Check if we have enough space available now. */
5197 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5198 hs->buffering.total_bytes_buffered ) )
5199 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005201 return( 0 );
5202 }
5203 }
5204
5205 return( -1 );
5206}
5207
Hanno Becker40f50842018-08-15 14:48:01 +01005208static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5209{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005210 int ret = 0;
5211 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5212
5213 if( hs == NULL )
5214 return( 0 );
5215
5216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5217
5218 switch( ssl->in_msgtype )
5219 {
5220 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005222
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005223 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005224 break;
5225
5226 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005227 {
5228 unsigned recv_msg_seq_offset;
5229 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5230 mbedtls_ssl_hs_buffer *hs_buf;
5231 size_t msg_len = ssl->in_hslen - 12;
5232
5233 /* We should never receive an old handshake
5234 * message - double-check nonetheless. */
5235 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5236 {
5237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5239 }
5240
5241 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5242 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5243 {
5244 /* Silently ignore -- message too far in the future */
5245 MBEDTLS_SSL_DEBUG_MSG( 2,
5246 ( "Ignore future HS message with sequence number %u, "
5247 "buffering window %u - %u",
5248 recv_msg_seq, ssl->handshake->in_msg_seq,
5249 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5250
5251 goto exit;
5252 }
5253
5254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5255 recv_msg_seq, recv_msg_seq_offset ) );
5256
5257 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5258
5259 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005260 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005261 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005262 size_t reassembly_buf_sz;
5263
Hanno Becker37f95322018-08-16 13:55:32 +01005264 hs_buf->is_fragmented =
5265 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5266
5267 /* We copy the message back into the input buffer
5268 * after reassembly, so check that it's not too large.
5269 * This is an implementation-specific limitation
5270 * and not one from the standard, hence it is not
5271 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005272 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005273 {
5274 /* Ignore message */
5275 goto exit;
5276 }
5277
Hanno Beckere0b150f2018-08-21 15:51:03 +01005278 /* Check if we have enough space to buffer the message. */
5279 if( hs->buffering.total_bytes_buffered >
5280 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5281 {
5282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5284 }
5285
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005286 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5287 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005288
5289 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5290 hs->buffering.total_bytes_buffered ) )
5291 {
5292 if( recv_msg_seq_offset > 0 )
5293 {
5294 /* If we can't buffer a future message because
5295 * of space limitations -- ignore. */
5296 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",
5297 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5298 (unsigned) hs->buffering.total_bytes_buffered ) );
5299 goto exit;
5300 }
Hanno Beckere1801392018-08-21 16:51:05 +01005301 else
5302 {
5303 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",
5304 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5305 (unsigned) hs->buffering.total_bytes_buffered ) );
5306 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005307
Hanno Beckera02b0b42018-08-21 17:20:27 +01005308 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005309 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005310 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",
5311 (unsigned) msg_len,
5312 (unsigned) reassembly_buf_sz,
5313 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005314 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005315 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5316 goto exit;
5317 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005318 }
5319
5320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5321 msg_len ) );
5322
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005323 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5324 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005325 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005326 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005327 goto exit;
5328 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005329 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005330
5331 /* Prepare final header: copy msg_type, length and message_seq,
5332 * then add standardised fragment_offset and fragment_length */
5333 memcpy( hs_buf->data, ssl->in_msg, 6 );
5334 memset( hs_buf->data + 6, 0, 3 );
5335 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5336
5337 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005338
5339 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005340 }
5341 else
5342 {
5343 /* Make sure msg_type and length are consistent */
5344 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5345 {
5346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5347 /* Ignore */
5348 goto exit;
5349 }
5350 }
5351
Hanno Becker4422bbb2018-08-20 09:40:19 +01005352 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005353 {
5354 size_t frag_len, frag_off;
5355 unsigned char * const msg = hs_buf->data + 12;
5356
5357 /*
5358 * Check and copy current fragment
5359 */
5360
5361 /* Validation of header fields already done in
5362 * mbedtls_ssl_prepare_handshake_record(). */
5363 frag_off = ssl_get_hs_frag_off( ssl );
5364 frag_len = ssl_get_hs_frag_len( ssl );
5365
5366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5367 frag_off, frag_len ) );
5368 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5369
5370 if( hs_buf->is_fragmented )
5371 {
5372 unsigned char * const bitmask = msg + msg_len;
5373 ssl_bitmask_set( bitmask, frag_off, frag_len );
5374 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5375 msg_len ) == 0 );
5376 }
5377 else
5378 {
5379 hs_buf->is_complete = 1;
5380 }
5381
5382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5383 hs_buf->is_complete ? "" : "not yet " ) );
5384 }
5385
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005386 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005387 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005388
5389 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005390 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005391 break;
5392 }
5393
5394exit:
5395
5396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5397 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005398}
5399#endif /* MBEDTLS_SSL_PROTO_DTLS */
5400
Hanno Becker1097b342018-08-15 14:09:41 +01005401static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005402{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005403 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005404 * Consume last content-layer message and potentially
5405 * update in_msglen which keeps track of the contents'
5406 * consumption state.
5407 *
5408 * (1) Handshake messages:
5409 * Remove last handshake message, move content
5410 * and adapt in_msglen.
5411 *
5412 * (2) Alert messages:
5413 * Consume whole record content, in_msglen = 0.
5414 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005415 * (3) Change cipher spec:
5416 * Consume whole record content, in_msglen = 0.
5417 *
5418 * (4) Application data:
5419 * Don't do anything - the record layer provides
5420 * the application data as a stream transport
5421 * and consumes through mbedtls_ssl_read only.
5422 *
5423 */
5424
5425 /* Case (1): Handshake messages */
5426 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005427 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005428 /* Hard assertion to be sure that no application data
5429 * is in flight, as corrupting ssl->in_msglen during
5430 * ssl->in_offt != NULL is fatal. */
5431 if( ssl->in_offt != NULL )
5432 {
5433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5435 }
5436
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005437 /*
5438 * Get next Handshake message in the current record
5439 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005440
Hanno Becker4a810fb2017-05-24 16:27:30 +01005441 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005442 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005443 * current handshake content: If DTLS handshake
5444 * fragmentation is used, that's the fragment
5445 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005446 * size here is faulty and should be changed at
5447 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005448 * (2) While it doesn't seem to cause problems, one
5449 * has to be very careful not to assume that in_hslen
5450 * is always <= in_msglen in a sensible communication.
5451 * Again, it's wrong for DTLS handshake fragmentation.
5452 * The following check is therefore mandatory, and
5453 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005454 * Additionally, ssl->in_hslen might be arbitrarily out of
5455 * bounds after handling a DTLS message with an unexpected
5456 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005457 */
5458 if( ssl->in_hslen < ssl->in_msglen )
5459 {
5460 ssl->in_msglen -= ssl->in_hslen;
5461 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5462 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005463
Hanno Becker4a810fb2017-05-24 16:27:30 +01005464 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5465 ssl->in_msg, ssl->in_msglen );
5466 }
5467 else
5468 {
5469 ssl->in_msglen = 0;
5470 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005471
Hanno Becker4a810fb2017-05-24 16:27:30 +01005472 ssl->in_hslen = 0;
5473 }
5474 /* Case (4): Application data */
5475 else if( ssl->in_offt != NULL )
5476 {
5477 return( 0 );
5478 }
5479 /* Everything else (CCS & Alerts) */
5480 else
5481 {
5482 ssl->in_msglen = 0;
5483 }
5484
Hanno Becker1097b342018-08-15 14:09:41 +01005485 return( 0 );
5486}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005487
Hanno Beckere74d5562018-08-15 14:26:08 +01005488static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5489{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005490 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005491 return( 1 );
5492
5493 return( 0 );
5494}
5495
Hanno Becker5f066e72018-08-16 14:56:31 +01005496#if defined(MBEDTLS_SSL_PROTO_DTLS)
5497
5498static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5499{
5500 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5501 if( hs == NULL )
5502 return;
5503
Hanno Becker01315ea2018-08-21 17:22:17 +01005504 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005505 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005506 hs->buffering.total_bytes_buffered -=
5507 hs->buffering.future_record.len;
5508
5509 mbedtls_free( hs->buffering.future_record.data );
5510 hs->buffering.future_record.data = NULL;
5511 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005512}
5513
5514static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5515{
5516 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5517 unsigned char * rec;
5518 size_t rec_len;
5519 unsigned rec_epoch;
5520
5521 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5522 return( 0 );
5523
5524 if( hs == NULL )
5525 return( 0 );
5526
Hanno Becker5f066e72018-08-16 14:56:31 +01005527 rec = hs->buffering.future_record.data;
5528 rec_len = hs->buffering.future_record.len;
5529 rec_epoch = hs->buffering.future_record.epoch;
5530
5531 if( rec == NULL )
5532 return( 0 );
5533
Hanno Becker4cb782d2018-08-20 11:19:05 +01005534 /* Only consider loading future records if the
5535 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005536 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005537 return( 0 );
5538
Hanno Becker5f066e72018-08-16 14:56:31 +01005539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5540
5541 if( rec_epoch != ssl->in_epoch )
5542 {
5543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5544 goto exit;
5545 }
5546
5547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5548
5549 /* Double-check that the record is not too large */
5550 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5551 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5552 {
5553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5555 }
5556
5557 memcpy( ssl->in_hdr, rec, rec_len );
5558 ssl->in_left = rec_len;
5559 ssl->next_record_offset = 0;
5560
5561 ssl_free_buffered_record( ssl );
5562
5563exit:
5564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5565 return( 0 );
5566}
5567
5568static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5569{
5570 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5571 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005572 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005573
5574 /* Don't buffer future records outside handshakes. */
5575 if( hs == NULL )
5576 return( 0 );
5577
5578 /* Only buffer handshake records (we are only interested
5579 * in Finished messages). */
5580 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5581 return( 0 );
5582
5583 /* Don't buffer more than one future epoch record. */
5584 if( hs->buffering.future_record.data != NULL )
5585 return( 0 );
5586
Hanno Becker01315ea2018-08-21 17:22:17 +01005587 /* Don't buffer record if there's not enough buffering space remaining. */
5588 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5589 hs->buffering.total_bytes_buffered ) )
5590 {
5591 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",
5592 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5593 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005594 return( 0 );
5595 }
5596
Hanno Becker5f066e72018-08-16 14:56:31 +01005597 /* Buffer record */
5598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5599 ssl->in_epoch + 1 ) );
5600 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5601 rec_hdr_len + ssl->in_msglen );
5602
5603 /* ssl_parse_record_header() only considers records
5604 * of the next epoch as candidates for buffering. */
5605 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005606 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005607
5608 hs->buffering.future_record.data =
5609 mbedtls_calloc( 1, hs->buffering.future_record.len );
5610 if( hs->buffering.future_record.data == NULL )
5611 {
5612 /* If we run out of RAM trying to buffer a
5613 * record from the next epoch, just ignore. */
5614 return( 0 );
5615 }
5616
Hanno Becker01315ea2018-08-21 17:22:17 +01005617 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005618
Hanno Becker01315ea2018-08-21 17:22:17 +01005619 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005620 return( 0 );
5621}
5622
5623#endif /* MBEDTLS_SSL_PROTO_DTLS */
5624
Hanno Beckere74d5562018-08-15 14:26:08 +01005625static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005626{
5627 int ret;
5628
Hanno Becker5f066e72018-08-16 14:56:31 +01005629#if defined(MBEDTLS_SSL_PROTO_DTLS)
5630 /* We might have buffered a future record; if so,
5631 * and if the epoch matches now, load it.
5632 * On success, this call will set ssl->in_left to
5633 * the length of the buffered record, so that
5634 * the calls to ssl_fetch_input() below will
5635 * essentially be no-ops. */
5636 ret = ssl_load_buffered_record( ssl );
5637 if( ret != 0 )
5638 return( ret );
5639#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005640
Hanno Becker8b09b732019-05-08 12:03:28 +01005641 /* Reset in pointers to default state for TLS/DTLS records,
5642 * assuming no CID and no offset between record content and
5643 * record plaintext. */
5644 ssl_update_in_pointers( ssl );
5645
5646 /* Ensure that we have enough space available for the default form
5647 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5648 * with no space for CIDs counted in). */
5649 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5650 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005653 return( ret );
5654 }
5655
5656 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005659 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5660 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005661 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005662 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5663 {
5664 ret = ssl_buffer_future_record( ssl );
5665 if( ret != 0 )
5666 return( ret );
5667
5668 /* Fall through to handling of unexpected records */
5669 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5670 }
5671
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005672 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5673 {
5674 /* Skip unexpected record (but not whole datagram) */
5675 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005676 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005677
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5679 "(header)" ) );
5680 }
5681 else
5682 {
5683 /* Skip invalid record and the rest of the datagram */
5684 ssl->next_record_offset = 0;
5685 ssl->in_left = 0;
5686
5687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5688 "(header)" ) );
5689 }
5690
5691 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005692 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005693 }
5694#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005695 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005696 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005697
5698 /*
5699 * Read and optionally decrypt the message contents
5700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005702 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005705 return( ret );
5706 }
5707
5708 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005710 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005711 {
Hanno Becker43395762019-05-03 14:46:38 +01005712 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005713 if( ssl->next_record_offset < ssl->in_left )
5714 {
5715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5716 }
5717 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005718 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005719#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005720#if defined(MBEDTLS_SSL_PROTO_TLS)
5721 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005723 }
5724#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005725
5726 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005729 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005730 {
5731 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005732 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005733 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005734 /* Except when waiting for Finished as a bad mac here
5735 * probably means something went wrong in the handshake
5736 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5737 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5738 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5739 {
5740#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5741 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5742 {
5743 mbedtls_ssl_send_alert_message( ssl,
5744 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5745 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5746 }
5747#endif
5748 return( ret );
5749 }
5750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005752 if( ssl->conf->badmac_limit != 0 &&
5753 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5756 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005757 }
5758#endif
5759
Hanno Becker4a810fb2017-05-24 16:27:30 +01005760 /* As above, invalid records cause
5761 * dismissal of the whole datagram. */
5762
5763 ssl->next_record_offset = 0;
5764 ssl->in_left = 0;
5765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005767 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005768 }
5769
5770 return( ret );
5771 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005772 MBEDTLS_SSL_TRANSPORT_ELSE
5773#endif /* MBEDTLS_SSL_PROTO_DTLS */
5774#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005775 {
5776 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5778 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 mbedtls_ssl_send_alert_message( ssl,
5781 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5782 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005783 }
5784#endif
5785 return( ret );
5786 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005787#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005788 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005789
Simon Butcher99000142016-10-13 17:21:01 +01005790 return( 0 );
5791}
5792
5793int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5794{
5795 int ret;
5796
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005797 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005798 * Handle particular types of records
5799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005801 {
Simon Butcher99000142016-10-13 17:21:01 +01005802 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5803 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005804 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005805 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005806 }
5807
Hanno Beckere678eaa2018-08-21 14:57:46 +01005808 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005809 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005810 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005811 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5813 ssl->in_msglen ) );
5814 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005815 }
5816
Hanno Beckere678eaa2018-08-21 14:57:46 +01005817 if( ssl->in_msg[0] != 1 )
5818 {
5819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5820 ssl->in_msg[0] ) );
5821 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5822 }
5823
5824#if defined(MBEDTLS_SSL_PROTO_DTLS)
5825 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5826 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5827 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5828 {
5829 if( ssl->handshake == NULL )
5830 {
5831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5832 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5833 }
5834
5835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5836 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5837 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005838#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005839 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005842 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005843 if( ssl->in_msglen != 2 )
5844 {
5845 /* Note: Standard allows for more than one 2 byte alert
5846 to be packed in a single message, but Mbed TLS doesn't
5847 currently support this. */
5848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5849 ssl->in_msglen ) );
5850 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5851 }
5852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005854 ssl->in_msg[0], ssl->in_msg[1] ) );
5855
5856 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005857 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005862 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005864 }
5865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5867 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5870 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005871 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005872
5873#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5874 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5875 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5876 {
Hanno Becker90333da2017-10-10 11:27:13 +01005877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005878 /* Will be handled when trying to parse ServerHello */
5879 return( 0 );
5880 }
5881#endif
5882
5883#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5884 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5885 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5886 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5887 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5888 {
5889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5890 /* Will be handled in mbedtls_ssl_parse_certificate() */
5891 return( 0 );
5892 }
5893#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5894
5895 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005896 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005897 }
5898
Hanno Beckerc76c6192017-06-06 10:03:17 +01005899#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005900 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005901 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005902 /* Drop unexpected ApplicationData records,
5903 * except at the beginning of renegotiations */
5904 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5905 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5906#if defined(MBEDTLS_SSL_RENEGOTIATION)
5907 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5908 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005909#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005910 )
5911 {
5912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5913 return( MBEDTLS_ERR_SSL_NON_FATAL );
5914 }
5915
5916 if( ssl->handshake != NULL &&
5917 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5918 {
5919 ssl_handshake_wrapup_free_hs_transform( ssl );
5920 }
5921 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005922#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005923
Paul Bakker5121ce52009-01-03 21:22:43 +00005924 return( 0 );
5925}
5926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005928{
5929 int ret;
5930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5932 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5933 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005934 {
5935 return( ret );
5936 }
5937
5938 return( 0 );
5939}
5940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005942 unsigned char level,
5943 unsigned char message )
5944{
5945 int ret;
5946
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005947 if( ssl == NULL || ssl->conf == NULL )
5948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005954 ssl->out_msglen = 2;
5955 ssl->out_msg[0] = level;
5956 ssl->out_msg[1] = message;
5957
Hanno Becker67bc7c32018-08-06 11:33:50 +01005958 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005961 return( ret );
5962 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005964
5965 return( 0 );
5966}
5967
Paul Bakker5121ce52009-01-03 21:22:43 +00005968/*
5969 * Handshake functions
5970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5972 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5973 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5974 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5975 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5976 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5977 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005978/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005980{
Hanno Becker8759e162017-12-27 21:34:08 +00005981 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5986 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005987 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5988 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005991 ssl->state++;
5992 return( 0 );
5993 }
5994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5996 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005997}
5998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006000{
Hanno Becker8759e162017-12-27 21:34:08 +00006001 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6006 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006007 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6008 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006011 ssl->state++;
6012 return( 0 );
6013 }
6014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6016 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006017}
Gilles Peskinef9828522017-05-03 12:28:43 +02006018
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006019#else
Gilles Peskinef9828522017-05-03 12:28:43 +02006020/* Some certificate support -> implement write and parse */
6021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006023{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006025 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006027 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6032 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006033 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6034 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006037 ssl->state++;
6038 return( 0 );
6039 }
6040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006042 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006043 {
6044 if( ssl->client_auth == 0 )
6045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006047 ssl->state++;
6048 return( 0 );
6049 }
6050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006052 /*
6053 * If using SSLv3 and got no cert, send an Alert message
6054 * (otherwise an empty Certificate message will be sent).
6055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6057 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006058 {
6059 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6061 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6062 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006065 goto write_msg;
6066 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069#endif /* MBEDTLS_SSL_CLI_C */
6070#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006071 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6076 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 }
6078 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006079#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006082
6083 /*
6084 * 0 . 0 handshake type
6085 * 1 . 3 handshake length
6086 * 4 . 6 length of all certs
6087 * 7 . 9 length of cert. 1
6088 * 10 . n-1 peer certificate
6089 * n . n+2 length of cert. 2
6090 * n+3 . ... upper level cert, etc.
6091 */
6092 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006094
Paul Bakker29087132010-03-21 21:03:34 +00006095 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006096 {
6097 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006098 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006101 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006103 }
6104
6105 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6106 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6107 ssl->out_msg[i + 2] = (unsigned char)( n );
6108
6109 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6110 i += n; crt = crt->next;
6111 }
6112
6113 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6114 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6115 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6116
6117 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6119 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006120
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006121#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006122write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006123#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006124
6125 ssl->state++;
6126
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006127 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006128 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 return( ret );
6131 }
6132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006134
Paul Bakkered27a042013-04-18 22:46:23 +02006135 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006136}
6137
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006138/*
6139 * Once the certificate message is read, parse it into a cert chain and
6140 * perform basic checks, but leave actual verification to the caller
6141 */
6142static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006143{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006144 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006145 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006146 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148#if defined(MBEDTLS_SSL_SRV_C)
6149#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006150 /*
6151 * Check if the client sent an empty certificate
6152 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006153 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006156 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6158 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6159 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006162
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006163 /* The client was asked for a certificate but didn't send
6164 one. The client should know what's going on, so we
6165 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006166 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006167 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006168 }
6169 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6173 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006174 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6178 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6179 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6180 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006183
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006184 /* The client was asked for a certificate but didn't send
6185 one. The client should know what's going on, so we
6186 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006187 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006188 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 }
6190 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6192 MBEDTLS_SSL_PROTO_TLS1_2 */
6193#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006198 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6199 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 }
6202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6204 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006207 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6208 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006210 }
6211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006213
Paul Bakker5121ce52009-01-03 21:22:43 +00006214 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006216 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006217 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006218
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006219 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006223 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6224 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006226 }
6227
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006228 /* In case we tried to reuse a session but it failed */
6229 if( ssl->session_negotiate->peer_cert != NULL )
6230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6232 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006233 }
6234
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006235 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006237 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006240 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6241 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006242 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006243 }
6244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006246
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006247 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006248
6249 while( i < ssl->in_hslen )
6250 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006251 if ( i + 3 > ssl->in_hslen ) {
6252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6253 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6254 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6255 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006257 if( ssl->in_msg[i] != 0 )
6258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006260 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6261 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006263 }
6264
6265 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6266 | (unsigned int) ssl->in_msg[i + 2];
6267 i += 3;
6268
6269 if( n < 128 || i + n > ssl->in_hslen )
6270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006272 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6273 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006275 }
6276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006277 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006278 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006279 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006280 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006281 case 0: /*ok*/
6282 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6283 /* Ignore certificate with an unknown algorithm: maybe a
6284 prior certificate was already trusted. */
6285 break;
6286
6287 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6288 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6289 goto crt_parse_der_failed;
6290
6291 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6292 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6293 goto crt_parse_der_failed;
6294
6295 default:
6296 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6297 crt_parse_der_failed:
6298 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006300 return( ret );
6301 }
6302
6303 i += n;
6304 }
6305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006307
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006308 /*
6309 * On client, make sure the server cert doesn't change during renego to
6310 * avoid "triple handshake" attack: https://secure-resumption.com/
6311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006313 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006315 {
6316 if( ssl->session->peer_cert == NULL )
6317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006319 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6320 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006322 }
6323
6324 if( ssl->session->peer_cert->raw.len !=
6325 ssl->session_negotiate->peer_cert->raw.len ||
6326 memcmp( ssl->session->peer_cert->raw.p,
6327 ssl->session_negotiate->peer_cert->raw.p,
6328 ssl->session->peer_cert->raw.len ) != 0 )
6329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006331 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6332 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006334 }
6335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006337
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006338 return( 0 );
6339}
6340
6341int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6342{
6343 int ret;
6344 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006345 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006346#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6347 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6348 ? ssl->handshake->sni_authmode
6349 : ssl->conf->authmode;
6350#else
6351 const int authmode = ssl->conf->authmode;
6352#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006353 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006354
6355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6356
6357 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6358 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6359 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6360 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6361 {
6362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6363 ssl->state++;
6364 return( 0 );
6365 }
6366
6367#if defined(MBEDTLS_SSL_SRV_C)
6368 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6369 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6370 {
6371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6372 ssl->state++;
6373 return( 0 );
6374 }
6375
6376 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6377 authmode == MBEDTLS_SSL_VERIFY_NONE )
6378 {
6379 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006381
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006382 ssl->state++;
6383 return( 0 );
6384 }
6385#endif
6386
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006387#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6388 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006389 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006390 {
6391 goto crt_verify;
6392 }
6393#endif
6394
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006395 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006396 {
6397 /* mbedtls_ssl_read_record may have sent an alert already. We
6398 let it decide whether to alert. */
6399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6400 return( ret );
6401 }
6402
6403 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6404 {
6405#if defined(MBEDTLS_SSL_SRV_C)
6406 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6407 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6408 {
6409 ret = 0;
6410 }
6411#endif
6412
6413 ssl->state++;
6414 return( ret );
6415 }
6416
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006417#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6418 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006419 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006420
6421crt_verify:
6422 if( ssl->handshake->ecrs_enabled)
6423 rs_ctx = &ssl->handshake->ecrs_ctx;
6424#endif
6425
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006426 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006427 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006428 mbedtls_x509_crt *ca_chain;
6429 mbedtls_x509_crl *ca_crl;
6430
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006431#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006432 if( ssl->handshake->sni_ca_chain != NULL )
6433 {
6434 ca_chain = ssl->handshake->sni_ca_chain;
6435 ca_crl = ssl->handshake->sni_ca_crl;
6436 }
6437 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006438#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006439 {
6440 ca_chain = ssl->conf->ca_chain;
6441 ca_crl = ssl->conf->ca_crl;
6442 }
6443
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006444 /*
6445 * Main check: verify certificate
6446 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006447 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006448 ssl->session_negotiate->peer_cert,
6449 ca_chain, ca_crl,
6450 ssl->conf->cert_profile,
6451 ssl->hostname,
6452 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006453 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006454
6455 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006458 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006459
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006460#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6461 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006462 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006463#endif
6464
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006465 /*
6466 * Secondary checks: always done, but change 'ret' only if it was 0
6467 */
6468
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006469#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006472
6473 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006475 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006476 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006477 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006480 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006482 }
6483 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006484#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006487 ciphersuite_info,
6488 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006489 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006492 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006494 }
6495
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006496 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6497 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6498 * with details encoded in the verification flags. All other kinds
6499 * of error codes, including those from the user provided f_vrfy
6500 * functions, are treated as fatal and lead to a failure of
6501 * ssl_parse_certificate even if verification was optional. */
6502 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6503 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6504 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6505 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006506 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006507 }
6508
6509 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6510 {
6511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6512 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6513 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006514
6515 if( ret != 0 )
6516 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006517 uint8_t alert;
6518
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006519 /* The certificate may have been rejected for several reasons.
6520 Pick one and send the corresponding alert. Which alert to send
6521 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006522 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6523 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6524 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6525 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6526 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6527 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6528 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6529 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6530 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6531 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6532 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6533 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6534 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6535 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6536 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6537 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6538 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6539 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6540 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6541 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006542 else
6543 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006544 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6545 alert );
6546 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006547
Hanno Beckere6706e62017-05-15 16:05:15 +01006548#if defined(MBEDTLS_DEBUG_C)
6549 if( ssl->session_negotiate->verify_result != 0 )
6550 {
6551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6552 ssl->session_negotiate->verify_result ) );
6553 }
6554 else
6555 {
6556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6557 }
6558#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006559 }
6560
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006561 ssl->state++;
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006564
6565 return( ret );
6566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6568 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6569 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6570 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6571 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6572 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6573 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006576{
6577 int ret;
6578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006582 ssl->out_msglen = 1;
6583 ssl->out_msg[0] = 1;
6584
Paul Bakker5121ce52009-01-03 21:22:43 +00006585 ssl->state++;
6586
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006587 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006588 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006590 return( ret );
6591 }
6592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594
6595 return( 0 );
6596}
6597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006599{
6600 int ret;
6601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006603
Hanno Becker327c93b2018-08-15 13:56:18 +01006604 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006607 return( ret );
6608 }
6609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006613 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6614 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006616 }
6617
Hanno Beckere678eaa2018-08-21 14:57:46 +01006618 /* CCS records are only accepted if they have length 1 and content '1',
6619 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006620
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006621 /*
6622 * Switch to our negotiated transform and session parameters for inbound
6623 * data.
6624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006626 ssl->transform_in = ssl->transform_negotiate;
6627 ssl->session_in = ssl->session_negotiate;
6628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006630 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006633 ssl_dtls_replay_reset( ssl );
6634#endif
6635
6636 /* Increment epoch */
6637 if( ++ssl->in_epoch == 0 )
6638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006640 /* This is highly unlikely to happen for legitimate reasons, so
6641 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006643 }
6644 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006645 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006647#if defined(MBEDTLS_SSL_PROTO_TLS)
6648 {
6649 memset( ssl->in_ctr, 0, 8 );
6650 }
6651#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006652
Hanno Beckerf5970a02019-05-08 09:38:41 +01006653 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6656 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6662 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006664 }
6665 }
6666#endif
6667
Paul Bakker5121ce52009-01-03 21:22:43 +00006668 ssl->state++;
6669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
6672 return( 0 );
6673}
6674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6676 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006677{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006678 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6681 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6682 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006683 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006684 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006685#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6687#if defined(MBEDTLS_SHA512_C)
6688 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006689 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6690 else
6691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692#if defined(MBEDTLS_SHA256_C)
6693 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006694 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006695 else
6696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006700 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006701 }
Paul Bakker380da532012-04-18 16:10:25 +00006702}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6707 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006708 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6709 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6712#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006713 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006716 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006717#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006719}
6720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006722 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6725 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006726 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6727 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6730#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006731 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006732#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006734 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006737}
6738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6740 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6741static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006742 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006743{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006744 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6745 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006746}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006747#endif
Paul Bakker380da532012-04-18 16:10:25 +00006748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6750#if defined(MBEDTLS_SHA256_C)
6751static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006752 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006753{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006754 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006755}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006756#endif
Paul Bakker380da532012-04-18 16:10:25 +00006757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758#if defined(MBEDTLS_SHA512_C)
6759static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006760 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006761{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006762 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006763}
Paul Bakker769075d2012-11-24 11:26:46 +01006764#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006768static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006770{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006771 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006772 mbedtls_md5_context md5;
6773 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006774
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 unsigned char padbuf[48];
6776 unsigned char md5sum[16];
6777 unsigned char sha1sum[20];
6778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006780 if( !session )
6781 session = ssl->session;
6782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006784
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006785 mbedtls_md5_init( &md5 );
6786 mbedtls_sha1_init( &sha1 );
6787
6788 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6789 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006790
6791 /*
6792 * SSLv3:
6793 * hash =
6794 * MD5( master + pad2 +
6795 * MD5( handshake + sender + master + pad1 ) )
6796 * + SHA1( master + pad2 +
6797 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006798 */
6799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006801 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6802 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006803#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006806 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6807 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006808#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006811 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006812
Paul Bakker1ef83d62012-04-11 12:09:53 +00006813 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006814
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006815 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6816 mbedtls_md5_update_ret( &md5, session->master, 48 );
6817 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6818 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006819
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006820 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6821 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6822 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6823 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
Paul Bakker1ef83d62012-04-11 12:09:53 +00006825 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006826
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006827 mbedtls_md5_starts_ret( &md5 );
6828 mbedtls_md5_update_ret( &md5, session->master, 48 );
6829 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6830 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6831 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006833 mbedtls_sha1_starts_ret( &sha1 );
6834 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6835 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6836 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6837 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006840
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006841 mbedtls_md5_free( &md5 );
6842 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006843
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006844 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6845 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6846 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006853static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006855{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006856 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006857 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006858 mbedtls_md5_context md5;
6859 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006860 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006862 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006863 if( !session )
6864 session = ssl->session;
6865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006867
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006868 mbedtls_md5_init( &md5 );
6869 mbedtls_sha1_init( &sha1 );
6870
6871 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6872 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006873
Paul Bakker1ef83d62012-04-11 12:09:53 +00006874 /*
6875 * TLSv1:
6876 * hash = PRF( master, finished_label,
6877 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6878 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006881 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6882 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006883#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006886 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6887 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006888#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006891 ? "client finished"
6892 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006893
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006894 mbedtls_md5_finish_ret( &md5, padbuf );
6895 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006896
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006897 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006898 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006901
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006902 mbedtls_md5_free( &md5 );
6903 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006904
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006905 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006908}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6912#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006913static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006915{
6916 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006917 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006918 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006919 unsigned char padbuf[32];
6920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006922 if( !session )
6923 session = ssl->session;
6924
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006925 mbedtls_sha256_init( &sha256 );
6926
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006929 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006930
6931 /*
6932 * TLSv1.2:
6933 * hash = PRF( master, finished_label,
6934 * Hash( handshake ) )[0.11]
6935 */
6936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#if !defined(MBEDTLS_SHA256_ALT)
6938 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006939 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006940#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006943 ? "client finished"
6944 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006945
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006946 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006947
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006948 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006949 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006952
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006953 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006954
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006955 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006962static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006964{
6965 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006966 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006967 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006968 unsigned char padbuf[48];
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006971 if( !session )
6972 session = ssl->session;
6973
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006974 mbedtls_sha512_init( &sha512 );
6975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006977
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006978 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006979
6980 /*
6981 * TLSv1.2:
6982 * hash = PRF( master, finished_label,
6983 * Hash( handshake ) )[0.11]
6984 */
6985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006987 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6988 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006989#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006992 ? "client finished"
6993 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006994
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006995 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006996
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006997 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006998 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007001
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007002 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007003
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007004 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008#endif /* MBEDTLS_SHA512_C */
7009#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007014
7015 /*
7016 * Free our handshake params
7017 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007018 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007020 ssl->handshake = NULL;
7021
7022 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007023 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007024 */
7025 if( ssl->transform )
7026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027 mbedtls_ssl_transform_free( ssl->transform );
7028 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007029 }
7030 ssl->transform = ssl->transform_negotiate;
7031 ssl->transform_negotiate = NULL;
7032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007034}
7035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007037{
7038 int resume = ssl->handshake->resume;
7039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042#if defined(MBEDTLS_SSL_RENEGOTIATION)
7043 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007046 ssl->renego_records_seen = 0;
7047 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007048#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007049
7050 /*
7051 * Free the previous session and switch in the current one
7052 */
Paul Bakker0a597072012-09-25 21:55:46 +00007053 if( ssl->session )
7054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007056 /* RFC 7366 3.1: keep the EtM state */
7057 ssl->session_negotiate->encrypt_then_mac =
7058 ssl->session->encrypt_then_mac;
7059#endif
7060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061 mbedtls_ssl_session_free( ssl->session );
7062 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007063 }
7064 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007065 ssl->session_negotiate = NULL;
7066
Paul Bakker0a597072012-09-25 21:55:46 +00007067 /*
7068 * Add cache entry
7069 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007070 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007071 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007072 resume == 0 )
7073 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007074 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007076 }
Paul Bakker0a597072012-09-25 21:55:46 +00007077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007079 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007080 ssl->handshake->flight != NULL )
7081 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007082 /* Cancel handshake timer */
7083 ssl_set_timer( ssl, 0 );
7084
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007085 /* Keep last flight around in case we need to resend it:
7086 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007088 }
7089 else
7090#endif
7091 ssl_handshake_wrapup_free_hs_transform( ssl );
7092
Paul Bakker48916f92012-09-16 19:57:18 +00007093 ssl->state++;
7094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007096}
7097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007099{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007100 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007103
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007104 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007105
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007106 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007107
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007108 /*
7109 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7110 * may define some other value. Currently (early 2016), no defined
7111 * ciphersuite does this (and this is unlikely to change as activity has
7112 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007117 ssl->verify_data_len = hash_len;
7118 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007119#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007120
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7123 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007124
7125 /*
7126 * In case of session resuming, invert the client and server
7127 * ChangeCipherSpec messages order.
7128 */
Paul Bakker0a597072012-09-25 21:55:46 +00007129 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007132 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007136 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007139 }
7140 else
7141 ssl->state++;
7142
Paul Bakker48916f92012-09-16 19:57:18 +00007143 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007144 * Switch to our negotiated transform and session parameters for outbound
7145 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007150 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007151 {
7152 unsigned char i;
7153
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007154 /* Remember current epoch settings for resending */
7155 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007156 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007157
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007158 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007159 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007160
7161 /* Increment epoch */
7162 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007163 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007164 break;
7165
7166 /* The loop goes to its end iff the counter is wrapping */
7167 if( i == 0 )
7168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7170 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007171 }
7172 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007173 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007175#if defined(MBEDTLS_SSL_PROTO_TLS)
7176 {
7177 memset( ssl->cur_out_ctr, 0, 8 );
7178 }
7179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007180
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007181 ssl->transform_out = ssl->transform_negotiate;
7182 ssl->session_out = ssl->session_negotiate;
7183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7185 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7190 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007191 }
7192 }
7193#endif
7194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007196 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007198#endif
7199
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007200 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007201 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007203 return( ret );
7204 }
7205
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007206#if defined(MBEDTLS_SSL_PROTO_DTLS)
7207 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7208 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7209 {
7210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7211 return( ret );
7212 }
7213#endif
7214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007216
7217 return( 0 );
7218}
7219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007221#define SSL_MAX_HASH_LEN 36
7222#else
7223#define SSL_MAX_HASH_LEN 12
7224#endif
7225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007227{
Paul Bakker23986e52011-04-24 08:57:21 +00007228 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007229 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007230 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007233
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007234 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007235
Hanno Becker327c93b2018-08-15 13:56:18 +01007236 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007239 return( ret );
7240 }
7241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007245 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7246 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007248 }
7249
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007250 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_SSL_PROTO_SSL3)
7252 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007253 hash_len = 36;
7254 else
7255#endif
7256 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7259 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007262 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7263 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007265 }
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007268 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007271 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7272 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007274 }
7275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007277 ssl->verify_data_len = hash_len;
7278 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007279#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007280
Paul Bakker0a597072012-09-25 21:55:46 +00007281 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007284 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007288 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007291 }
7292 else
7293 ssl->state++;
7294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007296 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007298#endif
7299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007301
7302 return( 0 );
7303}
7304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7310 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7311 mbedtls_md5_init( &handshake->fin_md5 );
7312 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007313 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7314 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007315#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7317#if defined(MBEDTLS_SHA256_C)
7318 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007319 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007320#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321#if defined(MBEDTLS_SHA512_C)
7322 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007323 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007326
7327 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007328
7329#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7330 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7331 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7332#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334#if defined(MBEDTLS_DHM_C)
7335 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337#if defined(MBEDTLS_ECDH_C)
7338 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007339#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007340#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007341 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007342#if defined(MBEDTLS_SSL_CLI_C)
7343 handshake->ecjpake_cache = NULL;
7344 handshake->ecjpake_cache_len = 0;
7345#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007346#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007347
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007348#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007349 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007350#endif
7351
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007352#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7353 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7354#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007355}
7356
Hanno Becker611a83b2018-01-03 14:27:32 +00007357void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007358{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7362 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007363
Hanno Becker92231322018-01-03 15:32:51 +00007364#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365 mbedtls_md_init( &transform->md_ctx_enc );
7366 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007367#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007368}
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007373}
7374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007376{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007377 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007378 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007380 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007382 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007383 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007384
7385 /*
7386 * Either the pointers are now NULL or cleared properly and can be freed.
7387 * Now allocate missing structures.
7388 */
7389 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007390 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007391 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007392 }
Paul Bakker48916f92012-09-16 19:57:18 +00007393
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007394 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007395 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007396 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007397 }
Paul Bakker48916f92012-09-16 19:57:18 +00007398
Paul Bakker82788fb2014-10-20 13:59:19 +02007399 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007400 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007401 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007402 }
Paul Bakker48916f92012-09-16 19:57:18 +00007403
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007404 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007405 if( ssl->handshake == NULL ||
7406 ssl->transform_negotiate == NULL ||
7407 ssl->session_negotiate == NULL )
7408 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 mbedtls_free( ssl->handshake );
7412 mbedtls_free( ssl->transform_negotiate );
7413 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007414
7415 ssl->handshake = NULL;
7416 ssl->transform_negotiate = NULL;
7417 ssl->session_negotiate = NULL;
7418
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007420 }
7421
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007422 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007424 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007425 ssl_handshake_params_init( ssl->handshake );
7426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007428 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7429 {
7430 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007431
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007432 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7433 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7434 else
7435 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007436
7437 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007438 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007439#endif
7440
Paul Bakker48916f92012-09-16 19:57:18 +00007441 return( 0 );
7442}
7443
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007444#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007445/* Dummy cookie callbacks for defaults */
7446static int ssl_cookie_write_dummy( void *ctx,
7447 unsigned char **p, unsigned char *end,
7448 const unsigned char *cli_id, size_t cli_id_len )
7449{
7450 ((void) ctx);
7451 ((void) p);
7452 ((void) end);
7453 ((void) cli_id);
7454 ((void) cli_id_len);
7455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007457}
7458
7459static int ssl_cookie_check_dummy( void *ctx,
7460 const unsigned char *cookie, size_t cookie_len,
7461 const unsigned char *cli_id, size_t cli_id_len )
7462{
7463 ((void) ctx);
7464 ((void) cookie);
7465 ((void) cookie_len);
7466 ((void) cli_id);
7467 ((void) cli_id_len);
7468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007470}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007471#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007472
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007473/* Once ssl->out_hdr as the address of the beginning of the
7474 * next outgoing record is set, deduce the other pointers.
7475 *
7476 * Note: For TLS, we save the implicit record sequence number
7477 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7478 * and the caller has to make sure there's space for this.
7479 */
7480
7481static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7482 mbedtls_ssl_transform *transform )
7483{
7484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007485 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007486 {
7487 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007488#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007489 ssl->out_cid = ssl->out_ctr + 8;
7490 ssl->out_len = ssl->out_cid;
7491 if( transform != NULL )
7492 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007493#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007494 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007495#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007496 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007497 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007498 MBEDTLS_SSL_TRANSPORT_ELSE
7499#endif /* MBEDTLS_SSL_PROTO_DTLS */
7500#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007501 {
7502 ssl->out_ctr = ssl->out_hdr - 8;
7503 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007504#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007505 ssl->out_cid = ssl->out_len;
7506#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007507 ssl->out_iv = ssl->out_hdr + 5;
7508 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007509#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007510
7511 /* Adjust out_msg to make space for explicit IV, if used. */
7512 if( transform != NULL &&
7513 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7514 {
7515 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7516 }
7517 else
7518 ssl->out_msg = ssl->out_iv;
7519}
7520
7521/* Once ssl->in_hdr as the address of the beginning of the
7522 * next incoming record is set, deduce the other pointers.
7523 *
7524 * Note: For TLS, we save the implicit record sequence number
7525 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7526 * and the caller has to make sure there's space for this.
7527 */
7528
Hanno Beckerf5970a02019-05-08 09:38:41 +01007529static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007530{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007531 /* This function sets the pointers to match the case
7532 * of unprotected TLS/DTLS records, with both ssl->in_iv
7533 * and ssl->in_msg pointing to the beginning of the record
7534 * content.
7535 *
7536 * When decrypting a protected record, ssl->in_msg
7537 * will be shifted to point to the beginning of the
7538 * record plaintext.
7539 */
7540
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007541#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007542 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007543 {
Hanno Becker70e79282019-05-03 14:34:53 +01007544 /* This sets the header pointers to match records
7545 * without CID. When we receive a record containing
7546 * a CID, the fields are shifted accordingly in
7547 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007548 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007549#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007550 ssl->in_cid = ssl->in_ctr + 8;
7551 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007552#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007553 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007554#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007555 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007556 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007557 MBEDTLS_SSL_TRANSPORT_ELSE
7558#endif /* MBEDTLS_SSL_PROTO_DTLS */
7559#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007560 {
7561 ssl->in_ctr = ssl->in_hdr - 8;
7562 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007563#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007564 ssl->in_cid = ssl->in_len;
7565#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007566 ssl->in_iv = ssl->in_hdr + 5;
7567 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007568#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007569
Hanno Beckerf5970a02019-05-08 09:38:41 +01007570 /* This will be adjusted at record decryption time. */
7571 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007572}
7573
Paul Bakker5121ce52009-01-03 21:22:43 +00007574/*
7575 * Initialize an SSL context
7576 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007577void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7578{
7579 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7580}
7581
7582/*
7583 * Setup an SSL context
7584 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007585
7586static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7587{
7588 /* Set the incoming and outgoing record pointers. */
7589#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007590 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007591 {
7592 ssl->out_hdr = ssl->out_buf;
7593 ssl->in_hdr = ssl->in_buf;
7594 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007595 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007596#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007597#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007598 {
7599 ssl->out_hdr = ssl->out_buf + 8;
7600 ssl->in_hdr = ssl->in_buf + 8;
7601 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007602#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007603
7604 /* Derive other internal pointers. */
7605 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007606 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007607}
7608
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007609int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007610 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007611{
Paul Bakker48916f92012-09-16 19:57:18 +00007612 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007613
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007614 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007615
7616 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007617 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007618 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007619
7620 /* Set to NULL in case of an error condition */
7621 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007622
Angus Grattond8213d02016-05-25 20:56:48 +10007623 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7624 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007625 {
Angus Grattond8213d02016-05-25 20:56:48 +10007626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007627 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007628 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007629 }
7630
7631 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7632 if( ssl->out_buf == NULL )
7633 {
7634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007635 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007636 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007637 }
7638
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007639 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007640
Paul Bakker48916f92012-09-16 19:57:18 +00007641 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007642 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007643
7644 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007645
7646error:
7647 mbedtls_free( ssl->in_buf );
7648 mbedtls_free( ssl->out_buf );
7649
7650 ssl->conf = NULL;
7651
7652 ssl->in_buf = NULL;
7653 ssl->out_buf = NULL;
7654
7655 ssl->in_hdr = NULL;
7656 ssl->in_ctr = NULL;
7657 ssl->in_len = NULL;
7658 ssl->in_iv = NULL;
7659 ssl->in_msg = NULL;
7660
7661 ssl->out_hdr = NULL;
7662 ssl->out_ctr = NULL;
7663 ssl->out_len = NULL;
7664 ssl->out_iv = NULL;
7665 ssl->out_msg = NULL;
7666
k-stachowiak9f7798e2018-07-31 16:52:32 +02007667 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007668}
7669
7670/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007671 * Reset an initialized and used SSL context for re-use while retaining
7672 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007673 *
7674 * If partial is non-zero, keep data in the input buffer and client ID.
7675 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007676 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007677static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007678{
Paul Bakker48916f92012-09-16 19:57:18 +00007679 int ret;
7680
Hanno Becker7e772132018-08-10 12:38:21 +01007681#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7682 !defined(MBEDTLS_SSL_SRV_C)
7683 ((void) partial);
7684#endif
7685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007687
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007688 /* Cancel any possibly running timer */
7689 ssl_set_timer( ssl, 0 );
7690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691#if defined(MBEDTLS_SSL_RENEGOTIATION)
7692 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007693 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007694
7695 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7697 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007700
Paul Bakker7eb013f2011-10-06 12:37:39 +00007701 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007702 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007703
7704 ssl->in_msgtype = 0;
7705 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007707 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007708 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007711 ssl_dtls_replay_reset( ssl );
7712#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007713
7714 ssl->in_hslen = 0;
7715 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007716
7717 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007718
7719 ssl->out_msgtype = 0;
7720 ssl->out_msglen = 0;
7721 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7723 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007724 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007725#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007726
Hanno Becker19859472018-08-06 09:40:20 +01007727 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7728
Paul Bakker48916f92012-09-16 19:57:18 +00007729 ssl->transform_in = NULL;
7730 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007731
Hanno Becker78640902018-08-13 16:35:15 +01007732 ssl->session_in = NULL;
7733 ssl->session_out = NULL;
7734
Angus Grattond8213d02016-05-25 20:56:48 +10007735 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007736
7737#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007738 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007739#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7740 {
7741 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007742 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007743 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7746 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7749 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7752 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007753 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007754 }
7755#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007756
Paul Bakker48916f92012-09-16 19:57:18 +00007757 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 mbedtls_ssl_transform_free( ssl->transform );
7760 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007761 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007762 }
Paul Bakker48916f92012-09-16 19:57:18 +00007763
Paul Bakkerc0463502013-02-14 11:19:38 +01007764 if( ssl->session )
7765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 mbedtls_ssl_session_free( ssl->session );
7767 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007768 ssl->session = NULL;
7769 }
7770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007772 ssl->alpn_chosen = NULL;
7773#endif
7774
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007775#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007776#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007777 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007778#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007779 {
7780 mbedtls_free( ssl->cli_id );
7781 ssl->cli_id = NULL;
7782 ssl->cli_id_len = 0;
7783 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007784#endif
7785
Paul Bakker48916f92012-09-16 19:57:18 +00007786 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7787 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007788
7789 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007790}
7791
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007792/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007793 * Reset an initialized and used SSL context for re-use while retaining
7794 * all application-set variables, function pointers and data.
7795 */
7796int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7797{
7798 return( ssl_session_reset_int( ssl, 0 ) );
7799}
7800
7801/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007802 * SSL set accessors
7803 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007804void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007805{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007806 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007807}
7808
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007809void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007810{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007811 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007812}
7813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007815void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007816{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007817 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007818}
7819#endif
7820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007822void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007823{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007824 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007825}
7826#endif
7827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007829
Hanno Becker1841b0a2018-08-24 11:13:57 +01007830void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7831 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007832{
7833 ssl->disable_datagram_packing = !allow_packing;
7834}
7835
7836void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7837 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007838{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007839 conf->hs_timeout_min = min;
7840 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007841}
7842#endif
7843
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007844void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007845{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007846 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007847}
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007850void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007851 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007852 void *p_vrfy )
7853{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007854 conf->f_vrfy = f_vrfy;
7855 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007858
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007859void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007860 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007861 void *p_rng )
7862{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007863 conf->f_rng = f_rng;
7864 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007865}
7866
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007867void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007868 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007869 void *p_dbg )
7870{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007871 conf->f_dbg = f_dbg;
7872 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007873}
7874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007876 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007877 mbedtls_ssl_send_t *f_send,
7878 mbedtls_ssl_recv_t *f_recv,
7879 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007880{
7881 ssl->p_bio = p_bio;
7882 ssl->f_send = f_send;
7883 ssl->f_recv = f_recv;
7884 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007885}
7886
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007887#if defined(MBEDTLS_SSL_PROTO_DTLS)
7888void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7889{
7890 ssl->mtu = mtu;
7891}
7892#endif
7893
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007894void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007895{
7896 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007897}
7898
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007899void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7900 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007901 mbedtls_ssl_set_timer_t *f_set_timer,
7902 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007903{
7904 ssl->p_timer = p_timer;
7905 ssl->f_set_timer = f_set_timer;
7906 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007907
7908 /* Make sure we start with no timer running */
7909 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007910}
7911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007913void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007914 void *p_cache,
7915 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7916 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007917{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007918 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007919 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007920 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924#if defined(MBEDTLS_SSL_CLI_C)
7925int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007926{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007927 int ret;
7928
7929 if( ssl == NULL ||
7930 session == NULL ||
7931 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007932 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007935 }
7936
7937 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7938 return( ret );
7939
Paul Bakker0a597072012-09-25 21:55:46 +00007940 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007941
7942 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007943}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007945
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007946void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007947 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007948{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007949 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7950 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7951 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7952 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007953}
7954
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007955void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007956 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007957 int major, int minor )
7958{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007960 return;
7961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007963 return;
7964
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007965 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007966}
7967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007969void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007970 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007971{
7972 conf->cert_profile = profile;
7973}
7974
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007975/* Append a new keycert entry to a (possibly empty) list */
7976static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7977 mbedtls_x509_crt *cert,
7978 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007979{
niisato8ee24222018-06-25 19:05:48 +09007980 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007981
niisato8ee24222018-06-25 19:05:48 +09007982 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7983 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007984 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007985
niisato8ee24222018-06-25 19:05:48 +09007986 new_cert->cert = cert;
7987 new_cert->key = key;
7988 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007989
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007990 /* Update head is the list was null, else add to the end */
7991 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007992 {
niisato8ee24222018-06-25 19:05:48 +09007993 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007994 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007995 else
7996 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007997 mbedtls_ssl_key_cert *cur = *head;
7998 while( cur->next != NULL )
7999 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008000 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008001 }
8002
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008003 return( 0 );
8004}
8005
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008006int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008007 mbedtls_x509_crt *own_cert,
8008 mbedtls_pk_context *pk_key )
8009{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008010 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008011}
8012
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008013void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008014 mbedtls_x509_crt *ca_chain,
8015 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008016{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008017 conf->ca_chain = ca_chain;
8018 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008019}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008021
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008022#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8023int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8024 mbedtls_x509_crt *own_cert,
8025 mbedtls_pk_context *pk_key )
8026{
8027 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8028 own_cert, pk_key ) );
8029}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008030
8031void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8032 mbedtls_x509_crt *ca_chain,
8033 mbedtls_x509_crl *ca_crl )
8034{
8035 ssl->handshake->sni_ca_chain = ca_chain;
8036 ssl->handshake->sni_ca_crl = ca_crl;
8037}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008038
8039void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8040 int authmode )
8041{
8042 ssl->handshake->sni_authmode = authmode;
8043}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008044#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8045
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008046#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008047/*
8048 * Set EC J-PAKE password for current handshake
8049 */
8050int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8051 const unsigned char *pw,
8052 size_t pw_len )
8053{
8054 mbedtls_ecjpake_role role;
8055
Janos Follath8eb64132016-06-03 15:40:57 +01008056 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8058
8059 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8060 role = MBEDTLS_ECJPAKE_SERVER;
8061 else
8062 role = MBEDTLS_ECJPAKE_CLIENT;
8063
8064 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8065 role,
8066 MBEDTLS_MD_SHA256,
8067 MBEDTLS_ECP_DP_SECP256R1,
8068 pw, pw_len ) );
8069}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008070#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008073int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008074 const unsigned char *psk, size_t psk_len,
8075 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008076{
Paul Bakker6db455e2013-09-18 17:29:31 +02008077 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008082
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008083 /* Identity len will be encoded on two bytes */
8084 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008085 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008086 {
8087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8088 }
8089
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008090 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008091 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008092 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008093
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008094 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008095 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008096 conf->psk_len = 0;
8097 }
8098 if( conf->psk_identity != NULL )
8099 {
8100 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008101 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008102 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008103 }
8104
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008105 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8106 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008107 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008108 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008109 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008110 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008111 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008112 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008113 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008114
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008115 conf->psk_len = psk_len;
8116 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008117
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008118 memcpy( conf->psk, psk, conf->psk_len );
8119 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008120
8121 return( 0 );
8122}
8123
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008124int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8125 const unsigned char *psk, size_t psk_len )
8126{
8127 if( psk == NULL || ssl->handshake == NULL )
8128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8129
8130 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8132
8133 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008134 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008135 mbedtls_platform_zeroize( ssl->handshake->psk,
8136 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008137 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008138 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008139 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008140
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008141 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008142 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008143
8144 ssl->handshake->psk_len = psk_len;
8145 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8146
8147 return( 0 );
8148}
8149
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008150void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008152 size_t),
8153 void *p_psk )
8154{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008155 conf->f_psk = f_psk;
8156 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008159
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008160#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008161
8162#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008163int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008164{
8165 int ret;
8166
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008167 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8168 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8169 {
8170 mbedtls_mpi_free( &conf->dhm_P );
8171 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008172 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008174
8175 return( 0 );
8176}
Hanno Becker470a8c42017-10-04 15:28:46 +01008177#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008178
Hanno Beckera90658f2017-10-04 15:29:08 +01008179int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8180 const unsigned char *dhm_P, size_t P_len,
8181 const unsigned char *dhm_G, size_t G_len )
8182{
8183 int ret;
8184
8185 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8186 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8187 {
8188 mbedtls_mpi_free( &conf->dhm_P );
8189 mbedtls_mpi_free( &conf->dhm_G );
8190 return( ret );
8191 }
8192
8193 return( 0 );
8194}
Paul Bakker5121ce52009-01-03 21:22:43 +00008195
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008196int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008197{
8198 int ret;
8199
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008200 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8201 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8202 {
8203 mbedtls_mpi_free( &conf->dhm_P );
8204 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008205 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008206 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008207
8208 return( 0 );
8209}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008210#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008211
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008212#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8213/*
8214 * Set the minimum length for Diffie-Hellman parameters
8215 */
8216void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8217 unsigned int bitlen )
8218{
8219 conf->dhm_min_bitlen = bitlen;
8220}
8221#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8222
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008223#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008224/*
8225 * Set allowed/preferred hashes for handshake signatures
8226 */
8227void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8228 const int *hashes )
8229{
8230 conf->sig_hashes = hashes;
8231}
Hanno Becker947194e2017-04-07 13:25:49 +01008232#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008233
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008234#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008235/*
8236 * Set the allowed elliptic curves
8237 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008238void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008239 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008240{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008241 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008242}
Hanno Becker947194e2017-04-07 13:25:49 +01008243#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008244
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008245#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008247{
Hanno Becker947194e2017-04-07 13:25:49 +01008248 /* Initialize to suppress unnecessary compiler warning */
8249 size_t hostname_len = 0;
8250
8251 /* Check if new hostname is valid before
8252 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008253 if( hostname != NULL )
8254 {
8255 hostname_len = strlen( hostname );
8256
8257 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8259 }
8260
8261 /* Now it's clear that we will overwrite the old hostname,
8262 * so we can free it safely */
8263
8264 if( ssl->hostname != NULL )
8265 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008266 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008267 mbedtls_free( ssl->hostname );
8268 }
8269
8270 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008271
Paul Bakker5121ce52009-01-03 21:22:43 +00008272 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008273 {
8274 ssl->hostname = NULL;
8275 }
8276 else
8277 {
8278 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008279 if( ssl->hostname == NULL )
8280 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008281
Hanno Becker947194e2017-04-07 13:25:49 +01008282 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008283
Hanno Becker947194e2017-04-07 13:25:49 +01008284 ssl->hostname[hostname_len] = '\0';
8285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008286
8287 return( 0 );
8288}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008289#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008290
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008291#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008292void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008294 const unsigned char *, size_t),
8295 void *p_sni )
8296{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008297 conf->f_sni = f_sni;
8298 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008299}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008303int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008304{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008305 size_t cur_len, tot_len;
8306 const char **p;
8307
8308 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008309 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8310 * MUST NOT be truncated."
8311 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008312 */
8313 tot_len = 0;
8314 for( p = protos; *p != NULL; p++ )
8315 {
8316 cur_len = strlen( *p );
8317 tot_len += cur_len;
8318
8319 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008321 }
8322
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008323 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008324
8325 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008326}
8327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008329{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008330 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008331}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008333
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008334void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008335{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008336 conf->max_major_ver = major;
8337 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008338}
8339
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008340void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008341{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008342 conf->min_major_ver = major;
8343 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008344}
8345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008347void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008348{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008349 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008350}
8351#endif
8352
Janos Follath088ce432017-04-10 12:42:31 +01008353#if defined(MBEDTLS_SSL_SRV_C)
8354void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8355 char cert_req_ca_list )
8356{
8357 conf->cert_req_ca_list = cert_req_ca_list;
8358}
8359#endif
8360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008362void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008363{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008364 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008365}
8366#endif
8367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008369void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008370{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008371 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008372}
8373#endif
8374
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008375#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008376void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008377{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008378 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008379}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008380#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008383int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008384{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008386 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008389 }
8390
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008391 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008392
8393 return( 0 );
8394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008398void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008400 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008405void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008406{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008407 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008408}
8409#endif
8410
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008411void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008412{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008413 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008414}
8415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008417void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008418{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008419 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008420}
8421
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008422void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008423{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008424 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008425}
8426
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008427void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008428 const unsigned char period[8] )
8429{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008430 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008431}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008435#if defined(MBEDTLS_SSL_CLI_C)
8436void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008437{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008438 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008439}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008440#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008441
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008442#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008443void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8444 mbedtls_ssl_ticket_write_t *f_ticket_write,
8445 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8446 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008447{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008448 conf->f_ticket_write = f_ticket_write;
8449 conf->f_ticket_parse = f_ticket_parse;
8450 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008451}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008452#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008454
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008455#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8456void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8457 mbedtls_ssl_export_keys_t *f_export_keys,
8458 void *p_export_keys )
8459{
8460 conf->f_export_keys = f_export_keys;
8461 conf->p_export_keys = p_export_keys;
8462}
8463#endif
8464
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008465#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008466void mbedtls_ssl_conf_async_private_cb(
8467 mbedtls_ssl_config *conf,
8468 mbedtls_ssl_async_sign_t *f_async_sign,
8469 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8470 mbedtls_ssl_async_resume_t *f_async_resume,
8471 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008472 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008473{
8474 conf->f_async_sign_start = f_async_sign;
8475 conf->f_async_decrypt_start = f_async_decrypt;
8476 conf->f_async_resume = f_async_resume;
8477 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008478 conf->p_async_config_data = async_config_data;
8479}
8480
Gilles Peskine8f97af72018-04-26 11:46:10 +02008481void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8482{
8483 return( conf->p_async_config_data );
8484}
8485
Gilles Peskine1febfef2018-04-30 11:54:39 +02008486void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008487{
8488 if( ssl->handshake == NULL )
8489 return( NULL );
8490 else
8491 return( ssl->handshake->user_async_ctx );
8492}
8493
Gilles Peskine1febfef2018-04-30 11:54:39 +02008494void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008495 void *ctx )
8496{
8497 if( ssl->handshake != NULL )
8498 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008499}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008500#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008501
Paul Bakker5121ce52009-01-03 21:22:43 +00008502/*
8503 * SSL get accessors
8504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008506{
8507 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8508}
8509
Hanno Becker8b170a02017-10-10 11:51:19 +01008510int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8511{
8512 /*
8513 * Case A: We're currently holding back
8514 * a message for further processing.
8515 */
8516
8517 if( ssl->keep_current_message == 1 )
8518 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008520 return( 1 );
8521 }
8522
8523 /*
8524 * Case B: Further records are pending in the current datagram.
8525 */
8526
8527#if defined(MBEDTLS_SSL_PROTO_DTLS)
8528 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8529 ssl->in_left > ssl->next_record_offset )
8530 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008532 return( 1 );
8533 }
8534#endif /* MBEDTLS_SSL_PROTO_DTLS */
8535
8536 /*
8537 * Case C: A handshake message is being processed.
8538 */
8539
Hanno Becker8b170a02017-10-10 11:51:19 +01008540 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8541 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008543 return( 1 );
8544 }
8545
8546 /*
8547 * Case D: An application data message is being processed
8548 */
8549 if( ssl->in_offt != NULL )
8550 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008552 return( 1 );
8553 }
8554
8555 /*
8556 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008557 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008558 * we implement support for multiple alerts in single records.
8559 */
8560
8561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8562 return( 0 );
8563}
8564
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008565uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008566{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008567 if( ssl->session != NULL )
8568 return( ssl->session->verify_result );
8569
8570 if( ssl->session_negotiate != NULL )
8571 return( ssl->session_negotiate->verify_result );
8572
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008573 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008574}
8575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008577{
Paul Bakker926c8e42013-03-06 10:23:34 +01008578 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008579 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008582}
8583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008587 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008588 {
8589 switch( ssl->minor_ver )
8590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008592 return( "DTLSv1.0" );
8593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008595 return( "DTLSv1.2" );
8596
8597 default:
8598 return( "unknown (DTLS)" );
8599 }
8600 }
8601#endif
8602
Paul Bakker43ca69c2011-01-15 17:35:19 +00008603 switch( ssl->minor_ver )
8604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008606 return( "SSLv3.0" );
8607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008609 return( "TLSv1.0" );
8610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008612 return( "TLSv1.1" );
8613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008615 return( "TLSv1.2" );
8616
Paul Bakker43ca69c2011-01-15 17:35:19 +00008617 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008618 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008619 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008620}
8621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008623{
Hanno Becker3136ede2018-08-17 15:28:19 +01008624 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008626 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008627
Hanno Becker43395762019-05-03 14:46:38 +01008628 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8629
Hanno Becker78640902018-08-13 16:35:15 +01008630 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008631 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633#if defined(MBEDTLS_ZLIB_SUPPORT)
8634 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8635 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008636#endif
8637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640 case MBEDTLS_MODE_GCM:
8641 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008642 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008644 transform_expansion = transform->minlen;
8645 break;
8646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008648
8649 block_size = mbedtls_cipher_get_block_size(
8650 &transform->cipher_ctx_enc );
8651
Hanno Becker3136ede2018-08-17 15:28:19 +01008652 /* Expansion due to the addition of the MAC. */
8653 transform_expansion += transform->maclen;
8654
8655 /* Expansion due to the addition of CBC padding;
8656 * Theoretically up to 256 bytes, but we never use
8657 * more than the block size of the underlying cipher. */
8658 transform_expansion += block_size;
8659
8660 /* For TLS 1.1 or higher, an explicit IV is added
8661 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008662#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8663 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008664 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008665#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008666
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008667 break;
8668
8669 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008672 }
8673
Hanno Beckera5a2b082019-05-15 14:03:01 +01008674#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008675 if( transform->out_cid_len != 0 )
8676 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008677#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008678
Hanno Becker43395762019-05-03 14:46:38 +01008679 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008680}
8681
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008682#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8683size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8684{
8685 size_t max_len;
8686
8687 /*
8688 * Assume mfl_code is correct since it was checked when set
8689 */
Angus Grattond8213d02016-05-25 20:56:48 +10008690 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008691
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008692 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008693 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008694 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008695 {
Angus Grattond8213d02016-05-25 20:56:48 +10008696 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008697 }
8698
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008699 /* During a handshake, use the value being negotiated */
8700 if( ssl->session_negotiate != NULL &&
8701 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8702 {
8703 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8704 }
8705
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008706 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008707}
8708#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8709
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008710#if defined(MBEDTLS_SSL_PROTO_DTLS)
8711static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8712{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008713 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8714 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8715 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8716 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8717 return ( 0 );
8718
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008719 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8720 return( ssl->mtu );
8721
8722 if( ssl->mtu == 0 )
8723 return( ssl->handshake->mtu );
8724
8725 return( ssl->mtu < ssl->handshake->mtu ?
8726 ssl->mtu : ssl->handshake->mtu );
8727}
8728#endif /* MBEDTLS_SSL_PROTO_DTLS */
8729
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008730int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8731{
8732 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8733
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008734#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8735 !defined(MBEDTLS_SSL_PROTO_DTLS)
8736 (void) ssl;
8737#endif
8738
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008739#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8740 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8741
8742 if( max_len > mfl )
8743 max_len = mfl;
8744#endif
8745
8746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008747 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008748 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008749 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008750 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8751 const size_t overhead = (size_t) ret;
8752
8753 if( ret < 0 )
8754 return( ret );
8755
8756 if( mtu <= overhead )
8757 {
8758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8759 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8760 }
8761
8762 if( max_len > mtu - overhead )
8763 max_len = mtu - overhead;
8764 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008765#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008766
Hanno Becker0defedb2018-08-10 12:35:02 +01008767#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8768 !defined(MBEDTLS_SSL_PROTO_DTLS)
8769 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008770#endif
8771
8772 return( (int) max_len );
8773}
8774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775#if defined(MBEDTLS_X509_CRT_PARSE_C)
8776const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008777{
8778 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008779 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008780
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008781 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785#if defined(MBEDTLS_SSL_CLI_C)
8786int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008787{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008788 if( ssl == NULL ||
8789 dst == NULL ||
8790 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008791 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008794 }
8795
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008796 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008797}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008799
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008800const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8801{
8802 if( ssl == NULL )
8803 return( NULL );
8804
8805 return( ssl->session );
8806}
8807
Paul Bakker5121ce52009-01-03 21:22:43 +00008808/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008809 * Define ticket header determining Mbed TLS version
8810 * and structure of the ticket.
8811 */
8812
Hanno Becker41527622019-05-16 12:50:45 +01008813/*
Hanno Becker26829e92019-05-28 14:30:45 +01008814 * Define bitflag determining compile-time settings influencing
8815 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008816 */
8817
Hanno Becker26829e92019-05-28 14:30:45 +01008818#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008819#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008820#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008821#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008822#endif /* MBEDTLS_HAVE_TIME */
8823
8824#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008825#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008826#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008827#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008828#endif /* MBEDTLS_X509_CRT_PARSE_C */
8829
8830#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008831#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008832#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008833#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008834#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8835
8836#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008837#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008838#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008839#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008840#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8841
8842#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008843#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008844#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008845#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008846#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8847
8848#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008849#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008850#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008851#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008852#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8853
Hanno Becker41527622019-05-16 12:50:45 +01008854#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8855#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8856#else
8857#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8858#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8859
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008860#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8861#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8862#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8863#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8864#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8865#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8866#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008867
Hanno Becker26829e92019-05-28 14:30:45 +01008868#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008869 ( (uint16_t) ( \
8870 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8871 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8872 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8873 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8874 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8875 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008876 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008877
Hanno Becker557fe9f2019-05-16 12:41:07 +01008878static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008879 MBEDTLS_VERSION_MAJOR,
8880 MBEDTLS_VERSION_MINOR,
8881 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008882 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8883 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008884};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008885
8886/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008887 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008888 * (in the presentation language of TLS, RFC 8446 section 3)
8889 *
Hanno Becker26829e92019-05-28 14:30:45 +01008890 * opaque mbedtls_version[3]; // major, minor, patch
8891 * opaque session_format[2]; // version-specific 16-bit field determining
8892 * // the format of the remaining
8893 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008894 *
8895 * Note: When updating the format, remember to keep
8896 * these version+format bytes.
8897 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008898 * // In this version, `session_format` determines
8899 * // the setting of those compile-time
8900 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008901 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008902 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008903 * uint8 ciphersuite[2]; // defined by the standard
8904 * uint8 compression; // 0 or 1
8905 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008906 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008907 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008908 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008909 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8910 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008911 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008912 * uint8 mfl_code; // up to 255 according to standard
8913 * uint8 trunc_hmac; // 0 or 1
8914 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008915 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008916 * The order is the same as in the definition of the structure, except
8917 * verify_result is put before peer_cert so that all mandatory fields come
8918 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008919 */
8920int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8921 unsigned char *buf,
8922 size_t buf_len,
8923 size_t *olen )
8924{
8925 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008926 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008927#if defined(MBEDTLS_HAVE_TIME)
8928 uint64_t start;
8929#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008930#if defined(MBEDTLS_X509_CRT_PARSE_C)
8931 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008932#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008933
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008934 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008935 * Add version identifier
8936 */
8937
8938 used += sizeof( ssl_serialized_session_header );
8939
8940 if( used <= buf_len )
8941 {
8942 memcpy( p, ssl_serialized_session_header,
8943 sizeof( ssl_serialized_session_header ) );
8944 p += sizeof( ssl_serialized_session_header );
8945 }
8946
8947 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008948 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008949 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008950#if defined(MBEDTLS_HAVE_TIME)
8951 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008952
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008953 if( used <= buf_len )
8954 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008955 start = (uint64_t) session->start;
8956
8957 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
8958 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
8959 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
8960 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
8961 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
8962 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
8963 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
8964 *p++ = (unsigned char)( ( start ) & 0xFF );
8965 }
8966#endif /* MBEDTLS_HAVE_TIME */
8967
8968 /*
8969 * Basic mandatory fields
8970 */
8971 used += 2 /* ciphersuite */
8972 + 1 /* compression */
8973 + 1 /* id_len */
8974 + sizeof( session->id )
8975 + sizeof( session->master )
8976 + 4; /* verify_result */
8977
8978 if( used <= buf_len )
8979 {
8980 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
8981 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
8982
8983 *p++ = (unsigned char)( session->compression & 0xFF );
8984
8985 *p++ = (unsigned char)( session->id_len & 0xFF );
8986 memcpy( p, session->id, 32 );
8987 p += 32;
8988
8989 memcpy( p, session->master, 48 );
8990 p += 48;
8991
8992 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
8993 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
8994 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
8995 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008996 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008997
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008998 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008999 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009000 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009001#if defined(MBEDTLS_X509_CRT_PARSE_C)
9002 if( session->peer_cert == NULL )
9003 cert_len = 0;
9004 else
9005 cert_len = session->peer_cert->raw.len;
9006
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009007 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009008
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009009 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009010 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009011 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9012 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9013 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9014
9015 if( session->peer_cert != NULL )
9016 {
9017 memcpy( p, session->peer_cert->raw.p, cert_len );
9018 p += cert_len;
9019 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009020 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009021#endif /* MBEDTLS_X509_CRT_PARSE_C */
9022
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009023 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009024 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009025 */
9026#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009027 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009028
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009029 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009030 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009031 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9032 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9033 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9034
9035 if( session->ticket != NULL )
9036 {
9037 memcpy( p, session->ticket, session->ticket_len );
9038 p += session->ticket_len;
9039 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009040
9041 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9042 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9043 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9044 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009045 }
9046#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9047
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009048 /*
9049 * Misc extension-related info
9050 */
9051#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9052 used += 1;
9053
9054 if( used <= buf_len )
9055 *p++ = session->mfl_code;
9056#endif
9057
9058#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9059 used += 1;
9060
9061 if( used <= buf_len )
9062 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9063#endif
9064
9065#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9066 used += 1;
9067
9068 if( used <= buf_len )
9069 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9070#endif
9071
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009072 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009073 *olen = used;
9074
9075 if( used > buf_len )
9076 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009077
9078 return( 0 );
9079}
9080
9081/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009082 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009083 *
9084 * This internal version is wrapped by a public function that cleans up in
9085 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009086 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009087static int ssl_session_load( mbedtls_ssl_session *session,
9088 const unsigned char *buf,
9089 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009090{
9091 const unsigned char *p = buf;
9092 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009093#if defined(MBEDTLS_HAVE_TIME)
9094 uint64_t start;
9095#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009096#if defined(MBEDTLS_X509_CRT_PARSE_C)
9097 size_t cert_len;
9098#endif /* MBEDTLS_X509_CRT_PARSE_C */
9099
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009100 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009101 * Check version identifier
9102 */
9103
9104 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009106
9107 if( memcmp( p, ssl_serialized_session_header,
9108 sizeof( ssl_serialized_session_header ) ) != 0 )
9109 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009110 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009111 }
9112 p += sizeof( ssl_serialized_session_header );
9113
9114 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009115 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009116 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009117#if defined(MBEDTLS_HAVE_TIME)
9118 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9120
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009121 start = ( (uint64_t) p[0] << 56 ) |
9122 ( (uint64_t) p[1] << 48 ) |
9123 ( (uint64_t) p[2] << 40 ) |
9124 ( (uint64_t) p[3] << 32 ) |
9125 ( (uint64_t) p[4] << 24 ) |
9126 ( (uint64_t) p[5] << 16 ) |
9127 ( (uint64_t) p[6] << 8 ) |
9128 ( (uint64_t) p[7] );
9129 p += 8;
9130
9131 session->start = (time_t) start;
9132#endif /* MBEDTLS_HAVE_TIME */
9133
9134 /*
9135 * Basic mandatory fields
9136 */
9137 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9139
9140 session->ciphersuite = ( p[0] << 8 ) | p[1];
9141 p += 2;
9142
9143 session->compression = *p++;
9144
9145 session->id_len = *p++;
9146 memcpy( session->id, p, 32 );
9147 p += 32;
9148
9149 memcpy( session->master, p, 48 );
9150 p += 48;
9151
9152 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9153 ( (uint32_t) p[1] << 16 ) |
9154 ( (uint32_t) p[2] << 8 ) |
9155 ( (uint32_t) p[3] );
9156 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009157
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009158 /* Immediately clear invalid pointer values that have been read, in case
9159 * we exit early before we replaced them with valid ones. */
9160#if defined(MBEDTLS_X509_CRT_PARSE_C)
9161 session->peer_cert = NULL;
9162#endif
9163#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9164 session->ticket = NULL;
9165#endif
9166
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009167 /*
9168 * Peer certificate
9169 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009170#if defined(MBEDTLS_X509_CRT_PARSE_C)
9171 if( 3 > (size_t)( end - p ) )
9172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9173
9174 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9175 p += 3;
9176
9177 if( cert_len == 0 )
9178 {
9179 session->peer_cert = NULL;
9180 }
9181 else
9182 {
9183 int ret;
9184
9185 if( cert_len > (size_t)( end - p ) )
9186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9187
9188 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9189
9190 if( session->peer_cert == NULL )
9191 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9192
9193 mbedtls_x509_crt_init( session->peer_cert );
9194
9195 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9196 p, cert_len ) ) != 0 )
9197 {
9198 mbedtls_x509_crt_free( session->peer_cert );
9199 mbedtls_free( session->peer_cert );
9200 session->peer_cert = NULL;
9201 return( ret );
9202 }
9203
9204 p += cert_len;
9205 }
9206#endif /* MBEDTLS_X509_CRT_PARSE_C */
9207
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009208 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009209 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009210 */
9211#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9212 if( 3 > (size_t)( end - p ) )
9213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9214
9215 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9216 p += 3;
9217
9218 if( session->ticket_len != 0 )
9219 {
9220 if( session->ticket_len > (size_t)( end - p ) )
9221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9222
9223 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9224 if( session->ticket == NULL )
9225 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9226
9227 memcpy( session->ticket, p, session->ticket_len );
9228 p += session->ticket_len;
9229 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009230
9231 if( 4 > (size_t)( end - p ) )
9232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9233
9234 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9235 ( (uint32_t) p[1] << 16 ) |
9236 ( (uint32_t) p[2] << 8 ) |
9237 ( (uint32_t) p[3] );
9238 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009239#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9240
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009241 /*
9242 * Misc extension-related info
9243 */
9244#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9245 if( 1 > (size_t)( end - p ) )
9246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9247
9248 session->mfl_code = *p++;
9249#endif
9250
9251#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9252 if( 1 > (size_t)( end - p ) )
9253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9254
9255 session->trunc_hmac = *p++;
9256#endif
9257
9258#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9259 if( 1 > (size_t)( end - p ) )
9260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9261
9262 session->encrypt_then_mac = *p++;
9263#endif
9264
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009265 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009266 if( p != end )
9267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9268
9269 return( 0 );
9270}
9271
9272/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009273 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009274 */
9275int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9276 const unsigned char *buf,
9277 size_t len )
9278{
9279 int ret = ssl_session_load( session, buf, len );
9280
9281 if( ret != 0 )
9282 mbedtls_ssl_session_free( session );
9283
9284 return( ret );
9285}
9286
9287/*
Paul Bakker1961b702013-01-25 14:49:24 +01009288 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009293
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009294 if( ssl == NULL || ssl->conf == NULL )
9295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009298 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009302 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009304#endif
9305
Paul Bakker1961b702013-01-25 14:49:24 +01009306 return( ret );
9307}
9308
9309/*
9310 * Perform the SSL handshake
9311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009313{
9314 int ret = 0;
9315
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009316 if( ssl == NULL || ssl->conf == NULL )
9317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009324
9325 if( ret != 0 )
9326 break;
9327 }
9328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009330
9331 return( ret );
9332}
9333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334#if defined(MBEDTLS_SSL_RENEGOTIATION)
9335#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009336/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009337 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009340{
9341 int ret;
9342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009344
9345 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9347 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009348
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009349 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009350 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009352 return( ret );
9353 }
9354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009356
9357 return( 0 );
9358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009360
9361/*
9362 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363 * - any side: calling mbedtls_ssl_renegotiate(),
9364 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9365 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009366 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009367 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009371{
9372 int ret;
9373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009375
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009376 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9377 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009378
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009379 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9380 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009382 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009384 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009385 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009386 ssl->handshake->out_msg_seq = 1;
9387 else
9388 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009389 }
9390#endif
9391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9393 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009398 return( ret );
9399 }
9400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009402
9403 return( 0 );
9404}
9405
9406/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009407 * Renegotiate current connection on client,
9408 * or request renegotiation on server
9409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009411{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009413
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009414 if( ssl == NULL || ssl->conf == NULL )
9415 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009418 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009419 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009425
9426 /* Did we already try/start sending HelloRequest? */
9427 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009429
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009430 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009431 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009435 /*
9436 * On client, either start the renegotiation process or,
9437 * if already in progress, continue the handshake
9438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009443
9444 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009447 return( ret );
9448 }
9449 }
9450 else
9451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009455 return( ret );
9456 }
9457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009459
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009460 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009461}
9462
9463/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009464 * Check record counters and renegotiate if they're above the limit.
9465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009467{
Andres AG2196c7f2016-12-15 17:01:16 +00009468 size_t ep_len = ssl_ep_len( ssl );
9469 int in_ctr_cmp;
9470 int out_ctr_cmp;
9471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9473 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009474 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009475 {
9476 return( 0 );
9477 }
9478
Andres AG2196c7f2016-12-15 17:01:16 +00009479 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9480 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009481 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009482 ssl->conf->renego_period + ep_len, 8 - ep_len );
9483
9484 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009485 {
9486 return( 0 );
9487 }
9488
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009493
9494/*
9495 * Receive application data decrypted from the SSL layer
9496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009498{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009499 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009500 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009501
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009502 if( ssl == NULL || ssl->conf == NULL )
9503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009508 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009511 return( ret );
9512
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009513 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009515 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009516 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009517 return( ret );
9518 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009519 }
9520#endif
9521
Hanno Becker4a810fb2017-05-24 16:27:30 +01009522 /*
9523 * Check if renegotiation is necessary and/or handshake is
9524 * in process. If yes, perform/continue, and fall through
9525 * if an unexpected packet is received while the client
9526 * is waiting for the ServerHello.
9527 *
9528 * (There is no equivalent to the last condition on
9529 * the server-side as it is not treated as within
9530 * a handshake while waiting for the ClientHello
9531 * after a renegotiation request.)
9532 */
9533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009535 ret = ssl_check_ctr_renegotiate( ssl );
9536 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9537 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009540 return( ret );
9541 }
9542#endif
9543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009547 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9548 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009551 return( ret );
9552 }
9553 }
9554
Hanno Beckere41158b2017-10-23 13:30:32 +01009555 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009556 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009557 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009558 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009559 if( ssl->f_get_timer != NULL &&
9560 ssl->f_get_timer( ssl->p_timer ) == -1 )
9561 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009562 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009563 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009564
Hanno Becker327c93b2018-08-15 13:56:18 +01009565 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009566 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009567 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9568 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009569
Hanno Becker4a810fb2017-05-24 16:27:30 +01009570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9571 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009572 }
9573
9574 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009576 {
9577 /*
9578 * OpenSSL sends empty messages to randomize the IV
9579 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009580 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009583 return( 0 );
9584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009586 return( ret );
9587 }
9588 }
9589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009593
Hanno Becker4a810fb2017-05-24 16:27:30 +01009594 /*
9595 * - For client-side, expect SERVER_HELLO_REQUEST.
9596 * - For server-side, expect CLIENT_HELLO.
9597 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9598 */
9599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009601 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009603 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009606
9607 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009610 {
9611 continue;
9612 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009615 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009616#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009617
Hanno Becker4a810fb2017-05-24 16:27:30 +01009618#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009619 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009623
9624 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009626 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009627 {
9628 continue;
9629 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009630#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009632 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009633#endif /* MBEDTLS_SSL_SRV_C */
9634
Hanno Becker21df7f92017-10-17 11:03:26 +01009635#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009636 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009637 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9638 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9639 ssl->conf->allow_legacy_renegotiation ==
9640 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9641 {
9642 /*
9643 * Accept renegotiation request
9644 */
Paul Bakker48916f92012-09-16 19:57:18 +00009645
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009646 /* DTLS clients need to know renego is server-initiated */
9647#if defined(MBEDTLS_SSL_PROTO_DTLS)
9648 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9649 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9650 {
9651 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9652 }
9653#endif
9654 ret = ssl_start_renegotiation( ssl );
9655 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9656 ret != 0 )
9657 {
9658 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9659 return( ret );
9660 }
9661 }
9662 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009663#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009664 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009665 /*
9666 * Refuse renegotiation
9667 */
9668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671#if defined(MBEDTLS_SSL_PROTO_SSL3)
9672 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009673 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009674 /* SSLv3 does not have a "no_renegotiation" warning, so
9675 we send a fatal alert and abort the connection. */
9676 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9677 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9678 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009679 }
9680 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9682#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9683 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9684 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9687 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9688 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009689 {
9690 return( ret );
9691 }
Paul Bakker48916f92012-09-16 19:57:18 +00009692 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009693 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9695 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009699 }
Paul Bakker48916f92012-09-16 19:57:18 +00009700 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009701
Hanno Becker90333da2017-10-10 11:27:13 +01009702 /* At this point, we don't know whether the renegotiation has been
9703 * completed or not. The cases to consider are the following:
9704 * 1) The renegotiation is complete. In this case, no new record
9705 * has been read yet.
9706 * 2) The renegotiation is incomplete because the client received
9707 * an application data record while awaiting the ServerHello.
9708 * 3) The renegotiation is incomplete because the client received
9709 * a non-handshake, non-application data message while awaiting
9710 * the ServerHello.
9711 * In each of these case, looping will be the proper action:
9712 * - For 1), the next iteration will read a new record and check
9713 * if it's application data.
9714 * - For 2), the loop condition isn't satisfied as application data
9715 * is present, hence continue is the same as break
9716 * - For 3), the loop condition is satisfied and read_record
9717 * will re-deliver the message that was held back by the client
9718 * when expecting the ServerHello.
9719 */
9720 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009721 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009722#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009724 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009725 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009726 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009727 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009730 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009732 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009733 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009734 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009735#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9738 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009741 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009742 }
9743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9747 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009748 }
9749
9750 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009751
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009752 /* We're going to return something now, cancel timer,
9753 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009755 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009756
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009757#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009758 /* If we requested renego but received AppData, resend HelloRequest.
9759 * Do it now, after setting in_offt, to avoid taking this branch
9760 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009762 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009764 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009765 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009768 return( ret );
9769 }
9770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009772#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009773 }
9774
9775 n = ( len < ssl->in_msglen )
9776 ? len : ssl->in_msglen;
9777
9778 memcpy( buf, ssl->in_offt, n );
9779 ssl->in_msglen -= n;
9780
9781 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009782 {
9783 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009784 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009785 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009786 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009787 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009788 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009789 /* more data available */
9790 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009791 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009794
Paul Bakker23986e52011-04-24 08:57:21 +00009795 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009796}
9797
9798/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009799 * Send application data to be encrypted by the SSL layer, taking care of max
9800 * fragment length and buffer size.
9801 *
9802 * According to RFC 5246 Section 6.2.1:
9803 *
9804 * Zero-length fragments of Application data MAY be sent as they are
9805 * potentially useful as a traffic analysis countermeasure.
9806 *
9807 * Therefore, it is possible that the input message length is 0 and the
9808 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009809 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009810static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009811 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009812{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009813 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9814 const size_t max_len = (size_t) ret;
9815
9816 if( ret < 0 )
9817 {
9818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9819 return( ret );
9820 }
9821
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009822 if( len > max_len )
9823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009825 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009828 "maximum fragment length: %d > %d",
9829 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009831 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009832 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009833#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009834#if defined(MBEDTLS_SSL_PROTO_TLS)
9835 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009836 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009837 }
9838#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009839 }
Paul Bakker887bd502011-06-08 13:10:54 +00009840
Paul Bakker5121ce52009-01-03 21:22:43 +00009841 if( ssl->out_left != 0 )
9842 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009843 /*
9844 * The user has previously tried to send the data and
9845 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9846 * written. In this case, we expect the high-level write function
9847 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009852 return( ret );
9853 }
9854 }
Paul Bakker887bd502011-06-08 13:10:54 +00009855 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009856 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009857 /*
9858 * The user is trying to send a message the first time, so we need to
9859 * copy the data into the internal buffers and setup the data structure
9860 * to keep track of partial writes
9861 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009862 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009864 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009865
Hanno Becker67bc7c32018-08-06 11:33:50 +01009866 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009869 return( ret );
9870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009871 }
9872
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009873 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009874}
9875
9876/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009877 * Write application data, doing 1/n-1 splitting if necessary.
9878 *
9879 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009880 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009881 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009884static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009885 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009886{
9887 int ret;
9888
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009889 if( ssl->conf->cbc_record_splitting ==
9890 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009891 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9893 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9894 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009895 {
9896 return( ssl_write_real( ssl, buf, len ) );
9897 }
9898
9899 if( ssl->split_done == 0 )
9900 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009901 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009902 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009903 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009904 }
9905
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009906 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9907 return( ret );
9908 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009909
9910 return( ret + 1 );
9911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009913
9914/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009915 * Write application data (public-facing wrapper)
9916 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009917int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009918{
9919 int ret;
9920
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009922
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009923 if( ssl == NULL || ssl->conf == NULL )
9924 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9925
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009926#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009927 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9928 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009929 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009930 return( ret );
9931 }
9932#endif
9933
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009934 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009935 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009936 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009937 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009939 return( ret );
9940 }
9941 }
9942
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009943#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009944 ret = ssl_write_split( ssl, buf, len );
9945#else
9946 ret = ssl_write_real( ssl, buf, len );
9947#endif
9948
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009950
9951 return( ret );
9952}
9953
9954/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009955 * Notify the peer that the connection is being closed
9956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009958{
9959 int ret;
9960
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009961 if( ssl == NULL || ssl->conf == NULL )
9962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009965
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009966 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9972 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9973 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009976 return( ret );
9977 }
9978 }
9979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009981
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009982 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009983}
9984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009985void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009986{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009987 if( transform == NULL )
9988 return;
9989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009991 deflateEnd( &transform->ctx_deflate );
9992 inflateEnd( &transform->ctx_inflate );
9993#endif
9994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9996 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009997
Hanno Becker92231322018-01-03 15:32:51 +00009998#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009999 mbedtls_md_free( &transform->md_ctx_enc );
10000 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010001#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010002
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010003 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010004}
10005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006#if defined(MBEDTLS_X509_CRT_PARSE_C)
10007static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010010
10011 while( cur != NULL )
10012 {
10013 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010014 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010015 cur = next;
10016 }
10017}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010018#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010019
Hanno Becker0271f962018-08-16 13:23:47 +010010020#if defined(MBEDTLS_SSL_PROTO_DTLS)
10021
10022static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10023{
10024 unsigned offset;
10025 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10026
10027 if( hs == NULL )
10028 return;
10029
Hanno Becker283f5ef2018-08-24 09:34:47 +010010030 ssl_free_buffered_record( ssl );
10031
Hanno Becker0271f962018-08-16 13:23:47 +010010032 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010033 ssl_buffering_free_slot( ssl, offset );
10034}
10035
10036static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10037 uint8_t slot )
10038{
10039 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10040 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010041
10042 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10043 return;
10044
Hanno Beckere605b192018-08-21 15:59:07 +010010045 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010046 {
Hanno Beckere605b192018-08-21 15:59:07 +010010047 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010048 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010049 mbedtls_free( hs_buf->data );
10050 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010051 }
10052}
10053
10054#endif /* MBEDTLS_SSL_PROTO_DTLS */
10055
Gilles Peskine9b562d52018-04-25 20:32:43 +020010056void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010057{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010058 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10059
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010060 if( handshake == NULL )
10061 return;
10062
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010063#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10064 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10065 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010066 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010067 handshake->async_in_progress = 0;
10068 }
10069#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10070
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010071#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10072 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10073 mbedtls_md5_free( &handshake->fin_md5 );
10074 mbedtls_sha1_free( &handshake->fin_sha1 );
10075#endif
10076#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10077#if defined(MBEDTLS_SHA256_C)
10078 mbedtls_sha256_free( &handshake->fin_sha256 );
10079#endif
10080#if defined(MBEDTLS_SHA512_C)
10081 mbedtls_sha512_free( &handshake->fin_sha512 );
10082#endif
10083#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010085#if defined(MBEDTLS_DHM_C)
10086 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088#if defined(MBEDTLS_ECDH_C)
10089 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010090#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010091#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010092 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010093#if defined(MBEDTLS_SSL_CLI_C)
10094 mbedtls_free( handshake->ecjpake_cache );
10095 handshake->ecjpake_cache = NULL;
10096 handshake->ecjpake_cache_len = 0;
10097#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010098#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010099
Janos Follath4ae5c292016-02-10 11:27:43 +000010100#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10101 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010102 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010104#endif
10105
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010106#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10107 if( handshake->psk != NULL )
10108 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010109 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010110 mbedtls_free( handshake->psk );
10111 }
10112#endif
10113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010114#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10115 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010116 /*
10117 * Free only the linked list wrapper, not the keys themselves
10118 * since the belong to the SNI callback
10119 */
10120 if( handshake->sni_key_cert != NULL )
10121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010123
10124 while( cur != NULL )
10125 {
10126 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010127 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010128 cur = next;
10129 }
10130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010131#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010132
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010133#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010134 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010135#endif
10136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137#if defined(MBEDTLS_SSL_PROTO_DTLS)
10138 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010139 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010140 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010141#endif
10142
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010143 mbedtls_platform_zeroize( handshake,
10144 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010145}
10146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010147void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010148{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010149 if( session == NULL )
10150 return;
10151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010152#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000010153 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +000010154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010155 mbedtls_x509_crt_free( session->peer_cert );
10156 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +000010157 }
Paul Bakkered27a042013-04-18 22:46:23 +020010158#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010159
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010160#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010161 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010162#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010163
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010164 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010165}
10166
Paul Bakker5121ce52009-01-03 21:22:43 +000010167/*
10168 * Free an SSL context
10169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010171{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010172 if( ssl == NULL )
10173 return;
10174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010176
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010177 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010178 {
Angus Grattond8213d02016-05-25 20:56:48 +100010179 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010181 }
10182
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010183 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010184 {
Angus Grattond8213d02016-05-25 20:56:48 +100010185 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010187 }
10188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010189#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010190 if( ssl->compress_buf != NULL )
10191 {
Angus Grattond8213d02016-05-25 20:56:48 +100010192 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010194 }
10195#endif
10196
Paul Bakker48916f92012-09-16 19:57:18 +000010197 if( ssl->transform )
10198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 mbedtls_ssl_transform_free( ssl->transform );
10200 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010201 }
10202
10203 if( ssl->handshake )
10204 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010205 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10207 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 mbedtls_free( ssl->handshake );
10210 mbedtls_free( ssl->transform_negotiate );
10211 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010212 }
10213
Paul Bakkerc0463502013-02-14 11:19:38 +010010214 if( ssl->session )
10215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 mbedtls_ssl_session_free( ssl->session );
10217 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010218 }
10219
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010220#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010221 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010222 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010223 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010225 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010226#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10229 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10232 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010233 }
10234#endif
10235
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010236#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010238#endif
10239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010241
Paul Bakker86f04f42013-02-14 11:20:09 +010010242 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010243 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010244}
10245
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010246/*
10247 * Initialze mbedtls_ssl_config
10248 */
10249void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10250{
10251 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010252
10253#if !defined(MBEDTLS_SSL_PROTO_TLS)
10254 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10255#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010256}
10257
Simon Butcherc97b6972015-12-27 23:48:17 +000010258#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010259static int ssl_preset_default_hashes[] = {
10260#if defined(MBEDTLS_SHA512_C)
10261 MBEDTLS_MD_SHA512,
10262 MBEDTLS_MD_SHA384,
10263#endif
10264#if defined(MBEDTLS_SHA256_C)
10265 MBEDTLS_MD_SHA256,
10266 MBEDTLS_MD_SHA224,
10267#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010268#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010269 MBEDTLS_MD_SHA1,
10270#endif
10271 MBEDTLS_MD_NONE
10272};
Simon Butcherc97b6972015-12-27 23:48:17 +000010273#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010274
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010275static int ssl_preset_suiteb_ciphersuites[] = {
10276 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10277 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10278 0
10279};
10280
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010281#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010282static int ssl_preset_suiteb_hashes[] = {
10283 MBEDTLS_MD_SHA256,
10284 MBEDTLS_MD_SHA384,
10285 MBEDTLS_MD_NONE
10286};
10287#endif
10288
10289#if defined(MBEDTLS_ECP_C)
10290static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10291 MBEDTLS_ECP_DP_SECP256R1,
10292 MBEDTLS_ECP_DP_SECP384R1,
10293 MBEDTLS_ECP_DP_NONE
10294};
10295#endif
10296
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010297/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010298 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010299 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010300int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010301 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010302{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010303#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010304 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010305#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010306
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010307 /* Use the functions here so that they are covered in tests,
10308 * but otherwise access member directly for efficiency */
10309 mbedtls_ssl_conf_endpoint( conf, endpoint );
10310 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010311
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010312 /*
10313 * Things that are common to all presets
10314 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010315#if defined(MBEDTLS_SSL_CLI_C)
10316 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10317 {
10318 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10319#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10320 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10321#endif
10322 }
10323#endif
10324
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010325#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010326 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010327#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010328
10329#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10330 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10331#endif
10332
10333#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10334 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10335#endif
10336
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010337#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10338 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10339#endif
10340
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010341#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010342 conf->f_cookie_write = ssl_cookie_write_dummy;
10343 conf->f_cookie_check = ssl_cookie_check_dummy;
10344#endif
10345
10346#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10347 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10348#endif
10349
Janos Follath088ce432017-04-10 12:42:31 +010010350#if defined(MBEDTLS_SSL_SRV_C)
10351 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10352#endif
10353
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010354#if defined(MBEDTLS_SSL_PROTO_DTLS)
10355 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10356 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10357#endif
10358
10359#if defined(MBEDTLS_SSL_RENEGOTIATION)
10360 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010361 memset( conf->renego_period, 0x00, 2 );
10362 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010363#endif
10364
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010365#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10366 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10367 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010368 const unsigned char dhm_p[] =
10369 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10370 const unsigned char dhm_g[] =
10371 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10372
Hanno Beckera90658f2017-10-04 15:29:08 +010010373 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10374 dhm_p, sizeof( dhm_p ),
10375 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010376 {
10377 return( ret );
10378 }
10379 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010380#endif
10381
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010382 /*
10383 * Preset-specific defaults
10384 */
10385 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010386 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010387 /*
10388 * NSA Suite B
10389 */
10390 case MBEDTLS_SSL_PRESET_SUITEB:
10391 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10392 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10393 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10394 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10395
10396 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10397 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10398 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10399 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10400 ssl_preset_suiteb_ciphersuites;
10401
10402#if defined(MBEDTLS_X509_CRT_PARSE_C)
10403 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010404#endif
10405
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010406#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010407 conf->sig_hashes = ssl_preset_suiteb_hashes;
10408#endif
10409
10410#if defined(MBEDTLS_ECP_C)
10411 conf->curve_list = ssl_preset_suiteb_curves;
10412#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010413 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010414
10415 /*
10416 * Default
10417 */
10418 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010419 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10420 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10421 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10422 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10423 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10424 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10425 MBEDTLS_SSL_MIN_MINOR_VERSION :
10426 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010427 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10428 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10429
10430#if defined(MBEDTLS_SSL_PROTO_DTLS)
10431 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10432 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10433#endif
10434
10435 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10436 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10437 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10438 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10439 mbedtls_ssl_list_ciphersuites();
10440
10441#if defined(MBEDTLS_X509_CRT_PARSE_C)
10442 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10443#endif
10444
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010445#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010446 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010447#endif
10448
10449#if defined(MBEDTLS_ECP_C)
10450 conf->curve_list = mbedtls_ecp_grp_id_list();
10451#endif
10452
10453#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10454 conf->dhm_min_bitlen = 1024;
10455#endif
10456 }
10457
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010458 return( 0 );
10459}
10460
10461/*
10462 * Free mbedtls_ssl_config
10463 */
10464void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10465{
10466#if defined(MBEDTLS_DHM_C)
10467 mbedtls_mpi_free( &conf->dhm_P );
10468 mbedtls_mpi_free( &conf->dhm_G );
10469#endif
10470
10471#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10472 if( conf->psk != NULL )
10473 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010474 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010475 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010476 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010477 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010478 }
10479
10480 if( conf->psk_identity != NULL )
10481 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010482 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010483 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010484 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010485 conf->psk_identity_len = 0;
10486 }
10487#endif
10488
10489#if defined(MBEDTLS_X509_CRT_PARSE_C)
10490 ssl_key_cert_free( conf->key_cert );
10491#endif
10492
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010493 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010494}
10495
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010496#if defined(MBEDTLS_PK_C) && \
10497 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010498/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010499 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010501unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010503#if defined(MBEDTLS_RSA_C)
10504 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10505 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010507#if defined(MBEDTLS_ECDSA_C)
10508 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10509 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010511 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010512}
10513
Hanno Becker7e5437a2017-04-28 17:15:26 +010010514unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10515{
10516 switch( type ) {
10517 case MBEDTLS_PK_RSA:
10518 return( MBEDTLS_SSL_SIG_RSA );
10519 case MBEDTLS_PK_ECDSA:
10520 case MBEDTLS_PK_ECKEY:
10521 return( MBEDTLS_SSL_SIG_ECDSA );
10522 default:
10523 return( MBEDTLS_SSL_SIG_ANON );
10524 }
10525}
10526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010527mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010528{
10529 switch( sig )
10530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010531#if defined(MBEDTLS_RSA_C)
10532 case MBEDTLS_SSL_SIG_RSA:
10533 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010534#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010535#if defined(MBEDTLS_ECDSA_C)
10536 case MBEDTLS_SSL_SIG_ECDSA:
10537 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010538#endif
10539 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010541 }
10542}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010543#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010544
Hanno Becker7e5437a2017-04-28 17:15:26 +010010545#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10546 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10547
10548/* Find an entry in a signature-hash set matching a given hash algorithm. */
10549mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10550 mbedtls_pk_type_t sig_alg )
10551{
10552 switch( sig_alg )
10553 {
10554 case MBEDTLS_PK_RSA:
10555 return( set->rsa );
10556 case MBEDTLS_PK_ECDSA:
10557 return( set->ecdsa );
10558 default:
10559 return( MBEDTLS_MD_NONE );
10560 }
10561}
10562
10563/* Add a signature-hash-pair to a signature-hash set */
10564void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10565 mbedtls_pk_type_t sig_alg,
10566 mbedtls_md_type_t md_alg )
10567{
10568 switch( sig_alg )
10569 {
10570 case MBEDTLS_PK_RSA:
10571 if( set->rsa == MBEDTLS_MD_NONE )
10572 set->rsa = md_alg;
10573 break;
10574
10575 case MBEDTLS_PK_ECDSA:
10576 if( set->ecdsa == MBEDTLS_MD_NONE )
10577 set->ecdsa = md_alg;
10578 break;
10579
10580 default:
10581 break;
10582 }
10583}
10584
10585/* Allow exactly one hash algorithm for each signature. */
10586void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10587 mbedtls_md_type_t md_alg )
10588{
10589 set->rsa = md_alg;
10590 set->ecdsa = md_alg;
10591}
10592
10593#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10594 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10595
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010596/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010597 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010600{
10601 switch( hash )
10602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603#if defined(MBEDTLS_MD5_C)
10604 case MBEDTLS_SSL_HASH_MD5:
10605 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607#if defined(MBEDTLS_SHA1_C)
10608 case MBEDTLS_SSL_HASH_SHA1:
10609 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611#if defined(MBEDTLS_SHA256_C)
10612 case MBEDTLS_SSL_HASH_SHA224:
10613 return( MBEDTLS_MD_SHA224 );
10614 case MBEDTLS_SSL_HASH_SHA256:
10615 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617#if defined(MBEDTLS_SHA512_C)
10618 case MBEDTLS_SSL_HASH_SHA384:
10619 return( MBEDTLS_MD_SHA384 );
10620 case MBEDTLS_SSL_HASH_SHA512:
10621 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010622#endif
10623 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010625 }
10626}
10627
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010628/*
10629 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10630 */
10631unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10632{
10633 switch( md )
10634 {
10635#if defined(MBEDTLS_MD5_C)
10636 case MBEDTLS_MD_MD5:
10637 return( MBEDTLS_SSL_HASH_MD5 );
10638#endif
10639#if defined(MBEDTLS_SHA1_C)
10640 case MBEDTLS_MD_SHA1:
10641 return( MBEDTLS_SSL_HASH_SHA1 );
10642#endif
10643#if defined(MBEDTLS_SHA256_C)
10644 case MBEDTLS_MD_SHA224:
10645 return( MBEDTLS_SSL_HASH_SHA224 );
10646 case MBEDTLS_MD_SHA256:
10647 return( MBEDTLS_SSL_HASH_SHA256 );
10648#endif
10649#if defined(MBEDTLS_SHA512_C)
10650 case MBEDTLS_MD_SHA384:
10651 return( MBEDTLS_SSL_HASH_SHA384 );
10652 case MBEDTLS_MD_SHA512:
10653 return( MBEDTLS_SSL_HASH_SHA512 );
10654#endif
10655 default:
10656 return( MBEDTLS_SSL_HASH_NONE );
10657 }
10658}
10659
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010660#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010661/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010662 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010663 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010664 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010665int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010667 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010668
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010669 if( ssl->conf->curve_list == NULL )
10670 return( -1 );
10671
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010672 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010673 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010674 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010675
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010676 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010677}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010678#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010679
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010680#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010681/*
10682 * Check if a hash proposed by the peer is in our list.
10683 * Return 0 if we're willing to use it, -1 otherwise.
10684 */
10685int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10686 mbedtls_md_type_t md )
10687{
10688 const int *cur;
10689
10690 if( ssl->conf->sig_hashes == NULL )
10691 return( -1 );
10692
10693 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10694 if( *cur == (int) md )
10695 return( 0 );
10696
10697 return( -1 );
10698}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010699#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010701#if defined(MBEDTLS_X509_CRT_PARSE_C)
10702int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10703 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010704 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010705 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010706{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010707 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010708#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010709 int usage = 0;
10710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010712 const char *ext_oid;
10713 size_t ext_len;
10714#endif
10715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10717 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010718 ((void) cert);
10719 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010720 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010721#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010723#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10724 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010725 {
10726 /* Server part of the key exchange */
10727 switch( ciphersuite->key_exchange )
10728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 case MBEDTLS_KEY_EXCHANGE_RSA:
10730 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010731 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010732 break;
10733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10735 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10736 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10737 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010738 break;
10739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10741 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010742 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010743 break;
10744
10745 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746 case MBEDTLS_KEY_EXCHANGE_NONE:
10747 case MBEDTLS_KEY_EXCHANGE_PSK:
10748 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10749 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010750 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010751 usage = 0;
10752 }
10753 }
10754 else
10755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10757 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010758 }
10759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010760 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010761 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010762 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010763 ret = -1;
10764 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010765#else
10766 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10770 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10773 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010774 }
10775 else
10776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010777 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10778 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010779 }
10780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010782 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010783 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010784 ret = -1;
10785 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010786#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010787
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010788 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010791
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010792/*
10793 * Convert version numbers to/from wire format
10794 * and, for DTLS, to/from TLS equivalent.
10795 *
10796 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010797 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010798 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10799 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010802 unsigned char ver[2] )
10803{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010804#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10805 ((void) transport);
10806#endif
10807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010808#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010809 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010811 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010812 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10813
10814 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10815 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10816 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010817 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010818#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010819#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010820 {
10821 ver[0] = (unsigned char) major;
10822 ver[1] = (unsigned char) minor;
10823 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010824#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010825}
10826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010827void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010828 const unsigned char ver[2] )
10829{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010830#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10831 ((void) transport);
10832#endif
10833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010834#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010835 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010836 {
10837 *major = 255 - ver[0] + 2;
10838 *minor = 255 - ver[1] + 1;
10839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010840 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010841 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10842 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010843 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010844#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010845#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010846 {
10847 *major = ver[0];
10848 *minor = ver[1];
10849 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010850#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010851}
10852
Simon Butcher99000142016-10-13 17:21:01 +010010853int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10854{
10855#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10856 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10857 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10858
10859 switch( md )
10860 {
10861#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10862#if defined(MBEDTLS_MD5_C)
10863 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010864 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010865#endif
10866#if defined(MBEDTLS_SHA1_C)
10867 case MBEDTLS_SSL_HASH_SHA1:
10868 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10869 break;
10870#endif
10871#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10872#if defined(MBEDTLS_SHA512_C)
10873 case MBEDTLS_SSL_HASH_SHA384:
10874 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10875 break;
10876#endif
10877#if defined(MBEDTLS_SHA256_C)
10878 case MBEDTLS_SSL_HASH_SHA256:
10879 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10880 break;
10881#endif
10882 default:
10883 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10884 }
10885
10886 return 0;
10887#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10888 (void) ssl;
10889 (void) md;
10890
10891 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10892#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10893}
10894
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010895#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10896 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10897int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10898 unsigned char *output,
10899 unsigned char *data, size_t data_len )
10900{
10901 int ret = 0;
10902 mbedtls_md5_context mbedtls_md5;
10903 mbedtls_sha1_context mbedtls_sha1;
10904
10905 mbedtls_md5_init( &mbedtls_md5 );
10906 mbedtls_sha1_init( &mbedtls_sha1 );
10907
10908 /*
10909 * digitally-signed struct {
10910 * opaque md5_hash[16];
10911 * opaque sha_hash[20];
10912 * };
10913 *
10914 * md5_hash
10915 * MD5(ClientHello.random + ServerHello.random
10916 * + ServerParams);
10917 * sha_hash
10918 * SHA(ClientHello.random + ServerHello.random
10919 * + ServerParams);
10920 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010921 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010922 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010924 goto exit;
10925 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010926 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010927 ssl->handshake->randbytes, 64 ) ) != 0 )
10928 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010930 goto exit;
10931 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010932 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010933 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010935 goto exit;
10936 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010937 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010938 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010940 goto exit;
10941 }
10942
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010943 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010944 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010946 goto exit;
10947 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010948 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010949 ssl->handshake->randbytes, 64 ) ) != 0 )
10950 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_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_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010955 data_len ) ) != 0 )
10956 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010958 goto exit;
10959 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010960 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010961 output + 16 ) ) != 0 )
10962 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010964 goto exit;
10965 }
10966
10967exit:
10968 mbedtls_md5_free( &mbedtls_md5 );
10969 mbedtls_sha1_free( &mbedtls_sha1 );
10970
10971 if( ret != 0 )
10972 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10973 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10974
10975 return( ret );
10976
10977}
10978#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10979 MBEDTLS_SSL_PROTO_TLS1_1 */
10980
10981#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10982 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10983int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010984 unsigned char *hash, size_t *hashlen,
10985 unsigned char *data, size_t data_len,
10986 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010987{
10988 int ret = 0;
10989 mbedtls_md_context_t ctx;
10990 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010991 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010992
10993 mbedtls_md_init( &ctx );
10994
10995 /*
10996 * digitally-signed struct {
10997 * opaque client_random[32];
10998 * opaque server_random[32];
10999 * ServerDHParams params;
11000 * };
11001 */
11002 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11003 {
11004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11005 goto exit;
11006 }
11007 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11008 {
11009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11010 goto exit;
11011 }
11012 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11013 {
11014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11015 goto exit;
11016 }
11017 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11018 {
11019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11020 goto exit;
11021 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011022 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011023 {
11024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11025 goto exit;
11026 }
11027
11028exit:
11029 mbedtls_md_free( &ctx );
11030
11031 if( ret != 0 )
11032 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11033 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11034
11035 return( ret );
11036}
11037#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11038 MBEDTLS_SSL_PROTO_TLS1_2 */
11039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040#endif /* MBEDTLS_SSL_TLS_C */