blob: 247459f12019dd858715a15cae1fed00a5dd71e1 [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 Beckera835da52019-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
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050054#if defined(MBEDTLS_USE_PSA_CRYPTO)
55#include "mbedtls/psa_util.h"
56#include "psa/crypto.h"
57#endif
58
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Hanno Becker2a43f6f2018-08-10 11:12:52 +010063static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010064static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010065
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020070 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010072#else
73 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074#endif
75 return( 0 );
76}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
Hanno Beckercfe45792019-07-03 16:13:00 +0100112#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100113static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
114 unsigned char *buf,
115 size_t len,
116 mbedtls_record *rec );
117
Hanno Beckercfe45792019-07-03 16:13:00 +0100118int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
119 unsigned char *buf,
120 size_t buflen )
121{
Hanno Becker54229812019-07-12 14:40:00 +0100122 int ret = 0;
123 mbedtls_record rec;
124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
125 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
126
127 /* We don't support record checking in TLS because
128 * (a) there doesn't seem to be a usecase for it, and
129 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
130 * and we'd need to backup the transform here.
131 */
132 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
133 {
134 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
135 goto exit;
136 }
137#if defined(MBEDTLS_SSL_PROTO_DTLS)
138 else
139 {
140 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
141 if( ret != 0 )
142 {
143 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
144 goto exit;
145 }
146
147 if( ssl->transform_in != NULL )
148 {
149 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
150 if( ret != 0 )
151 {
152 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
153 goto exit;
154 }
155 }
156 }
157#endif /* MBEDTLS_SSL_PROTO_DTLS */
158
159exit:
160 /* On success, we have decrypted the buffer in-place, so make
161 * sure we don't leak any plaintext data. */
162 mbedtls_platform_zeroize( buf, buflen );
163
164 /* For the purpose of this API, treat messages with unexpected CID
165 * as well as such from future epochs as unexpected. */
166 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
167 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
168 {
169 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
170 }
171
172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
173 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100174}
175#endif /* MBEDTLS_SSL_RECORD_CHECKING */
176
Hanno Becker67bc7c32018-08-06 11:33:50 +0100177#define SSL_DONT_FORCE_FLUSH 0
178#define SSL_FORCE_FLUSH 1
179
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200180#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100181
Hanno Beckera0e20d02019-05-15 14:03:01 +0100182#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100183/* Top-level Connection ID API */
184
Hanno Becker8367ccc2019-05-14 11:30:10 +0100185int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
186 size_t len,
187 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100188{
189 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
191
Hanno Becker611ac772019-05-14 11:45:26 +0100192 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
193 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
194 {
195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
196 }
197
198 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100199 conf->cid_len = len;
200 return( 0 );
201}
202
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100203int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
204 int enable,
205 unsigned char const *own_cid,
206 size_t own_cid_len )
207{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100208 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
210
Hanno Beckerca092242019-04-25 16:01:49 +0100211 ssl->negotiate_cid = enable;
212 if( enable == MBEDTLS_SSL_CID_DISABLED )
213 {
214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
215 return( 0 );
216 }
217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100218 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100219
Hanno Beckerad4a1372019-05-03 13:06:44 +0100220 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100221 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
223 (unsigned) own_cid_len,
224 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
226 }
227
228 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100229 /* Truncation is not an issue here because
230 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
231 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100232
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100233 return( 0 );
234}
235
236int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
237 int *enabled,
238 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
239 size_t *peer_cid_len )
240{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100241 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100242
Hanno Becker76a79ab2019-05-03 14:38:32 +0100243 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
244 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
245 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100247 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100248
Hanno Beckerc5f24222019-05-03 12:54:52 +0100249 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
250 * were used, but client and server requested the empty CID.
251 * This is indistinguishable from not using the CID extension
252 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100253 if( ssl->transform_in->in_cid_len == 0 &&
254 ssl->transform_in->out_cid_len == 0 )
255 {
256 return( 0 );
257 }
258
Hanno Becker615ef172019-05-22 16:50:35 +0100259 if( peer_cid_len != NULL )
260 {
261 *peer_cid_len = ssl->transform_in->out_cid_len;
262 if( peer_cid != NULL )
263 {
264 memcpy( peer_cid, ssl->transform_in->out_cid,
265 ssl->transform_in->out_cid_len );
266 }
267 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100268
269 *enabled = MBEDTLS_SSL_CID_ENABLED;
270
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100271 return( 0 );
272}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100274
Hanno Beckerd5847772018-08-28 10:09:23 +0100275/* Forward declarations for functions related to message buffering. */
276static void ssl_buffering_free( mbedtls_ssl_context *ssl );
277static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
278 uint8_t slot );
279static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
280static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
281static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
282static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100283static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
284 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100285static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100286
Hanno Beckera67dee22018-08-22 10:05:20 +0100287static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100288static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100289{
Hanno Becker11682cc2018-08-22 14:41:02 +0100290 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100291
292 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100293 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100294
295 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
296}
297
Hanno Becker67bc7c32018-08-06 11:33:50 +0100298static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
299{
Hanno Becker11682cc2018-08-22 14:41:02 +0100300 size_t const bytes_written = ssl->out_left;
301 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100302
303 /* Double-check that the write-index hasn't gone
304 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100305 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100306 {
307 /* Should never happen... */
308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
309 }
310
311 return( (int) ( mtu - bytes_written ) );
312}
313
314static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
315{
316 int ret;
317 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400318 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100319
320#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
321 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
322
323 if( max_len > mfl )
324 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100325
326 /* By the standard (RFC 6066 Sect. 4), the MFL extension
327 * only limits the maximum record payload size, so in theory
328 * we would be allowed to pack multiple records of payload size
329 * MFL into a single datagram. However, this would mean that there's
330 * no way to explicitly communicate MTU restrictions to the peer.
331 *
332 * The following reduction of max_len makes sure that we never
333 * write datagrams larger than MFL + Record Expansion Overhead.
334 */
335 if( max_len <= ssl->out_left )
336 return( 0 );
337
338 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100339#endif
340
341 ret = ssl_get_remaining_space_in_datagram( ssl );
342 if( ret < 0 )
343 return( ret );
344 remaining = (size_t) ret;
345
346 ret = mbedtls_ssl_get_record_expansion( ssl );
347 if( ret < 0 )
348 return( ret );
349 expansion = (size_t) ret;
350
351 if( remaining <= expansion )
352 return( 0 );
353
354 remaining -= expansion;
355 if( remaining >= max_len )
356 remaining = max_len;
357
358 return( (int) remaining );
359}
360
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200361/*
362 * Double the retransmit timeout value, within the allowed range,
363 * returning -1 if the maximum value has already been reached.
364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200366{
367 uint32_t new_timeout;
368
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200369 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200370 return( -1 );
371
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200372 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
373 * in the following way: after the initial transmission and a first
374 * retransmission, back off to a temporary estimated MTU of 508 bytes.
375 * This value is guaranteed to be deliverable (if not guaranteed to be
376 * delivered) of any compliant IPv4 (and IPv6) network, and should work
377 * on most non-IP stacks too. */
378 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400379 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200380 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
382 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200383
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200384 new_timeout = 2 * ssl->handshake->retransmit_timeout;
385
386 /* Avoid arithmetic overflow and range overflow */
387 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200388 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200389 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200390 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200391 }
392
393 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200395 ssl->handshake->retransmit_timeout ) );
396
397 return( 0 );
398}
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200401{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200402 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200404 ssl->handshake->retransmit_timeout ) );
405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200409/*
410 * Convert max_fragment_length codes to length.
411 * RFC 6066 says:
412 * enum{
413 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
414 * } MaxFragmentLength;
415 * and we add 0 -> extension unused
416 */
Angus Grattond8213d02016-05-25 20:56:48 +1000417static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200418{
Angus Grattond8213d02016-05-25 20:56:48 +1000419 switch( mfl )
420 {
421 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
422 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
423 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
424 return 512;
425 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
426 return 1024;
427 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
428 return 2048;
429 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
430 return 4096;
431 default:
432 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
433 }
434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200436
Hanno Becker52055ae2019-02-06 14:30:46 +0000437int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
438 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_ssl_session_free( dst );
441 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000444
445#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200446 if( src->peer_cert != NULL )
447 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200448 int ret;
449
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200450 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200451 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200452 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200457 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200460 dst->peer_cert = NULL;
461 return( ret );
462 }
463 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000464#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000465 if( src->peer_cert_digest != NULL )
466 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000467 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000468 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000469 if( dst->peer_cert_digest == NULL )
470 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
471
472 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
473 src->peer_cert_digest_len );
474 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000475 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000476 }
477#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200480
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200481#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200482 if( src->ticket != NULL )
483 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200484 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200485 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200486 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200487
488 memcpy( dst->ticket, src->ticket, src->ticket_len );
489 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200490#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200491
492 return( 0 );
493}
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
496int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200497 const unsigned char *key_enc, const unsigned char *key_dec,
498 size_t keylen,
499 const unsigned char *iv_enc, const unsigned char *iv_dec,
500 size_t ivlen,
501 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200502 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
504int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
505int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
506int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
507int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
508#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000509
Paul Bakker5121ce52009-01-03 21:22:43 +0000510/*
511 * Key material generation
512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200514static int ssl3_prf( const unsigned char *secret, size_t slen,
515 const char *label,
516 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000517 unsigned char *dstbuf, size_t dlen )
518{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100519 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000520 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200521 mbedtls_md5_context md5;
522 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000523 unsigned char padding[16];
524 unsigned char sha1sum[20];
525 ((void)label);
526
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200527 mbedtls_md5_init( &md5 );
528 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200529
Paul Bakker5f70b252012-09-13 14:23:06 +0000530 /*
531 * SSLv3:
532 * block =
533 * MD5( secret + SHA1( 'A' + secret + random ) ) +
534 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
535 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
536 * ...
537 */
538 for( i = 0; i < dlen / 16; i++ )
539 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200540 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000541
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100542 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100543 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100544 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100545 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100546 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100547 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100548 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100549 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100550 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100551 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000552
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100553 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100554 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100555 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100556 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100557 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100558 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100559 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100560 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000561 }
562
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100563exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200564 mbedtls_md5_free( &md5 );
565 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000566
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500567 mbedtls_platform_zeroize( padding, sizeof( padding ) );
568 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000569
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100570 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200575static int tls1_prf( const unsigned char *secret, size_t slen,
576 const char *label,
577 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000578 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000579{
Paul Bakker23986e52011-04-24 08:57:21 +0000580 size_t nb, hs;
581 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200582 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300583 unsigned char *tmp;
584 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 const mbedtls_md_info_t *md_info;
587 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100588 int ret;
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Ron Eldor3b350852019-05-07 18:31:49 +0300592 tmp_len = 20 + strlen( label ) + rlen;
593 tmp = mbedtls_calloc( 1, tmp_len );
594 if( tmp == NULL )
595 {
596 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
597 goto exit;
598 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600 hs = ( slen + 1 ) / 2;
601 S1 = secret;
602 S2 = secret + slen - hs;
603
604 nb = strlen( label );
605 memcpy( tmp + 20, label, nb );
606 memcpy( tmp + 20 + nb, random, rlen );
607 nb += rlen;
608
609 /*
610 * First compute P_md5(secret,label+random)[0..dlen]
611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300613 {
614 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
615 goto exit;
616 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300619 {
620 goto exit;
621 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
624 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
625 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
627 for( i = 0; i < dlen; i += 16 )
628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_hmac_reset ( &md_ctx );
630 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + 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, 4 + tmp, 16 );
635 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
637 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
638
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
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 /*
646 * XOR out with P_sha1(secret,label+random)[0..dlen]
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300649 {
650 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
651 goto exit;
652 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300655 {
656 goto exit;
657 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
660 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
661 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
663 for( i = 0; i < dlen; i += 20 )
664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_md_hmac_reset ( &md_ctx );
666 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
667 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_md_hmac_reset ( &md_ctx );
670 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
671 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
674
675 for( j = 0; j < k; j++ )
676 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
677 }
678
Ron Eldor3b350852019-05-07 18:31:49 +0300679exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100681
Ron Eldor3b350852019-05-07 18:31:49 +0300682 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500683 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
Ron Eldor3b350852019-05-07 18:31:49 +0300685 mbedtls_free( tmp );
686 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500691#if defined(MBEDTLS_USE_PSA_CRYPTO)
692static int tls_prf_generic( mbedtls_md_type_t md_type,
693 const unsigned char *secret, size_t slen,
694 const char *label,
695 const unsigned char *random, size_t rlen,
696 unsigned char *dstbuf, size_t dlen )
697{
698 psa_status_t status;
699 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100700 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500701 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100702 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100703 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500704
Andrzej Kurekc929a822019-01-14 03:51:11 -0500705 if( md_type == MBEDTLS_MD_SHA384 )
706 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
707 else
708 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
709
Janos Follath53b8ec22019-08-08 10:28:27 +0100710 key_attributes = psa_key_attributes_init();
711 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
712 psa_set_key_algorithm( &key_attributes, alg );
713 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500714
Janos Follath53b8ec22019-08-08 10:28:27 +0100715 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500716 if( status != PSA_SUCCESS )
717 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
718
Janos Follathda6ac012019-08-16 13:47:29 +0100719 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500720 master_slot, alg,
721 random, rlen,
722 (unsigned char const *) label,
723 (size_t) strlen( label ),
724 dlen );
725 if( status != PSA_SUCCESS )
726 {
Janos Follathda6ac012019-08-16 13:47:29 +0100727 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500728 psa_destroy_key( master_slot );
729 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
730 }
731
Janos Follathda6ac012019-08-16 13:47:29 +0100732 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500733 if( status != PSA_SUCCESS )
734 {
Janos Follathda6ac012019-08-16 13:47:29 +0100735 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500736 psa_destroy_key( master_slot );
737 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
738 }
739
Janos Follathda6ac012019-08-16 13:47:29 +0100740 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500741 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500742 {
743 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500744 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500745 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500746
747 status = psa_destroy_key( master_slot );
748 if( status != PSA_SUCCESS )
749 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
750
Andrzej Kurek33171262019-01-15 03:25:18 -0500751 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500752}
753
754#else /* MBEDTLS_USE_PSA_CRYPTO */
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100757 const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
760 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761{
762 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300764 unsigned char *tmp;
765 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
767 const mbedtls_md_info_t *md_info;
768 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100769 int ret;
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
774 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100777
Ron Eldor3b350852019-05-07 18:31:49 +0300778 tmp_len = md_len + strlen( label ) + rlen;
779 tmp = mbedtls_calloc( 1, tmp_len );
780 if( tmp == NULL )
781 {
782 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
783 goto exit;
784 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000785
786 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100787 memcpy( tmp + md_len, label, nb );
788 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000789 nb += rlen;
790
791 /*
792 * Compute P_<hash>(secret, label + random)[0..dlen]
793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300795 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
798 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
799 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100800
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100801 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_md_hmac_reset ( &md_ctx );
804 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
805 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_md_hmac_reset ( &md_ctx );
808 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
809 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000810
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100811 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812
813 for( j = 0; j < k; j++ )
814 dstbuf[i + j] = h_i[j];
815 }
816
Ron Eldor3b350852019-05-07 18:31:49 +0300817exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100819
Ron Eldor3b350852019-05-07 18:31:49 +0300820 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500821 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000822
Ron Eldor3b350852019-05-07 18:31:49 +0300823 mbedtls_free( tmp );
824
825 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000826}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500827#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100829static int tls_prf_sha256( const unsigned char *secret, size_t slen,
830 const char *label,
831 const unsigned char *random, size_t rlen,
832 unsigned char *dstbuf, size_t dlen )
833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100835 label, random, rlen, dstbuf, dlen ) );
836}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200840static int tls_prf_sha384( const unsigned char *secret, size_t slen,
841 const char *label,
842 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843 unsigned char *dstbuf, size_t dlen )
844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100846 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000847}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#endif /* MBEDTLS_SHA512_C */
849#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
854 defined(MBEDTLS_SSL_PROTO_TLS1_1)
855static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200856#endif
Paul Bakker380da532012-04-18 16:10:25 +0000857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200859static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200861#endif
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200864static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200866#endif
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
869#if defined(MBEDTLS_SHA256_C)
870static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200871static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200873#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_SHA512_C)
876static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200877static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000881
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100882#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100883 defined(MBEDTLS_USE_PSA_CRYPTO)
884static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
885{
886 if( ssl->conf->f_psk != NULL )
887 {
888 /* If we've used a callback to select the PSK,
889 * the static configuration is irrelevant. */
890 if( ssl->handshake->psk_opaque != 0 )
891 return( 1 );
892
893 return( 0 );
894 }
895
896 if( ssl->conf->psk_opaque != 0 )
897 return( 1 );
898
899 return( 0 );
900}
901#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100902 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100903
Ron Eldorcf280092019-05-14 20:19:13 +0300904#if defined(MBEDTLS_SSL_EXPORT_KEYS)
905static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
906{
907#if defined(MBEDTLS_SSL_PROTO_SSL3)
908 if( tls_prf == ssl3_prf )
909 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300910 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300911 }
912 else
913#endif
914#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
915 if( tls_prf == tls1_prf )
916 {
917 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
918 }
919 else
920#endif
921#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
922#if defined(MBEDTLS_SHA512_C)
923 if( tls_prf == tls_prf_sha384 )
924 {
925 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
926 }
927 else
928#endif
929#if defined(MBEDTLS_SHA256_C)
930 if( tls_prf == tls_prf_sha256 )
931 {
932 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
933 }
934 else
935#endif
936#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
937 return( MBEDTLS_SSL_TLS_PRF_NONE );
938}
939#endif /* MBEDTLS_SSL_EXPORT_KEYS */
940
Ron Eldor51d3ab52019-05-12 14:54:30 +0300941int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
942 const unsigned char *secret, size_t slen,
943 const char *label,
944 const unsigned char *random, size_t rlen,
945 unsigned char *dstbuf, size_t dlen )
946{
947 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
948
949 switch( prf )
950 {
951#if defined(MBEDTLS_SSL_PROTO_SSL3)
952 case MBEDTLS_SSL_TLS_PRF_SSL3:
953 tls_prf = ssl3_prf;
954 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300955#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300956#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
957 case MBEDTLS_SSL_TLS_PRF_TLS1:
958 tls_prf = tls1_prf;
959 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300960#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
961
962#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300963#if defined(MBEDTLS_SHA512_C)
964 case MBEDTLS_SSL_TLS_PRF_SHA384:
965 tls_prf = tls_prf_sha384;
966 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300967#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300968#if defined(MBEDTLS_SHA256_C)
969 case MBEDTLS_SSL_TLS_PRF_SHA256:
970 tls_prf = tls_prf_sha256;
971 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300972#endif /* MBEDTLS_SHA256_C */
973#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300974 default:
975 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
976 }
977
978 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
979}
980
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200981/* Type for the TLS PRF */
982typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
983 const unsigned char *, size_t,
984 unsigned char *, size_t);
985
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200986/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200987 * Populate a transform structure with session keys and all the other
988 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200989 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200990 * Parameters:
991 * - [in/out]: transform: structure to populate
992 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200993 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200994 * - [in] ciphersuite
995 * - [in] master
996 * - [in] encrypt_then_mac
997 * - [in] trunc_hmac
998 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200999 * - [in] tls_prf: pointer to PRF to use for key derivation
1000 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001001 * - [in] minor_ver: SSL/TLS minor version
1002 * - [in] endpoint: client or server
1003 * - [in] ssl: optionally used for:
1004 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1005 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1006 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001007 */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001008static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001009 int ciphersuite,
1010 const unsigned char master[48],
1011#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1012 int encrypt_then_mac,
1013#endif
1014#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1015 int trunc_hmac,
1016#endif
1017#if defined(MBEDTLS_ZLIB_SUPPORT)
1018 int compression,
1019#endif
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001020 ssl_tls_prf_t tls_prf,
1021 const unsigned char randbytes[64],
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001022 int minor_ver,
1023 unsigned endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001024 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001025{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001026 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001027#if defined(MBEDTLS_USE_PSA_CRYPTO)
1028 int psa_fallthrough;
1029#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 unsigned char keyblk[256];
1031 unsigned char *key1;
1032 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001033 unsigned char *mac_enc;
1034 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001035 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001036 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001037 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001038 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 const mbedtls_cipher_info_t *cipher_info;
1040 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001041
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001042#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1043 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1044 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001045 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001046 (void) ssl;
1047#endif
1048
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001049 /*
1050 * Some data just needs copying into the structure
1051 */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1053 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001054 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001055#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001056 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001057
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001058#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1059 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1060#endif
1061
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001062 /*
1063 * Get various info structures
1064 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001065 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001066 if( ciphersuite_info == NULL )
1067 {
1068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001069 ciphersuite ) );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1071 }
1072
Hanno Beckere694c3e2017-12-27 21:34:08 +00001073 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001074 if( cipher_info == NULL )
1075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001077 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001079 }
1080
Hanno Beckere694c3e2017-12-27 21:34:08 +00001081 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001082 if( md_info == NULL )
1083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001085 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001087 }
1088
Hanno Beckera0e20d02019-05-15 14:03:01 +01001089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001090 /* Copy own and peer's CID if the use of the CID
1091 * extension has been negotiated. */
1092 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1093 {
1094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001095
Hanno Becker05154c32019-05-03 15:23:51 +01001096 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001097 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001098 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001099 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001100
1101 transform->out_cid_len = ssl->handshake->peer_cid_len;
1102 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1103 ssl->handshake->peer_cid_len );
1104 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1105 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001106 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001107#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001108
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001110 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001112 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001113 if( ret != 0 )
1114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001116 return( ret );
1117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnardd91efa42019-05-20 10:27:20 +02001120 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001121 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001122 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 /*
1126 * Determine the appropriate key, IV and MAC length.
1127 */
Paul Bakker68884e32013-01-07 18:20:04 +01001128
Hanno Becker88aaf652017-12-27 08:17:40 +00001129 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001130
Hanno Becker8031d062018-01-03 15:32:31 +00001131#if defined(MBEDTLS_GCM_C) || \
1132 defined(MBEDTLS_CCM_C) || \
1133 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001135 cipher_info->mode == MBEDTLS_MODE_CCM ||
1136 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001138 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001139
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001140 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001141 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001142 transform->taglen =
1143 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001144
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001145 /* All modes haves 96-bit IVs;
1146 * GCM and CCM has 4 implicit and 8 explicit bytes
1147 * ChachaPoly has all 12 bytes implicit
1148 */
Paul Bakker68884e32013-01-07 18:20:04 +01001149 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001150 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1151 transform->fixed_ivlen = 12;
1152 else
1153 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001154
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001155 /* Minimum length of encrypted record */
1156 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001157 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001158 }
1159 else
Hanno Becker8031d062018-01-03 15:32:31 +00001160#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1161#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1162 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1163 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001164 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001165 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1167 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001170 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001173 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001174 mac_key_len = mbedtls_md_get_size( md_info );
1175 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001178 /*
1179 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1180 * (rfc 6066 page 13 or rfc 2104 section 4),
1181 * so we only need to adjust the length here.
1182 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001183 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001186
1187#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1188 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001189 * HMAC implementation which also truncates the key
1190 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001191 mac_key_len = transform->maclen;
1192#endif
1193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001195
1196 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001197 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001198
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001199 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001201 transform->minlen = transform->maclen;
1202 else
Paul Bakker68884e32013-01-07 18:20:04 +01001203 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001204 /*
1205 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001206 * 1. if EtM is in use: one block plus MAC
1207 * otherwise: * first multiple of blocklen greater than maclen
1208 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001211 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001212 {
1213 transform->minlen = transform->maclen
1214 + cipher_info->block_size;
1215 }
1216 else
1217#endif
1218 {
1219 transform->minlen = transform->maclen
1220 + cipher_info->block_size
1221 - transform->maclen % cipher_info->block_size;
1222 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001225 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1226 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001227 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001228 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001231 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1232 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001233 {
1234 transform->minlen += transform->ivlen;
1235 }
1236 else
1237#endif
1238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001240 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1241 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001242 }
Paul Bakker68884e32013-01-07 18:20:04 +01001243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
Hanno Becker8031d062018-01-03 15:32:31 +00001245 else
1246#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1247 {
1248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1250 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001251
Hanno Becker88aaf652017-12-27 08:17:40 +00001252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1253 (unsigned) keylen,
1254 (unsigned) transform->minlen,
1255 (unsigned) transform->ivlen,
1256 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
1258 /*
1259 * Finally setup the cipher contexts, IVs and MAC secrets.
1260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001262 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001264 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001265 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Paul Bakker68884e32013-01-07 18:20:04 +01001267 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001268 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001270 /*
1271 * This is not used in TLS v1.1.
1272 */
Paul Bakker48916f92012-09-16 19:57:18 +00001273 iv_copy_len = ( transform->fixed_ivlen ) ?
1274 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001275 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1276 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001277 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 }
1279 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#endif /* MBEDTLS_SSL_CLI_C */
1281#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001282 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001284 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001285 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Hanno Becker81c7b182017-11-09 18:39:33 +00001287 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001288 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001290 /*
1291 * This is not used in TLS v1.1.
1292 */
Paul Bakker48916f92012-09-16 19:57:18 +00001293 iv_copy_len = ( transform->fixed_ivlen ) ?
1294 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001295 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1296 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001297 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001299 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001303 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1304 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Hanno Beckerd56ed242018-01-03 15:32:51 +00001307#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001309 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001310 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001311 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001314 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1315 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001316 }
1317
Hanno Becker81c7b182017-11-09 18:39:33 +00001318 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1319 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001320 }
1321 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1323#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1324 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001325 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001326 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001327 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1328 For AEAD-based ciphersuites, there is nothing to do here. */
1329 if( mac_key_len != 0 )
1330 {
1331 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1332 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1333 }
Paul Bakker68884e32013-01-07 18:20:04 +01001334 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001335 else
1336#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001339 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1340 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001341 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001342#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1345 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001346 {
1347 int ret = 0;
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001350
Hanno Becker88aaf652017-12-27 08:17:40 +00001351 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001352 transform->iv_enc, transform->iv_dec,
1353 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001354 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001355 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001358 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1359 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001360 }
1361 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001362#else
1363 ((void) mac_dec);
1364 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001366
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001367#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1368 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001369 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001370 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001371 master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001372 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001373 iv_copy_len );
1374 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001375
1376 if( ssl->conf->f_export_keys_ext != NULL )
1377 {
1378 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001379 master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001380 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001381 iv_copy_len,
Manuel Pégourié-Gonnard344460c2019-07-25 13:17:38 +02001382 /* work around bug in exporter type */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001383 (unsigned char *) randbytes + 32,
1384 (unsigned char *) randbytes,
1385 tls_prf_get_type( tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001386 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001387#endif
1388
Hanno Beckerf704bef2018-11-16 15:21:18 +00001389#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001390
1391 /* Only use PSA-based ciphers for TLS-1.2.
1392 * That's relevant at least for TLS-1.0, where
1393 * we assume that mbedtls_cipher_crypt() updates
1394 * the structure field for the IV, which the PSA-based
1395 * implementation currently doesn't. */
1396#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1397 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001398 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001399 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001400 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001401 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1402 {
1403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001404 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001405 }
1406
1407 if( ret == 0 )
1408 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001410 psa_fallthrough = 0;
1411 }
1412 else
1413 {
1414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1415 psa_fallthrough = 1;
1416 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001417 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001418 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001419 psa_fallthrough = 1;
1420#else
1421 psa_fallthrough = 1;
1422#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001423
Hanno Beckercb1cc802018-11-17 22:27:38 +00001424 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001425#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001426 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001427 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001430 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001431 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001432
Hanno Beckerf704bef2018-11-16 15:21:18 +00001433#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001434 /* Only use PSA-based ciphers for TLS-1.2.
1435 * That's relevant at least for TLS-1.0, where
1436 * we assume that mbedtls_cipher_crypt() updates
1437 * the structure field for the IV, which the PSA-based
1438 * implementation currently doesn't. */
1439#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1440 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001441 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001442 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001443 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001444 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1445 {
1446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001447 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001448 }
1449
1450 if( ret == 0 )
1451 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001453 psa_fallthrough = 0;
1454 }
1455 else
1456 {
1457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1458 psa_fallthrough = 1;
1459 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001460 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001461 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001462 psa_fallthrough = 1;
1463#else
1464 psa_fallthrough = 1;
1465#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001466
Hanno Beckercb1cc802018-11-17 22:27:38 +00001467 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001468#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001469 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001470 cipher_info ) ) != 0 )
1471 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001473 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001474 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001477 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001481 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001482 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001485 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001489 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001490 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_CIPHER_MODE_CBC)
1493 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1496 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001499 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001500 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1503 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001506 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Paul Bakker5121ce52009-01-03 21:22:43 +00001511
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001512 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001514 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001517
Paul Bakker48916f92012-09-16 19:57:18 +00001518 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1519 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001520
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001521 if( deflateInit( &transform->ctx_deflate,
1522 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001523 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001526 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1527 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001528 }
1529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001531
Ron Eldore6992702019-05-07 18:27:13 +03001532end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001533 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001534 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001535}
1536
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001537/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001538 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001539 *
1540 * Inputs:
1541 * - SSL/TLS minor version
1542 * - hash associated with the ciphersuite (only used by TLS 1.2)
1543 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001544 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001545 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1546 */
1547static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1548 int minor_ver,
1549 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001550{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001551#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1552 (void) hash;
1553#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001554
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001555#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001556 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001557 {
1558 handshake->tls_prf = ssl3_prf;
1559 handshake->calc_verify = ssl_calc_verify_ssl;
1560 handshake->calc_finished = ssl_calc_finished_ssl;
1561 }
1562 else
1563#endif
1564#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001565 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001566 {
1567 handshake->tls_prf = tls1_prf;
1568 handshake->calc_verify = ssl_calc_verify_tls;
1569 handshake->calc_finished = ssl_calc_finished_tls;
1570 }
1571 else
1572#endif
1573#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1574#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001575 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1576 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001577 {
1578 handshake->tls_prf = tls_prf_sha384;
1579 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1580 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1581 }
1582 else
1583#endif
1584#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001585 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001586 {
1587 handshake->tls_prf = tls_prf_sha256;
1588 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1589 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1590 }
1591 else
1592#endif
1593#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1594 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1596 }
1597
1598 return( 0 );
1599}
1600
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001601/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001602 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001603 *
1604 * Parameters:
1605 * [in/out] handshake
1606 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001607 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001608 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001609 * [out] master
1610 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1611 * debug: conf->f_dbg, conf->p_dbg
1612 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001613 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001614 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001615static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001616 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001617 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001618{
1619 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001620
1621 /* cf. RFC 5246, Section 8.1:
1622 * "The master secret is always exactly 48 bytes in length." */
1623 size_t const master_secret_len = 48;
1624
1625#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1626 unsigned char session_hash[48];
1627#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1628
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001629 /* The label for the KDF used for key expansion.
1630 * This is either "master secret" or "extended master secret"
1631 * depending on whether the Extended Master Secret extension
1632 * is used. */
1633 char const *lbl = "master secret";
1634
1635 /* The salt for the KDF used for key expansion.
1636 * - If the Extended Master Secret extension is not used,
1637 * this is ClientHello.Random + ServerHello.Random
1638 * (see Sect. 8.1 in RFC 5246).
1639 * - If the Extended Master Secret extension is used,
1640 * this is the transcript of the handshake so far.
1641 * (see Sect. 4 in RFC 7627). */
1642 unsigned char const *salt = handshake->randbytes;
1643 size_t salt_len = 64;
1644
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001645#if !defined(MBEDTLS_DEBUG_C) && \
1646 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1647 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1648 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001649 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001650 (void) ssl;
1651#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001652
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001653 if( handshake->resume != 0 )
1654 {
1655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001656 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001657 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001658
1659#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001660 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001661 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001662 lbl = "extended master secret";
1663 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001664 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001665
Manuel Pégourié-Gonnard8faa70e2019-05-20 12:09:50 +02001666 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1667 session_hash, salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001668 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001669#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1670
1671#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001672 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1673 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001674 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001675 ssl_use_opaque_psk( ssl ) == 1 )
1676 {
1677 /* Perform PSK-to-MS expansion in a single step. */
1678 psa_status_t status;
1679 psa_algorithm_t alg;
1680 psa_key_handle_t psk;
1681 psa_key_derivation_operation_t derivation =
1682 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001683 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001684
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001686
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001687 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001688 if( handshake->psk_opaque != 0 )
1689 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001690
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001691 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001692 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001693 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001694 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1695
1696 status = psa_key_derivation( &derivation, psk, alg,
1697 salt, salt_len,
1698 (unsigned char const *) lbl,
1699 (size_t) strlen( lbl ),
1700 master_secret_len );
1701 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001702 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001703 psa_key_derivation_abort( &derivation );
1704 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001705 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001706
1707 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001708 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001709 master_secret_len );
1710 if( status != PSA_SUCCESS )
1711 {
1712 psa_key_derivation_abort( &derivation );
1713 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1714 }
1715
1716 status = psa_key_derivation_abort( &derivation );
1717 if( status != PSA_SUCCESS )
1718 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1719 }
1720 else
1721#endif
1722 {
1723 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1724 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001725 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001726 master_secret_len );
1727 if( ret != 0 )
1728 {
1729 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1730 return( ret );
1731 }
1732
1733 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1734 handshake->premaster,
1735 handshake->pmslen );
1736
1737 mbedtls_platform_zeroize( handshake->premaster,
1738 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001739 }
1740
1741 return( 0 );
1742}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001743
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001744int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1745{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001746 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001747 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1748 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001749
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1751
1752 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001753 ret = ssl_set_handshake_prfs( ssl->handshake,
1754 ssl->minor_ver,
1755 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001756 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001757 {
1758 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001759 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001760 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001761
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001762 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001763 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001764 ssl->session_negotiate->master,
1765 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001766 if( ret != 0 )
1767 {
1768 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1769 return( ret );
1770 }
1771
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001772 /* Swap the client and server random values:
1773 * - MS derivation wanted client+server (RFC 5246 8.1)
1774 * - key derivation wants server+client (RFC 5246 6.3) */
1775 {
1776 unsigned char tmp[64];
1777 memcpy( tmp, ssl->handshake->randbytes, 64 );
1778 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1779 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1780 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1781 }
1782
1783 /* Populate transform structure */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001784 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001785 ssl->session_negotiate->ciphersuite,
1786 ssl->session_negotiate->master,
1787#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1788 ssl->session_negotiate->encrypt_then_mac,
1789#endif
1790#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1791 ssl->session_negotiate->trunc_hmac,
1792#endif
1793#if defined(MBEDTLS_ZLIB_SUPPORT)
1794 ssl->session_negotiate->compression,
1795#endif
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001796 ssl->handshake->tls_prf,
1797 ssl->handshake->randbytes,
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001798 ssl->minor_ver,
1799 ssl->conf->endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001800 ssl );
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001801 if( ret != 0 )
1802 {
1803 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1804 return( ret );
1805 }
1806
1807 /* We no longer need Server/ClientHello.random values */
1808 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1809 sizeof( ssl->handshake->randbytes ) );
1810
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001811 /* Allocate compression buffer */
1812#if defined(MBEDTLS_ZLIB_SUPPORT)
1813 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1814 ssl->compress_buf == NULL )
1815 {
1816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1817 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1818 if( ssl->compress_buf == NULL )
1819 {
1820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnardd91efa42019-05-20 10:27:20 +02001821 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001822 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1823 }
1824 }
1825#endif
1826
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1828
1829 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001830}
1831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001833void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1834 unsigned char hash[36],
1835 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001837 mbedtls_md5_context md5;
1838 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 unsigned char pad_1[48];
1840 unsigned char pad_2[48];
1841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001844 mbedtls_md5_init( &md5 );
1845 mbedtls_sha1_init( &sha1 );
1846
1847 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1848 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
Paul Bakker380da532012-04-18 16:10:25 +00001850 memset( pad_1, 0x36, 48 );
1851 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001853 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1854 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1855 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001856
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001857 mbedtls_md5_starts_ret( &md5 );
1858 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1859 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1860 mbedtls_md5_update_ret( &md5, hash, 16 );
1861 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001863 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1864 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1865 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001866
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001867 mbedtls_sha1_starts_ret( &sha1 );
1868 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1869 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1870 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1871 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001872
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001873 *hlen = 36;
1874
1875 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001877
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001878 mbedtls_md5_free( &md5 );
1879 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001880
Paul Bakker380da532012-04-18 16:10:25 +00001881 return;
1882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001886void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1887 unsigned char hash[36],
1888 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001889{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001890 mbedtls_md5_context md5;
1891 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001894
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001895 mbedtls_md5_init( &md5 );
1896 mbedtls_sha1_init( &sha1 );
1897
1898 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1899 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001900
Andrzej Kurekeb342242019-01-29 09:14:33 -05001901 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001902 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001903
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001904 *hlen = 36;
1905
1906 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001908
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001909 mbedtls_md5_free( &md5 );
1910 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001911
Paul Bakker380da532012-04-18 16:10:25 +00001912 return;
1913}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1917#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001918void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1919 unsigned char hash[32],
1920 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001921{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001922#if defined(MBEDTLS_USE_PSA_CRYPTO)
1923 size_t hash_size;
1924 psa_status_t status;
1925 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1926
1927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1928 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1929 if( status != PSA_SUCCESS )
1930 {
1931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1932 return;
1933 }
1934
1935 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1936 if( status != PSA_SUCCESS )
1937 {
1938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1939 return;
1940 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001941
1942 *hlen = 32;
1943 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1945#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001946 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001947
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001948 mbedtls_sha256_init( &sha256 );
1949
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001951
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001952 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001953 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001954
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001955 *hlen = 32;
1956
1957 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001959
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001960 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001961#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001962 return;
1963}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001967void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1968 unsigned char hash[48],
1969 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001970{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001971#if defined(MBEDTLS_USE_PSA_CRYPTO)
1972 size_t hash_size;
1973 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001974 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001975
1976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001977 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001978 if( status != PSA_SUCCESS )
1979 {
1980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1981 return;
1982 }
1983
Andrzej Kurek972fba52019-01-30 03:29:12 -05001984 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001985 if( status != PSA_SUCCESS )
1986 {
1987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1988 return;
1989 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001990
1991 *hlen = 48;
1992 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1994#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001995 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001996
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001997 mbedtls_sha512_init( &sha512 );
1998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00002000
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02002001 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002002 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002003
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002004 *hlen = 48;
2005
2006 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02002009 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05002010#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 return;
2012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#endif /* MBEDTLS_SHA512_C */
2014#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2017int 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 +02002018{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002019 unsigned char *p = ssl->handshake->premaster;
2020 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002021 const unsigned char *psk = ssl->conf->psk;
2022 size_t psk_len = ssl->conf->psk_len;
2023
2024 /* If the psk callback was called, use its result */
2025 if( ssl->handshake->psk != NULL )
2026 {
2027 psk = ssl->handshake->psk;
2028 psk_len = ssl->handshake->psk_len;
2029 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002030
2031 /*
2032 * PMS = struct {
2033 * opaque other_secret<0..2^16-1>;
2034 * opaque psk<0..2^16-1>;
2035 * };
2036 * with "other_secret" depending on the particular key exchange
2037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2039 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002040 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002041 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002043
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002044 *(p++) = (unsigned char)( psk_len >> 8 );
2045 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002046
2047 if( end < p || (size_t)( end - p ) < psk_len )
2048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2049
2050 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002051 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002052 }
2053 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2055#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2056 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002057 {
2058 /*
2059 * other_secret already set by the ClientKeyExchange message,
2060 * and is 48 bytes long
2061 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002062 if( end - p < 2 )
2063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2064
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002065 *p++ = 0;
2066 *p++ = 48;
2067 p += 48;
2068 }
2069 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2071#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2072 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002073 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002074 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002075 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002076
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002077 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002079 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002080 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002083 return( ret );
2084 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002085 *(p++) = (unsigned char)( len >> 8 );
2086 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002087 p += len;
2088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002090 }
2091 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2093#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2094 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002095 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002096 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002097 size_t zlen;
2098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002100 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002101 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002104 return( ret );
2105 }
2106
2107 *(p++) = (unsigned char)( zlen >> 8 );
2108 *(p++) = (unsigned char)( zlen );
2109 p += zlen;
2110
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002111 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2112 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002113 }
2114 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2118 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002119 }
2120
2121 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002122 if( end - p < 2 )
2123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002124
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002125 *(p++) = (unsigned char)( psk_len >> 8 );
2126 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002127
2128 if( end < p || (size_t)( end - p ) < psk_len )
2129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2130
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002131 memcpy( p, psk, psk_len );
2132 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002133
2134 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2135
2136 return( 0 );
2137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002141/*
2142 * SSLv3.0 MAC functions
2143 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002144#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002145static void ssl_mac( mbedtls_md_context_t *md_ctx,
2146 const unsigned char *secret,
2147 const unsigned char *buf, size_t len,
2148 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002149 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150{
2151 unsigned char header[11];
2152 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002153 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2155 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002156
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002157 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002159 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002160 else
Paul Bakker68884e32013-01-07 18:20:04 +01002161 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
2163 memcpy( header, ctr, 8 );
2164 header[ 8] = (unsigned char) type;
2165 header[ 9] = (unsigned char)( len >> 8 );
2166 header[10] = (unsigned char)( len );
2167
Paul Bakker68884e32013-01-07 18:20:04 +01002168 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 mbedtls_md_starts( md_ctx );
2170 mbedtls_md_update( md_ctx, secret, md_size );
2171 mbedtls_md_update( md_ctx, padding, padlen );
2172 mbedtls_md_update( md_ctx, header, 11 );
2173 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002174 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
Paul Bakker68884e32013-01-07 18:20:04 +01002176 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 mbedtls_md_starts( md_ctx );
2178 mbedtls_md_update( md_ctx, secret, md_size );
2179 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002180 mbedtls_md_update( md_ctx, out, md_size );
2181 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002184
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002185/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002186 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002187#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002188 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2189 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2190 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2191/* This function makes sure every byte in the memory region is accessed
2192 * (in ascending addresses order) */
2193static void ssl_read_memory( unsigned char *p, size_t len )
2194{
2195 unsigned char acc = 0;
2196 volatile unsigned char force;
2197
2198 for( ; len != 0; p++, len-- )
2199 acc ^= *p;
2200
2201 force = acc;
2202 (void) force;
2203}
2204#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2205
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002206/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002208 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002209
Hanno Beckera0e20d02019-05-15 14:03:01 +01002210#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002211/* This functions transforms a DTLS plaintext fragment and a record content
2212 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002213 *
2214 * struct {
2215 * opaque content[DTLSPlaintext.length];
2216 * ContentType real_type;
2217 * uint8 zeros[length_of_padding];
2218 * } DTLSInnerPlaintext;
2219 *
2220 * Input:
2221 * - `content`: The beginning of the buffer holding the
2222 * plaintext to be wrapped.
2223 * - `*content_size`: The length of the plaintext in Bytes.
2224 * - `max_len`: The number of Bytes available starting from
2225 * `content`. This must be `>= *content_size`.
2226 * - `rec_type`: The desired record content type.
2227 *
2228 * Output:
2229 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2230 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2231 *
2232 * Returns:
2233 * - `0` on success.
2234 * - A negative error code if `max_len` didn't offer enough space
2235 * for the expansion.
2236 */
2237static int ssl_cid_build_inner_plaintext( unsigned char *content,
2238 size_t *content_size,
2239 size_t remaining,
2240 uint8_t rec_type )
2241{
2242 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002243 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2244 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2245 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002246
2247 /* Write real content type */
2248 if( remaining == 0 )
2249 return( -1 );
2250 content[ len ] = rec_type;
2251 len++;
2252 remaining--;
2253
2254 if( remaining < pad )
2255 return( -1 );
2256 memset( content + len, 0, pad );
2257 len += pad;
2258 remaining -= pad;
2259
2260 *content_size = len;
2261 return( 0 );
2262}
2263
Hanno Becker07dc97d2019-05-20 15:08:01 +01002264/* This function parses a DTLSInnerPlaintext structure.
2265 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002266static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2267 size_t *content_size,
2268 uint8_t *rec_type )
2269{
2270 size_t remaining = *content_size;
2271
2272 /* Determine length of padding by skipping zeroes from the back. */
2273 do
2274 {
2275 if( remaining == 0 )
2276 return( -1 );
2277 remaining--;
2278 } while( content[ remaining ] == 0 );
2279
2280 *content_size = remaining;
2281 *rec_type = content[ remaining ];
2282
2283 return( 0 );
2284}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002285#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002286
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002287/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002288 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002289static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002290 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002291 mbedtls_record *rec )
2292{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002293 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002294 *
2295 * additional_data = seq_num + TLSCompressed.type +
2296 * TLSCompressed.version + TLSCompressed.length;
2297 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002298 * For the CID extension, this is extended as follows
2299 * (quoting draft-ietf-tls-dtls-connection-id-05,
2300 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002301 *
2302 * additional_data = seq_num + DTLSPlaintext.type +
2303 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002304 * cid +
2305 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002306 * length_of_DTLSInnerPlaintext;
2307 */
2308
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002309 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2310 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002311 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002312
Hanno Beckera0e20d02019-05-15 14:03:01 +01002313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002314 if( rec->cid_len != 0 )
2315 {
2316 memcpy( add_data + 11, rec->cid, rec->cid_len );
2317 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2318 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2319 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2320 *add_data_len = 13 + 1 + rec->cid_len;
2321 }
2322 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002323#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002324 {
2325 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2326 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2327 *add_data_len = 13;
2328 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002329}
2330
Hanno Beckera18d1322018-01-03 14:27:32 +00002331int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2332 mbedtls_ssl_transform *transform,
2333 mbedtls_record *rec,
2334 int (*f_rng)(void *, unsigned char *, size_t),
2335 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002336{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002338 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002340 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002341 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002342 size_t post_avail;
2343
2344 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002345#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002346 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002347 ((void) ssl);
2348#endif
2349
2350 /* The PRNG is used for dynamic IV generation that's used
2351 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2352#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2353 ( defined(MBEDTLS_AES_C) || \
2354 defined(MBEDTLS_ARIA_C) || \
2355 defined(MBEDTLS_CAMELLIA_C) ) && \
2356 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2357 ((void) f_rng);
2358 ((void) p_rng);
2359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002362
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002363 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002364 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2367 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002368 if( rec == NULL
2369 || rec->buf == NULL
2370 || rec->buf_len < rec->data_offset
2371 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002373 || rec->cid_len != 0
2374#endif
2375 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002376 {
2377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002379 }
2380
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002381 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002382 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002384 data, rec->data_len );
2385
2386 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2387
2388 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2389 {
2390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2391 (unsigned) rec->data_len,
2392 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2394 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002395
Hanno Beckera0e20d02019-05-15 14:03:01 +01002396#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002397 /*
2398 * Add CID information
2399 */
2400 rec->cid_len = transform->out_cid_len;
2401 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2402 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002403
2404 if( rec->cid_len != 0 )
2405 {
2406 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002407 * Wrap plaintext into DTLSInnerPlaintext structure.
2408 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002409 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002410 * Note that this changes `rec->data_len`, and hence
2411 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002412 */
2413 if( ssl_cid_build_inner_plaintext( data,
2414 &rec->data_len,
2415 post_avail,
2416 rec->type ) != 0 )
2417 {
2418 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2419 }
2420
2421 rec->type = MBEDTLS_SSL_MSG_CID;
2422 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002423#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002424
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002425 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2426
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002428 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 */
Hanno Becker52344c22018-01-03 15:24:20 +00002430#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 if( mode == MBEDTLS_MODE_STREAM ||
2432 ( mode == MBEDTLS_MODE_CBC
2433#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002435#endif
2436 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002438 if( post_avail < transform->maclen )
2439 {
2440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2441 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2442 }
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002445 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002446 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002447 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002448 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2449 data, rec->data_len, rec->ctr, rec->type, mac );
2450 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002451 }
2452 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2455 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002456 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002457 {
Hanno Becker992b6872017-11-09 18:57:39 +00002458 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2459
Hanno Beckercab87e62019-04-29 13:52:53 +01002460 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002461
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002462 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002463 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002464 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2465 data, rec->data_len );
2466 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2467 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2468
2469 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002470 }
2471 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002472#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002476 }
2477
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002478 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2479 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002480
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002481 rec->data_len += transform->maclen;
2482 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002483 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002484 }
Hanno Becker52344c22018-01-03 15:24:20 +00002485#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002487 /*
2488 * Encrypt
2489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2491 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002493 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002494 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002496 "including %d bytes of padding",
2497 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002499 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2500 transform->iv_enc, transform->ivlen,
2501 data, rec->data_len,
2502 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002505 return( ret );
2506 }
2507
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002508 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2511 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 }
Paul Bakker68884e32013-01-07 18:20:04 +01002514 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002516
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002517#if defined(MBEDTLS_GCM_C) || \
2518 defined(MBEDTLS_CCM_C) || \
2519 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002521 mode == MBEDTLS_MODE_CCM ||
2522 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002523 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002524 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002525 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002526 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002527
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 /* Check that there's space for both the authentication tag
2529 * and the explicit IV before and after the record content. */
2530 if( post_avail < transform->taglen ||
2531 rec->data_offset < explicit_iv_len )
2532 {
2533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2534 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2535 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002536
Paul Bakker68884e32013-01-07 18:20:04 +01002537 /*
2538 * Generate IV
2539 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002540 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2541 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002542 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002543 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002544 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2545 explicit_iv_len );
2546 /* Prefix record content with explicit IV. */
2547 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002548 }
2549 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2550 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002551 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002552 unsigned char i;
2553
2554 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2555
2556 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002557 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002558 }
2559 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002560 {
2561 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002564 }
2565
Hanno Beckercab87e62019-04-29 13:52:53 +01002566 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002567
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002568 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2569 iv, transform->ivlen );
2570 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002571 data - explicit_iv_len, explicit_iv_len );
2572 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002573 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002575 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002576 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002577
Paul Bakker68884e32013-01-07 18:20:04 +01002578 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002579 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002580 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002581
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002582 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002583 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002584 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002585 data, rec->data_len, /* source */
2586 data, &rec->data_len, /* destination */
2587 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002590 return( ret );
2591 }
2592
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002593 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2594 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002595
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002596 rec->data_len += transform->taglen + explicit_iv_len;
2597 rec->data_offset -= explicit_iv_len;
2598 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002599 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2603#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002604 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002607 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002608 size_t padlen, i;
2609 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002610
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002611 /* Currently we're always using minimal padding
2612 * (up to 255 bytes would be allowed). */
2613 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2614 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 padlen = 0;
2616
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002617 /* Check there's enough space in the buffer for the padding. */
2618 if( post_avail < padlen + 1 )
2619 {
2620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2621 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2622 }
2623
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002625 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002627 rec->data_len += padlen + 1;
2628 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002631 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002632 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2633 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002634 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002635 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002636 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002637 if( f_rng == NULL )
2638 {
2639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2641 }
2642
2643 if( rec->data_offset < transform->ivlen )
2644 {
2645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2646 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2647 }
2648
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002649 /*
2650 * Generate IV
2651 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002652 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002653 if( ret != 0 )
2654 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002655
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002656 memcpy( data - transform->ivlen, transform->iv_enc,
2657 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002658
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002659 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002663 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002664 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002665 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002667 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2668 transform->iv_enc,
2669 transform->ivlen,
2670 data, rec->data_len,
2671 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002674 return( ret );
2675 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002676
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002677 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2680 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002681 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002684 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002685 {
2686 /*
2687 * Save IV in SSL3 and TLS1
2688 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002689 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2690 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002692 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002693#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002694 {
2695 data -= transform->ivlen;
2696 rec->data_offset -= transform->ivlen;
2697 rec->data_len += transform->ivlen;
2698 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002701 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002702 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002703 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2704
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002705 /*
2706 * MAC(MAC_write_key, seq_num +
2707 * TLSCipherText.type +
2708 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002709 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002710 * IV + // except for TLS 1.0
2711 * ENC(content + padding + padding_length));
2712 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002713
2714 if( post_avail < transform->maclen)
2715 {
2716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2717 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2718 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002719
Hanno Beckercab87e62019-04-29 13:52:53 +01002720 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002723 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002724 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002725
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002726 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002727 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002728 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2729 data, rec->data_len );
2730 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2731 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002732
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002733 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002734
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002735 rec->data_len += transform->maclen;
2736 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002737 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002738 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002741 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002743 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2746 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002747 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002749 /* Make extra sure authentication was performed, exactly once */
2750 if( auth_done != 1 )
2751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2753 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002754 }
2755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
2758 return( 0 );
2759}
2760
Hanno Becker605949f2019-07-12 08:23:59 +01002761int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002762 mbedtls_ssl_transform *transform,
2763 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002764{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002765 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002767 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002768#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002769 size_t padlen = 0, correct = 1;
2770#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002772 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002773 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002774
Hanno Beckera18d1322018-01-03 14:27:32 +00002775#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002776 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002777 ((void) ssl);
2778#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002781 if( rec == NULL ||
2782 rec->buf == NULL ||
2783 rec->buf_len < rec->data_offset ||
2784 rec->buf_len - rec->data_offset < rec->data_len )
2785 {
2786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002788 }
2789
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002790 data = rec->buf + rec->data_offset;
2791 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Hanno Beckera0e20d02019-05-15 14:03:01 +01002793#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002794 /*
2795 * Match record's CID with incoming CID.
2796 */
Hanno Becker938489a2019-05-08 13:02:22 +01002797 if( rec->cid_len != transform->in_cid_len ||
2798 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2799 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002800 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002801 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002802#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2805 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002806 {
2807 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002808 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2809 transform->iv_dec,
2810 transform->ivlen,
2811 data, rec->data_len,
2812 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002815 return( ret );
2816 }
2817
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002818 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002822 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 }
Paul Bakker68884e32013-01-07 18:20:04 +01002824 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002826#if defined(MBEDTLS_GCM_C) || \
2827 defined(MBEDTLS_CCM_C) || \
2828 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002830 mode == MBEDTLS_MODE_CCM ||
2831 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002832 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002833 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002834 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002835
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002836 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002837 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002838 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002839
2840 /* Check that there's enough space for the explicit IV
2841 * (at the beginning of the record) and the MAC (at the
2842 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002846 "+ taglen (%d)", rec->data_len,
2847 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002849 }
Paul Bakker68884e32013-01-07 18:20:04 +01002850
Hanno Beckerd96a6522019-07-10 13:55:25 +01002851#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002852 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2853 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002854 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002855
Hanno Beckerd96a6522019-07-10 13:55:25 +01002856 /* Fixed */
2857 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2858 /* Explicit */
2859 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002860 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002861 else
2862#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2863#if defined(MBEDTLS_CHACHAPOLY_C)
2864 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002865 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002866 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002867 unsigned char i;
2868
2869 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2870
2871 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002872 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002873 }
2874 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002875#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002876 {
2877 /* Reminder if we ever add an AEAD mode with a different size */
2878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2880 }
2881
Hanno Beckerd96a6522019-07-10 13:55:25 +01002882 /* Group changes to data, data_len, and add_data, because
2883 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002884 data += explicit_iv_len;
2885 rec->data_offset += explicit_iv_len;
2886 rec->data_len -= explicit_iv_len + transform->taglen;
2887
Hanno Beckercab87e62019-04-29 13:52:53 +01002888 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002889 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002890 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002891
Hanno Beckerd96a6522019-07-10 13:55:25 +01002892 /* Because of the check above, we know that there are
2893 * explicit_iv_len Bytes preceeding data, and taglen
2894 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002895 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002896 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002897
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002898 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002899 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002900 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002901
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002902 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002903 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002904 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002905 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2906 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002907 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002908 data, rec->data_len,
2909 data, &olen,
2910 data + rec->data_len,
2911 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2916 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002917
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002918 return( ret );
2919 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002920 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002921
Hanno Beckerd96a6522019-07-10 13:55:25 +01002922 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002923 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2926 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002927 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002928 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2931#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002932 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002935 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002936
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 /*
Paul Bakker45829992013-01-03 14:52:21 +01002938 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002941 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2942 {
2943 /* The ciphertext is prefixed with the CBC IV. */
2944 minlen += transform->ivlen;
2945 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#endif
Paul Bakker45829992013-01-03 14:52:21 +01002947
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002948 /* Size considerations:
2949 *
2950 * - The CBC cipher text must not be empty and hence
2951 * at least of size transform->ivlen.
2952 *
2953 * Together with the potential IV-prefix, this explains
2954 * the first of the two checks below.
2955 *
2956 * - The record must contain a MAC, either in plain or
2957 * encrypted, depending on whether Encrypt-then-MAC
2958 * is used or not.
2959 * - If it is, the message contains the IV-prefix,
2960 * the CBC ciphertext, and the MAC.
2961 * - If it is not, the padded plaintext, and hence
2962 * the CBC ciphertext, has at least length maclen + 1
2963 * because there is at least the padding length byte.
2964 *
2965 * As the CBC ciphertext is not empty, both cases give the
2966 * lower bound minlen + maclen + 1 on the record size, which
2967 * we test for in the second check below.
2968 */
2969 if( rec->data_len < minlen + transform->ivlen ||
2970 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002973 "+ 1 ) ( + expl IV )", rec->data_len,
2974 transform->ivlen,
2975 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002977 }
2978
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002979 /*
2980 * Authenticate before decrypt if enabled
2981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002983 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002984 {
Hanno Becker992b6872017-11-09 18:57:39 +00002985 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002988
Hanno Beckerd96a6522019-07-10 13:55:25 +01002989 /* Update data_len in tandem with add_data.
2990 *
2991 * The subtraction is safe because of the previous check
2992 * data_len >= minlen + maclen + 1.
2993 *
2994 * Afterwards, we know that data + data_len is followed by at
2995 * least maclen Bytes, which justifies the call to
2996 * mbedtls_ssl_safer_memcmp() below.
2997 *
2998 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002999 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003000 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003001
Hanno Beckerd96a6522019-07-10 13:55:25 +01003002 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01003003 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3004 add_data_len );
3005 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3006 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003007 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3008 data, rec->data_len );
3009 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3010 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003011
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003012 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3013 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003014 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003015 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003016
Hanno Beckerd96a6522019-07-10 13:55:25 +01003017 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003018 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3019 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003023 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003024 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003025 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003027
3028 /*
3029 * Check length sanity
3030 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01003031
3032 /* We know from above that data_len > minlen >= 0,
3033 * so the following check in particular implies that
3034 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003035 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003038 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003040 }
3041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003043 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003045 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003046 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003047 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01003048 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003049 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003050
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003051 data += transform->ivlen;
3052 rec->data_offset += transform->ivlen;
3053 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003056
Hanno Beckerd96a6522019-07-10 13:55:25 +01003057 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3058
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3060 transform->iv_dec, transform->ivlen,
3061 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003064 return( ret );
3065 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003066
Hanno Beckerd96a6522019-07-10 13:55:25 +01003067 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003068 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003072 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003075 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003076 {
3077 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003078 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3079 * records is equivalent to CBC decryption of the concatenation
3080 * of the records; in other words, IVs are maintained across
3081 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003082 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003083 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3084 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003085 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003086#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003087
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003088 /* Safe since data_len >= minlen + maclen + 1, so after having
3089 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003090 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3091 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003092 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003093
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003094 if( auth_done == 1 )
3095 {
3096 correct *= ( rec->data_len >= padlen + 1 );
3097 padlen *= ( rec->data_len >= padlen + 1 );
3098 }
3099 else
Paul Bakker45829992013-01-03 14:52:21 +01003100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003102 if( rec->data_len < transform->maclen + padlen + 1 )
3103 {
3104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3105 rec->data_len,
3106 transform->maclen,
3107 padlen + 1 ) );
3108 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003109#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003110
3111 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3112 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003113 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003115 padlen++;
3116
3117 /* Regardless of the validity of the padding,
3118 * we have data_len >= padlen here. */
3119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003121 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003122 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003123 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125#if defined(MBEDTLS_SSL_DEBUG_ALL)
3126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003127 "should be no more than %d",
3128 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003129#endif
Paul Bakker45829992013-01-03 14:52:21 +01003130 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 }
3132 }
3133 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3135#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3136 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003137 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003138 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003139 /* The padding check involves a series of up to 256
3140 * consecutive memory reads at the end of the record
3141 * plaintext buffer. In order to hide the length and
3142 * validity of the padding, always perform exactly
3143 * `min(256,plaintext_len)` reads (but take into account
3144 * only the last `padlen` bytes for the padding check). */
3145 size_t pad_count = 0;
3146 size_t real_count = 0;
3147 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003148
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003149 /* Index of first padding byte; it has been ensured above
3150 * that the subtraction is safe. */
3151 size_t const padding_idx = rec->data_len - padlen;
3152 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3153 size_t const start_idx = rec->data_len - num_checks;
3154 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003155
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003156 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003157 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003158 real_count |= ( idx >= padding_idx );
3159 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003160 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003161 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003164 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003166#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003167 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003168 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003169 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3171 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003175 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003176
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003177 /* If the padding was found to be invalid, padlen == 0
3178 * and the subtraction is safe. If the padding was found valid,
3179 * padlen hasn't been changed and the previous assertion
3180 * data_len >= padlen still holds. */
3181 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003182 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003183 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003185 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003189 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003190
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003191#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003193 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003194#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
3196 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003197 * Authenticate if not done yet.
3198 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 */
Hanno Becker52344c22018-01-03 15:24:20 +00003200#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003201 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003202 {
Hanno Becker992b6872017-11-09 18:57:39 +00003203 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003204
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003205 /* If the initial value of padlen was such that
3206 * data_len < maclen + padlen + 1, then padlen
3207 * got reset to 1, and the initial check
3208 * data_len >= minlen + maclen + 1
3209 * guarantees that at this point we still
3210 * have at least data_len >= maclen.
3211 *
3212 * If the initial value of padlen was such that
3213 * data_len >= maclen + padlen + 1, then we have
3214 * subtracted either padlen + 1 (if the padding was correct)
3215 * or 0 (if the padding was incorrect) since then,
3216 * hence data_len >= maclen in any case.
3217 */
3218 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003219 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003222 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003223 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003224 ssl_mac( &transform->md_ctx_dec,
3225 transform->mac_dec,
3226 data, rec->data_len,
3227 rec->ctr, rec->type,
3228 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003229 }
3230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3232#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3233 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003234 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003235 {
3236 /*
3237 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003238 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003239 *
3240 * Known timing attacks:
3241 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3242 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003243 * To compensate for different timings for the MAC calculation
3244 * depending on how much padding was removed (which is determined
3245 * by padlen), process extra_run more blocks through the hash
3246 * function.
3247 *
3248 * The formula in the paper is
3249 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3250 * where L1 is the size of the header plus the decrypted message
3251 * plus CBC padding and L2 is the size of the header plus the
3252 * decrypted message. This is for an underlying hash function
3253 * with 64-byte blocks.
3254 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3255 * correctly. We round down instead of up, so -56 is the correct
3256 * value for our calculations instead of -55.
3257 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003258 * Repeat the formula rather than defining a block_size variable.
3259 * This avoids requiring division by a variable at runtime
3260 * (which would be marginally less efficient and would require
3261 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003262 */
3263 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003264 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003265
3266 /*
3267 * The next two sizes are the minimum and maximum values of
3268 * in_msglen over all padlen values.
3269 *
3270 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003271 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003272 *
3273 * Note that max_len + maclen is never more than the buffer
3274 * length, as we previously did in_msglen -= maclen too.
3275 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003276 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003277 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3278
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003279 memset( tmp, 0, sizeof( tmp ) );
3280
3281 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003282 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003283#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3284 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003285 case MBEDTLS_MD_MD5:
3286 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003287 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003288 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003289 extra_run =
3290 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3291 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003292 break;
3293#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003294#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003295 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003296 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003297 extra_run =
3298 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3299 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003300 break;
3301#endif
3302 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3305 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003306
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003307 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003308
Hanno Beckercab87e62019-04-29 13:52:53 +01003309 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3310 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003311 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3312 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003313 /* Make sure we access everything even when padlen > 0. This
3314 * makes the synchronisation requirements for just-in-time
3315 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003316 ssl_read_memory( data + rec->data_len, padlen );
3317 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003318
3319 /* Call mbedtls_md_process at least once due to cache attacks
3320 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003321 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003322 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003323
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003324 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003325
3326 /* Make sure we access all the memory that could contain the MAC,
3327 * before we check it in the next code block. This makes the
3328 * synchronisation requirements for just-in-time Prime+Probe
3329 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003330 ssl_read_memory( data + min_len,
3331 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003332 }
3333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3335 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003340
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003341#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003342 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3343 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003344#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003345
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003346 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3347 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349#if defined(MBEDTLS_SSL_DEBUG_ALL)
3350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003351#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003352 correct = 0;
3353 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003354 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003355 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003356
3357 /*
3358 * Finally check the correct flag
3359 */
3360 if( correct == 0 )
3361 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003362#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003363
3364 /* Make extra sure authentication was performed, exactly once */
3365 if( auth_done != 1 )
3366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3368 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003370
Hanno Beckera0e20d02019-05-15 14:03:01 +01003371#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003372 if( rec->cid_len != 0 )
3373 {
3374 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3375 &rec->type );
3376 if( ret != 0 )
3377 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3378 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003379#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003382
3383 return( 0 );
3384}
3385
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003386#undef MAC_NONE
3387#undef MAC_PLAINTEXT
3388#undef MAC_CIPHERTEXT
3389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003391/*
3392 * Compression/decompression functions
3393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003395{
3396 int ret;
3397 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003398 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003399 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003400 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003403
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003404 if( len_pre == 0 )
3405 return( 0 );
3406
Paul Bakker2770fbd2012-07-03 13:30:23 +00003407 memcpy( msg_pre, ssl->out_msg, len_pre );
3408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003410 ssl->out_msglen ) );
3411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003413 ssl->out_msg, ssl->out_msglen );
3414
Paul Bakker48916f92012-09-16 19:57:18 +00003415 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3416 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3417 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003418 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419
Paul Bakker48916f92012-09-16 19:57:18 +00003420 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421 if( ret != Z_OK )
3422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3424 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003425 }
3426
Angus Grattond8213d02016-05-25 20:56:48 +10003427 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003428 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003431 ssl->out_msglen ) );
3432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003434 ssl->out_msg, ssl->out_msglen );
3435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003437
3438 return( 0 );
3439}
3440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003442{
3443 int ret;
3444 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003445 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003446 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003447 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003450
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003451 if( len_pre == 0 )
3452 return( 0 );
3453
Paul Bakker2770fbd2012-07-03 13:30:23 +00003454 memcpy( msg_pre, ssl->in_msg, len_pre );
3455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003457 ssl->in_msglen ) );
3458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003460 ssl->in_msg, ssl->in_msglen );
3461
Paul Bakker48916f92012-09-16 19:57:18 +00003462 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3463 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3464 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003465 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003466 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003467
Paul Bakker48916f92012-09-16 19:57:18 +00003468 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003469 if( ret != Z_OK )
3470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3472 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003473 }
3474
Angus Grattond8213d02016-05-25 20:56:48 +10003475 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003476 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003479 ssl->in_msglen ) );
3480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003482 ssl->in_msg, ssl->in_msglen );
3483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003485
3486 return( 0 );
3487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3491static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493#if defined(MBEDTLS_SSL_PROTO_DTLS)
3494static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003495{
3496 /* If renegotiation is not enforced, retransmit until we would reach max
3497 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003498 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003499 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003500 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003501 unsigned char doublings = 1;
3502
3503 while( ratio != 0 )
3504 {
3505 ++doublings;
3506 ratio >>= 1;
3507 }
3508
3509 if( ++ssl->renego_records_seen > doublings )
3510 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003512 return( 0 );
3513 }
3514 }
3515
3516 return( ssl_write_hello_request( ssl ) );
3517}
3518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003520
Paul Bakker5121ce52009-01-03 21:22:43 +00003521/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003522 * Fill the input message buffer by appending data to it.
3523 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003524 *
3525 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3526 * available (from this read and/or a previous one). Otherwise, an error code
3527 * is returned (possibly EOF or WANT_READ).
3528 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003529 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3530 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3531 * since we always read a whole datagram at once.
3532 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003533 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003534 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003537{
Paul Bakker23986e52011-04-24 08:57:21 +00003538 int ret;
3539 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003542
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003543 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003546 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003548 }
3549
Angus Grattond8213d02016-05-25 20:56:48 +10003550 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003554 }
3555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003557 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003559 uint32_t timeout;
3560
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003561 /* Just to be sure */
3562 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3563 {
3564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3565 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3567 }
3568
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003569 /*
3570 * The point is, we need to always read a full datagram at once, so we
3571 * sometimes read more then requested, and handle the additional data.
3572 * It could be the rest of the current record (while fetching the
3573 * header) and/or some other records in the same datagram.
3574 */
3575
3576 /*
3577 * Move to the next record in the already read datagram if applicable
3578 */
3579 if( ssl->next_record_offset != 0 )
3580 {
3581 if( ssl->in_left < ssl->next_record_offset )
3582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003585 }
3586
3587 ssl->in_left -= ssl->next_record_offset;
3588
3589 if( ssl->in_left != 0 )
3590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003592 ssl->next_record_offset ) );
3593 memmove( ssl->in_hdr,
3594 ssl->in_hdr + ssl->next_record_offset,
3595 ssl->in_left );
3596 }
3597
3598 ssl->next_record_offset = 0;
3599 }
3600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003603
3604 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003605 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003606 */
3607 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003610 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003611 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003612
3613 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003614 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003615 * are not at the beginning of a new record, the caller did something
3616 * wrong.
3617 */
3618 if( ssl->in_left != 0 )
3619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3621 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003622 }
3623
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003624 /*
3625 * Don't even try to read if time's out already.
3626 * This avoids by-passing the timer when repeatedly receiving messages
3627 * that will end up being dropped.
3628 */
3629 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003630 {
3631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003632 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003633 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003634 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003635 {
Angus Grattond8213d02016-05-25 20:56:48 +10003636 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003639 timeout = ssl->handshake->retransmit_timeout;
3640 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003641 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003644
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003645 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003646 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3647 timeout );
3648 else
3649 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003652
3653 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003655 }
3656
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003657 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003660 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003663 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003664 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003667 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003668 }
3669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003673 return( ret );
3674 }
3675
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003676 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003677 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003679 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003681 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003682 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003685 return( ret );
3686 }
3687
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003688 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003689 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003691 }
3692
Paul Bakker5121ce52009-01-03 21:22:43 +00003693 if( ret < 0 )
3694 return( ret );
3695
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003696 ssl->in_left = ret;
3697 }
3698 else
3699#endif
3700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003702 ssl->in_left, nb_want ) );
3703
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003704 while( ssl->in_left < nb_want )
3705 {
3706 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003707
3708 if( ssl_check_timer( ssl ) != 0 )
3709 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3710 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003711 {
3712 if( ssl->f_recv_timeout != NULL )
3713 {
3714 ret = ssl->f_recv_timeout( ssl->p_bio,
3715 ssl->in_hdr + ssl->in_left, len,
3716 ssl->conf->read_timeout );
3717 }
3718 else
3719 {
3720 ret = ssl->f_recv( ssl->p_bio,
3721 ssl->in_hdr + ssl->in_left, len );
3722 }
3723 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003726 ssl->in_left, nb_want ) );
3727 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003728
3729 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003731
3732 if( ret < 0 )
3733 return( ret );
3734
mohammad160352aecb92018-03-28 23:41:40 -07003735 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003736 {
Darryl Green11999bb2018-03-13 15:22:58 +00003737 MBEDTLS_SSL_DEBUG_MSG( 1,
3738 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003739 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3741 }
3742
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003743 ssl->in_left += ret;
3744 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003745 }
3746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003748
3749 return( 0 );
3750}
3751
3752/*
3753 * Flush any data not yet written
3754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003756{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003757 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003758 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003761
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003762 if( ssl->f_send == NULL )
3763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003765 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003767 }
3768
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003769 /* Avoid incrementing counter if data is flushed */
3770 if( ssl->out_left == 0 )
3771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003773 return( 0 );
3774 }
3775
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 while( ssl->out_left > 0 )
3777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003779 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003780
Hanno Becker2b1e3542018-08-06 11:19:13 +01003781 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003782 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003785
3786 if( ret <= 0 )
3787 return( ret );
3788
mohammad160352aecb92018-03-28 23:41:40 -07003789 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003790 {
Darryl Green11999bb2018-03-13 15:22:58 +00003791 MBEDTLS_SSL_DEBUG_MSG( 1,
3792 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003793 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3795 }
3796
Paul Bakker5121ce52009-01-03 21:22:43 +00003797 ssl->out_left -= ret;
3798 }
3799
Hanno Becker2b1e3542018-08-06 11:19:13 +01003800#if defined(MBEDTLS_SSL_PROTO_DTLS)
3801 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003802 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003803 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003804 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003805 else
3806#endif
3807 {
3808 ssl->out_hdr = ssl->out_buf + 8;
3809 }
3810 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003813
3814 return( 0 );
3815}
3816
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003817/*
3818 * Functions to handle the DTLS retransmission state machine
3819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003821/*
3822 * Append current handshake message to current outgoing flight
3823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3828 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3829 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003830
3831 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003832 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003833 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003836 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003837 }
3838
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003839 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003840 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003843 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003844 }
3845
3846 /* Copy current handshake message with headers */
3847 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3848 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003849 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003850 msg->next = NULL;
3851
3852 /* Append to the current flight */
3853 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003854 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003855 else
3856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003858 while( cur->next != NULL )
3859 cur = cur->next;
3860 cur->next = msg;
3861 }
3862
Hanno Becker3b235902018-08-06 09:54:53 +01003863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003864 return( 0 );
3865}
3866
3867/*
3868 * Free the current flight of handshake messages
3869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003871{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 mbedtls_ssl_flight_item *cur = flight;
3873 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003874
3875 while( cur != NULL )
3876 {
3877 next = cur->next;
3878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 mbedtls_free( cur->p );
3880 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003881
3882 cur = next;
3883 }
3884}
3885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3887static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003888#endif
3889
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003890/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003891 * Swap transform_out and out_ctr with the alternative ones
3892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003894{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003896 unsigned char tmp_out_ctr[8];
3897
3898 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003901 return;
3902 }
3903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003905
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003906 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003907 tmp_transform = ssl->transform_out;
3908 ssl->transform_out = ssl->handshake->alt_transform_out;
3909 ssl->handshake->alt_transform_out = tmp_transform;
3910
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003911 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003912 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3913 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003914 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003915
3916 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003917 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3920 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3925 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003926 }
3927 }
3928#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003929}
3930
3931/*
3932 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003933 */
3934int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3935{
3936 int ret = 0;
3937
3938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3939
3940 ret = mbedtls_ssl_flight_transmit( ssl );
3941
3942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3943
3944 return( ret );
3945}
3946
3947/*
3948 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003949 *
3950 * Need to remember the current message in case flush_output returns
3951 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003952 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003953 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003954int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003955{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003956 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003960 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003962
3963 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003964 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003965 ssl_swap_epochs( ssl );
3966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003968 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003969
3970 while( ssl->handshake->cur_msg != NULL )
3971 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003972 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003973 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003974
Hanno Beckere1dcb032018-08-17 16:47:58 +01003975 int const is_finished =
3976 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3977 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3978
Hanno Becker04da1892018-08-14 13:22:10 +01003979 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3980 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3981
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003982 /* Swap epochs before sending Finished: we can't do it after
3983 * sending ChangeCipherSpec, in case write returns WANT_READ.
3984 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003985 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003986 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003988 ssl_swap_epochs( ssl );
3989 }
3990
Hanno Becker67bc7c32018-08-06 11:33:50 +01003991 ret = ssl_get_remaining_payload_in_datagram( ssl );
3992 if( ret < 0 )
3993 return( ret );
3994 max_frag_len = (size_t) ret;
3995
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003996 /* CCS is copied as is, while HS messages may need fragmentation */
3997 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3998 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003999 if( max_frag_len == 0 )
4000 {
4001 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4002 return( ret );
4003
4004 continue;
4005 }
4006
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004007 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004008 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004009 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004010
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004011 /* Update position inside current message */
4012 ssl->handshake->cur_msg_p += cur->len;
4013 }
4014 else
4015 {
4016 const unsigned char * const p = ssl->handshake->cur_msg_p;
4017 const size_t hs_len = cur->len - 12;
4018 const size_t frag_off = p - ( cur->p + 12 );
4019 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004020 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004021
Hanno Beckere1dcb032018-08-17 16:47:58 +01004022 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004023 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004024 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004025 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004026
Hanno Becker67bc7c32018-08-06 11:33:50 +01004027 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4028 return( ret );
4029
4030 continue;
4031 }
4032 max_hs_frag_len = max_frag_len - 12;
4033
4034 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4035 max_hs_frag_len : rem_len;
4036
4037 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004038 {
4039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004040 (unsigned) cur_hs_frag_len,
4041 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004042 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004043
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004044 /* Messages are stored with handshake headers as if not fragmented,
4045 * copy beginning of headers then fill fragmentation fields.
4046 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4047 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004048
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004049 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4050 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4051 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4052
Hanno Becker67bc7c32018-08-06 11:33:50 +01004053 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4054 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4055 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004056
4057 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4058
Hanno Becker3f7b9732018-08-28 09:53:25 +01004059 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004060 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4061 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004062 ssl->out_msgtype = cur->type;
4063
4064 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004065 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004066 }
4067
4068 /* If done with the current message move to the next one if any */
4069 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4070 {
4071 if( cur->next != NULL )
4072 {
4073 ssl->handshake->cur_msg = cur->next;
4074 ssl->handshake->cur_msg_p = cur->next->p + 12;
4075 }
4076 else
4077 {
4078 ssl->handshake->cur_msg = NULL;
4079 ssl->handshake->cur_msg_p = NULL;
4080 }
4081 }
4082
4083 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004084 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004087 return( ret );
4088 }
4089 }
4090
Hanno Becker67bc7c32018-08-06 11:33:50 +01004091 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4092 return( ret );
4093
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004094 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4096 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004097 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004100 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4101 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004102
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004104
4105 return( 0 );
4106}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004107
4108/*
4109 * To be called when the last message of an incoming flight is received.
4110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004112{
4113 /* We won't need to resend that one any more */
4114 ssl_flight_free( ssl->handshake->flight );
4115 ssl->handshake->flight = NULL;
4116 ssl->handshake->cur_msg = NULL;
4117
4118 /* The next incoming flight will start with this msg_seq */
4119 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4120
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004121 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004122 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004123
Hanno Becker0271f962018-08-16 13:23:47 +01004124 /* Clear future message buffering structure. */
4125 ssl_buffering_free( ssl );
4126
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004127 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004128 ssl_set_timer( ssl, 0 );
4129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4131 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004134 }
4135 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004137}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004138
4139/*
4140 * To be called when the last message of an outgoing flight is send.
4141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004143{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004144 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004145 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4148 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004151 }
4152 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004156
Paul Bakker5121ce52009-01-03 21:22:43 +00004157/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004158 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004159 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004160
4161/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004162 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004163 *
4164 * - fill in handshake headers
4165 * - update handshake checksum
4166 * - DTLS: save message for resending
4167 * - then pass to the record layer
4168 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004169 * DTLS: except for HelloRequest, messages are only queued, and will only be
4170 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004171 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004172 * Inputs:
4173 * - ssl->out_msglen: 4 + actual handshake message len
4174 * (4 is the size of handshake headers for TLS)
4175 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4176 * - ssl->out_msg + 4: the handshake message body
4177 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004178 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004179 * - ssl->out_msglen: the length of the record contents
4180 * (including handshake headers but excluding record headers)
4181 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004182 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004183int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004184{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004185 int ret;
4186 const size_t hs_len = ssl->out_msglen - 4;
4187 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004188
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4190
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004191 /*
4192 * Sanity checks
4193 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004194 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004195 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4196 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004197 /* In SSLv3, the client might send a NoCertificate alert. */
4198#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4199 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4200 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4201 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4202#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4203 {
4204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4205 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4206 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004208
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004209 /* Whenever we send anything different from a
4210 * HelloRequest we should be in a handshake - double check. */
4211 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4212 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004213 ssl->handshake == NULL )
4214 {
4215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4216 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4217 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004219#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004220 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004221 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004223 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004226 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004227#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004228
Hanno Beckerb50a2532018-08-06 11:52:54 +01004229 /* Double-check that we did not exceed the bounds
4230 * of the outgoing record buffer.
4231 * This should never fail as the various message
4232 * writing functions must obey the bounds of the
4233 * outgoing record buffer, but better be safe.
4234 *
4235 * Note: We deliberately do not check for the MTU or MFL here.
4236 */
4237 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4238 {
4239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4240 "size %u, maximum %u",
4241 (unsigned) ssl->out_msglen,
4242 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4244 }
4245
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004246 /*
4247 * Fill handshake headers
4248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004251 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4252 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4253 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004254
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004255 /*
4256 * DTLS has additional fields in the Handshake layer,
4257 * between the length field and the actual payload:
4258 * uint16 message_seq;
4259 * uint24 fragment_offset;
4260 * uint24 fragment_length;
4261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004262#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004263 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004264 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004265 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004266 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004267 {
4268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4269 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004270 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004271 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004272 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4273 }
4274
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004275 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004276 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004277
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004278 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004279 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004280 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004281 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4282 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4283 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004284 }
4285 else
4286 {
4287 ssl->out_msg[4] = 0;
4288 ssl->out_msg[5] = 0;
4289 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004290
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004291 /* Handshake hashes are computed without fragmentation,
4292 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004293 memset( ssl->out_msg + 6, 0x00, 3 );
4294 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004295 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004297
Hanno Becker0207e532018-08-28 10:28:28 +01004298 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004299 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4300 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004301 }
4302
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004303 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004306 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4307 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004308 {
4309 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004312 return( ret );
4313 }
4314 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004315 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004316#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004317 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004318 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004319 {
4320 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4321 return( ret );
4322 }
4323 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004324
4325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4326
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004327 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004328}
4329
4330/*
4331 * Record layer functions
4332 */
4333
4334/*
4335 * Write current record.
4336 *
4337 * Uses:
4338 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4339 * - ssl->out_msglen: length of the record content (excl headers)
4340 * - ssl->out_msg: record content
4341 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004342int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004343{
4344 int ret, done = 0;
4345 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004346 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004347
4348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004351 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004353 {
4354 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004357 return( ret );
4358 }
4359
4360 len = ssl->out_msglen;
4361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4365 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 ret = mbedtls_ssl_hw_record_write( ssl );
4370 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4373 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004374 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004375
4376 if( ret == 0 )
4377 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004378 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004380 if( !done )
4381 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004382 unsigned i;
4383 size_t protected_record_size;
4384
Hanno Becker6430faf2019-05-08 11:57:13 +01004385 /* Skip writing the record content type to after the encryption,
4386 * as it may change when using the CID extension. */
4387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004389 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004390
Hanno Becker19859472018-08-06 09:40:20 +01004391 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004392 ssl->out_len[0] = (unsigned char)( len >> 8 );
4393 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004394
Paul Bakker48916f92012-09-16 19:57:18 +00004395 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004396 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004397 mbedtls_record rec;
4398
4399 rec.buf = ssl->out_iv;
4400 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4401 ( ssl->out_iv - ssl->out_buf );
4402 rec.data_len = ssl->out_msglen;
4403 rec.data_offset = ssl->out_msg - rec.buf;
4404
4405 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4406 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4407 ssl->conf->transport, rec.ver );
4408 rec.type = ssl->out_msgtype;
4409
Hanno Beckera0e20d02019-05-15 14:03:01 +01004410#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004411 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004412 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004413#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004414
Hanno Beckera18d1322018-01-03 14:27:32 +00004415 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004416 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004419 return( ret );
4420 }
4421
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004422 if( rec.data_offset != 0 )
4423 {
4424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4426 }
4427
Hanno Becker6430faf2019-05-08 11:57:13 +01004428 /* Update the record content type and CID. */
4429 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004430#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004431 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004432#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004433 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004434 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4435 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004436 }
4437
Hanno Becker5903de42019-05-03 14:46:38 +01004438 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004439
4440#if defined(MBEDTLS_SSL_PROTO_DTLS)
4441 /* In case of DTLS, double-check that we don't exceed
4442 * the remaining space in the datagram. */
4443 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4444 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004445 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004446 if( ret < 0 )
4447 return( ret );
4448
4449 if( protected_record_size > (size_t) ret )
4450 {
4451 /* Should never happen */
4452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4453 }
4454 }
4455#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004456
Hanno Becker6430faf2019-05-08 11:57:13 +01004457 /* Now write the potentially updated record content type. */
4458 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004461 "version = [%d:%d], msglen = %d",
4462 ssl->out_hdr[0], ssl->out_hdr[1],
4463 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004466 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004467
4468 ssl->out_left += protected_record_size;
4469 ssl->out_hdr += protected_record_size;
4470 ssl_update_out_pointers( ssl, ssl->transform_out );
4471
Hanno Becker04484622018-08-06 09:49:38 +01004472 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4473 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4474 break;
4475
4476 /* The loop goes to its end iff the counter is wrapping */
4477 if( i == ssl_ep_len( ssl ) )
4478 {
4479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4480 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004482 }
4483
Hanno Becker67bc7c32018-08-06 11:33:50 +01004484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4486 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004487 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004488 size_t remaining;
4489 ret = ssl_get_remaining_payload_in_datagram( ssl );
4490 if( ret < 0 )
4491 {
4492 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4493 ret );
4494 return( ret );
4495 }
4496
4497 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004498 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004499 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004500 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004501 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004502 else
4503 {
Hanno Becker513815a2018-08-20 11:56:09 +01004504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004505 }
4506 }
4507#endif /* MBEDTLS_SSL_PROTO_DTLS */
4508
4509 if( ( flush == SSL_FORCE_FLUSH ) &&
4510 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004513 return( ret );
4514 }
4515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004517
4518 return( 0 );
4519}
4520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004522
4523static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4524{
4525 if( ssl->in_msglen < ssl->in_hslen ||
4526 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4527 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4528 {
4529 return( 1 );
4530 }
4531 return( 0 );
4532}
Hanno Becker44650b72018-08-16 12:51:11 +01004533
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004534static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004535{
4536 return( ( ssl->in_msg[9] << 16 ) |
4537 ( ssl->in_msg[10] << 8 ) |
4538 ssl->in_msg[11] );
4539}
4540
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004541static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004542{
4543 return( ( ssl->in_msg[6] << 16 ) |
4544 ( ssl->in_msg[7] << 8 ) |
4545 ssl->in_msg[8] );
4546}
4547
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004548static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004549{
4550 uint32_t msg_len, frag_off, frag_len;
4551
4552 msg_len = ssl_get_hs_total_len( ssl );
4553 frag_off = ssl_get_hs_frag_off( ssl );
4554 frag_len = ssl_get_hs_frag_len( ssl );
4555
4556 if( frag_off > msg_len )
4557 return( -1 );
4558
4559 if( frag_len > msg_len - frag_off )
4560 return( -1 );
4561
4562 if( frag_len + 12 > ssl->in_msglen )
4563 return( -1 );
4564
4565 return( 0 );
4566}
4567
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004568/*
4569 * Mark bits in bitmask (used for DTLS HS reassembly)
4570 */
4571static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4572{
4573 unsigned int start_bits, end_bits;
4574
4575 start_bits = 8 - ( offset % 8 );
4576 if( start_bits != 8 )
4577 {
4578 size_t first_byte_idx = offset / 8;
4579
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004580 /* Special case */
4581 if( len <= start_bits )
4582 {
4583 for( ; len != 0; len-- )
4584 mask[first_byte_idx] |= 1 << ( start_bits - len );
4585
4586 /* Avoid potential issues with offset or len becoming invalid */
4587 return;
4588 }
4589
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004590 offset += start_bits; /* Now offset % 8 == 0 */
4591 len -= start_bits;
4592
4593 for( ; start_bits != 0; start_bits-- )
4594 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4595 }
4596
4597 end_bits = len % 8;
4598 if( end_bits != 0 )
4599 {
4600 size_t last_byte_idx = ( offset + len ) / 8;
4601
4602 len -= end_bits; /* Now len % 8 == 0 */
4603
4604 for( ; end_bits != 0; end_bits-- )
4605 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4606 }
4607
4608 memset( mask + offset / 8, 0xFF, len / 8 );
4609}
4610
4611/*
4612 * Check that bitmask is full
4613 */
4614static int ssl_bitmask_check( unsigned char *mask, size_t len )
4615{
4616 size_t i;
4617
4618 for( i = 0; i < len / 8; i++ )
4619 if( mask[i] != 0xFF )
4620 return( -1 );
4621
4622 for( i = 0; i < len % 8; i++ )
4623 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4624 return( -1 );
4625
4626 return( 0 );
4627}
4628
Hanno Becker56e205e2018-08-16 09:06:12 +01004629/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004630static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004631 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004632{
Hanno Becker56e205e2018-08-16 09:06:12 +01004633 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004634
Hanno Becker56e205e2018-08-16 09:06:12 +01004635 alloc_len = 12; /* Handshake header */
4636 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004637
Hanno Beckerd07df862018-08-16 09:14:58 +01004638 if( add_bitmap )
4639 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004640
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004641 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004642}
Hanno Becker56e205e2018-08-16 09:06:12 +01004643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004645
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004646static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004647{
4648 return( ( ssl->in_msg[1] << 16 ) |
4649 ( ssl->in_msg[2] << 8 ) |
4650 ssl->in_msg[3] );
4651}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004652
Simon Butcher99000142016-10-13 17:21:01 +01004653int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004658 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004660 }
4661
Hanno Becker12555c62018-08-16 12:47:53 +01004662 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004665 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004666 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004669 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004670 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004671 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004672 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004673
Hanno Becker44650b72018-08-16 12:51:11 +01004674 if( ssl_check_hs_header( ssl ) != 0 )
4675 {
4676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4677 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4678 }
4679
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004680 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004681 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4682 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4683 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4684 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004685 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004686 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4687 {
4688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4689 recv_msg_seq,
4690 ssl->handshake->in_msg_seq ) );
4691 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4692 }
4693
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004694 /* Retransmit only on last message from previous flight, to avoid
4695 * too many retransmissions.
4696 * Besides, No sane server ever retransmits HelloVerifyRequest */
4697 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004701 "message_seq = %d, start_of_flight = %d",
4702 recv_msg_seq,
4703 ssl->handshake->in_flight_start_seq ) );
4704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004708 return( ret );
4709 }
4710 }
4711 else
4712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004714 "message_seq = %d, expected = %d",
4715 recv_msg_seq,
4716 ssl->handshake->in_msg_seq ) );
4717 }
4718
Hanno Becker90333da2017-10-10 11:27:13 +01004719 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004720 }
4721 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004722
Hanno Becker6d97ef52018-08-16 13:09:04 +01004723 /* Message reassembly is handled alongside buffering of future
4724 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004725 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004726 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004727 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004730 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004731 }
4732 }
4733 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004735 /* With TLS we don't handle fragmentation (for now) */
4736 if( ssl->in_msglen < ssl->in_hslen )
4737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4739 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004740 }
4741
Simon Butcher99000142016-10-13 17:21:01 +01004742 return( 0 );
4743}
4744
4745void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4746{
Hanno Becker0271f962018-08-16 13:23:47 +01004747 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004748
Hanno Becker0271f962018-08-16 13:23:47 +01004749 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004750 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004751 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004752 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004753
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004754 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004757 ssl->handshake != NULL )
4758 {
Hanno Becker0271f962018-08-16 13:23:47 +01004759 unsigned offset;
4760 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004761
Hanno Becker0271f962018-08-16 13:23:47 +01004762 /* Increment handshake sequence number */
4763 hs->in_msg_seq++;
4764
4765 /*
4766 * Clear up handshake buffering and reassembly structure.
4767 */
4768
4769 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004770 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004771
4772 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004773 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4774 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004775 offset++, hs_buf++ )
4776 {
4777 *hs_buf = *(hs_buf + 1);
4778 }
4779
4780 /* Create a fresh last entry */
4781 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004782 }
4783#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004784}
4785
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004786/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004787 * DTLS anti-replay: RFC 6347 4.1.2.6
4788 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004789 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4790 * Bit n is set iff record number in_window_top - n has been seen.
4791 *
4792 * Usually, in_window_top is the last record number seen and the lsb of
4793 * in_window is set. The only exception is the initial state (record number 0
4794 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4797static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004798{
4799 ssl->in_window_top = 0;
4800 ssl->in_window = 0;
4801}
4802
4803static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4804{
4805 return( ( (uint64_t) buf[0] << 40 ) |
4806 ( (uint64_t) buf[1] << 32 ) |
4807 ( (uint64_t) buf[2] << 24 ) |
4808 ( (uint64_t) buf[3] << 16 ) |
4809 ( (uint64_t) buf[4] << 8 ) |
4810 ( (uint64_t) buf[5] ) );
4811}
4812
4813/*
4814 * Return 0 if sequence number is acceptable, -1 otherwise
4815 */
Hanno Becker0183d692019-07-12 08:50:37 +01004816int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004817{
4818 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4819 uint64_t bit;
4820
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004821 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004822 return( 0 );
4823
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004824 if( rec_seqnum > ssl->in_window_top )
4825 return( 0 );
4826
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004827 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004828
4829 if( bit >= 64 )
4830 return( -1 );
4831
4832 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4833 return( -1 );
4834
4835 return( 0 );
4836}
4837
4838/*
4839 * Update replay window on new validated record
4840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004842{
4843 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4844
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004845 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004846 return;
4847
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004848 if( rec_seqnum > ssl->in_window_top )
4849 {
4850 /* Update window_top and the contents of the window */
4851 uint64_t shift = rec_seqnum - ssl->in_window_top;
4852
4853 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004854 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004855 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004856 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004857 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004858 ssl->in_window |= 1;
4859 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004860
4861 ssl->in_window_top = rec_seqnum;
4862 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004863 else
4864 {
4865 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004866 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004867
4868 if( bit < 64 ) /* Always true, but be extra sure */
4869 ssl->in_window |= (uint64_t) 1 << bit;
4870 }
4871}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004873
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004874#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004875/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004876static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4877
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004878/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004879 * Without any SSL context, check if a datagram looks like a ClientHello with
4880 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004881 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004882 *
4883 * - if cookie is valid, return 0
4884 * - if ClientHello looks superficially valid but cookie is not,
4885 * fill obuf and set olen, then
4886 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4887 * - otherwise return a specific error code
4888 */
4889static int ssl_check_dtls_clihlo_cookie(
4890 mbedtls_ssl_cookie_write_t *f_cookie_write,
4891 mbedtls_ssl_cookie_check_t *f_cookie_check,
4892 void *p_cookie,
4893 const unsigned char *cli_id, size_t cli_id_len,
4894 const unsigned char *in, size_t in_len,
4895 unsigned char *obuf, size_t buf_len, size_t *olen )
4896{
4897 size_t sid_len, cookie_len;
4898 unsigned char *p;
4899
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004900 /*
4901 * Structure of ClientHello with record and handshake headers,
4902 * and expected values. We don't need to check a lot, more checks will be
4903 * done when actually parsing the ClientHello - skipping those checks
4904 * avoids code duplication and does not make cookie forging any easier.
4905 *
4906 * 0-0 ContentType type; copied, must be handshake
4907 * 1-2 ProtocolVersion version; copied
4908 * 3-4 uint16 epoch; copied, must be 0
4909 * 5-10 uint48 sequence_number; copied
4910 * 11-12 uint16 length; (ignored)
4911 *
4912 * 13-13 HandshakeType msg_type; (ignored)
4913 * 14-16 uint24 length; (ignored)
4914 * 17-18 uint16 message_seq; copied
4915 * 19-21 uint24 fragment_offset; copied, must be 0
4916 * 22-24 uint24 fragment_length; (ignored)
4917 *
4918 * 25-26 ProtocolVersion client_version; (ignored)
4919 * 27-58 Random random; (ignored)
4920 * 59-xx SessionID session_id; 1 byte len + sid_len content
4921 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4922 * ...
4923 *
4924 * Minimum length is 61 bytes.
4925 */
4926 if( in_len < 61 ||
4927 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4928 in[3] != 0 || in[4] != 0 ||
4929 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4930 {
4931 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4932 }
4933
4934 sid_len = in[59];
4935 if( sid_len > in_len - 61 )
4936 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4937
4938 cookie_len = in[60 + sid_len];
4939 if( cookie_len > in_len - 60 )
4940 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4941
4942 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4943 cli_id, cli_id_len ) == 0 )
4944 {
4945 /* Valid cookie */
4946 return( 0 );
4947 }
4948
4949 /*
4950 * If we get here, we've got an invalid cookie, let's prepare HVR.
4951 *
4952 * 0-0 ContentType type; copied
4953 * 1-2 ProtocolVersion version; copied
4954 * 3-4 uint16 epoch; copied
4955 * 5-10 uint48 sequence_number; copied
4956 * 11-12 uint16 length; olen - 13
4957 *
4958 * 13-13 HandshakeType msg_type; hello_verify_request
4959 * 14-16 uint24 length; olen - 25
4960 * 17-18 uint16 message_seq; copied
4961 * 19-21 uint24 fragment_offset; copied
4962 * 22-24 uint24 fragment_length; olen - 25
4963 *
4964 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4965 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4966 *
4967 * Minimum length is 28.
4968 */
4969 if( buf_len < 28 )
4970 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4971
4972 /* Copy most fields and adapt others */
4973 memcpy( obuf, in, 25 );
4974 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4975 obuf[25] = 0xfe;
4976 obuf[26] = 0xff;
4977
4978 /* Generate and write actual cookie */
4979 p = obuf + 28;
4980 if( f_cookie_write( p_cookie,
4981 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4982 {
4983 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4984 }
4985
4986 *olen = p - obuf;
4987
4988 /* Go back and fill length fields */
4989 obuf[27] = (unsigned char)( *olen - 28 );
4990
4991 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4992 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4993 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4994
4995 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4996 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4997
4998 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4999}
5000
5001/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005002 * Handle possible client reconnect with the same UDP quadruplet
5003 * (RFC 6347 Section 4.2.8).
5004 *
5005 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5006 * that looks like a ClientHello.
5007 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005008 * - if the input looks like a ClientHello without cookies,
5009 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005010 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005011 * - if the input looks like a ClientHello with a valid cookie,
5012 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005013 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005014 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005015 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005016 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005017 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5018 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005019 */
5020static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5021{
5022 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005023 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005024
Hanno Becker2fddd372019-07-10 14:37:41 +01005025 if( ssl->conf->f_cookie_write == NULL ||
5026 ssl->conf->f_cookie_check == NULL )
5027 {
5028 /* If we can't use cookies to verify reachability of the peer,
5029 * drop the record. */
5030 return( 0 );
5031 }
5032
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005033 ret = ssl_check_dtls_clihlo_cookie(
5034 ssl->conf->f_cookie_write,
5035 ssl->conf->f_cookie_check,
5036 ssl->conf->p_cookie,
5037 ssl->cli_id, ssl->cli_id_len,
5038 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005039 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005040
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005041 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5042
5043 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005044 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005045 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005046 * If the error is permanent we'll catch it later,
5047 * if it's not, then hopefully it'll work next time. */
5048 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01005049 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005050 }
5051
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005052 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005053 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005054 /* Got a valid cookie, partially reset context */
5055 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5056 {
5057 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5058 return( ret );
5059 }
5060
5061 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005062 }
5063
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005064 return( ret );
5065}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005066#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005067
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005068static int ssl_check_record_type( uint8_t record_type )
5069{
5070 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5071 record_type != MBEDTLS_SSL_MSG_ALERT &&
5072 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5073 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5074 {
5075 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5076 }
5077
5078 return( 0 );
5079}
5080
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005081/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005082 * ContentType type;
5083 * ProtocolVersion version;
5084 * uint16 epoch; // DTLS only
5085 * uint48 sequence_number; // DTLS only
5086 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005087 *
5088 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005089 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005090 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5091 *
5092 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005093 * 1. proceed with the record if this function returns 0
5094 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5095 * 3. return CLIENT_RECONNECT if this function return that value
5096 * 4. drop the whole datagram if this function returns anything else.
5097 * Point 2 is needed when the peer is resending, and we have already received
5098 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005099 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005100static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005101 unsigned char *buf,
5102 size_t len,
5103 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005104{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005105 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005106
Hanno Beckere5e7e782019-07-11 12:29:35 +01005107 size_t const rec_hdr_type_offset = 0;
5108 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005109
Hanno Beckere5e7e782019-07-11 12:29:35 +01005110 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5111 rec_hdr_type_len;
5112 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005113
Hanno Beckere5e7e782019-07-11 12:29:35 +01005114 size_t const rec_hdr_ctr_len = 8;
5115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005116 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005117 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5118 rec_hdr_version_len;
5119
Hanno Beckera0e20d02019-05-15 14:03:01 +01005120#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005121 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5122 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005123 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005124#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5125#endif /* MBEDTLS_SSL_PROTO_DTLS */
5126
5127 size_t rec_hdr_len_offset; /* To be determined */
5128 size_t const rec_hdr_len_len = 2;
5129
5130 /*
5131 * Check minimum lengths for record header.
5132 */
5133
5134#if defined(MBEDTLS_SSL_PROTO_DTLS)
5135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5136 {
5137 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5138 }
5139 else
5140#endif /* MBEDTLS_SSL_PROTO_DTLS */
5141 {
5142 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5143 }
5144
5145 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5146 {
5147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5148 (unsigned) len,
5149 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5150 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5151 }
5152
5153 /*
5154 * Parse and validate record content type
5155 */
5156
5157 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005158
5159 /* Check record content type */
5160#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5161 rec->cid_len = 0;
5162
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005163 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005164 ssl->conf->cid_len != 0 &&
5165 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005166 {
5167 /* Shift pointers to account for record header including CID
5168 * struct {
5169 * ContentType special_type = tls12_cid;
5170 * ProtocolVersion version;
5171 * uint16 epoch;
5172 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005173 * opaque cid[cid_length]; // Additional field compared to
5174 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005175 * uint16 length;
5176 * opaque enc_content[DTLSCiphertext.length];
5177 * } DTLSCiphertext;
5178 */
5179
5180 /* So far, we only support static CID lengths
5181 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005182 rec_hdr_cid_len = ssl->conf->cid_len;
5183 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005184
Hanno Beckere5e7e782019-07-11 12:29:35 +01005185 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005186 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5188 (unsigned) len,
5189 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005190 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005191 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005192
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005193 /* configured CID len is guaranteed at most 255, see
5194 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5195 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005196 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005197 }
5198 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005199#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005200 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005201 if( ssl_check_record_type( rec->type ) )
5202 {
Hanno Becker54229812019-07-12 14:40:00 +01005203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5204 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005205 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5206 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005207 }
5208
Hanno Beckere5e7e782019-07-11 12:29:35 +01005209 /*
5210 * Parse and validate record version
5211 */
5212
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005213 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5214 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005215 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5216 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005217 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005218
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005219 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5222 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005223 }
5224
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005225 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5228 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005229 }
5230
Hanno Beckere5e7e782019-07-11 12:29:35 +01005231 /*
5232 * Parse/Copy record sequence number.
5233 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005234
Hanno Beckere5e7e782019-07-11 12:29:35 +01005235#if defined(MBEDTLS_SSL_PROTO_DTLS)
5236 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005237 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005238 /* Copy explicit record sequence number from input buffer. */
5239 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5240 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005241 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005242 else
5243#endif /* MBEDTLS_SSL_PROTO_DTLS */
5244 {
5245 /* Copy implicit record sequence number from SSL context structure. */
5246 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5247 }
Paul Bakker40e46942009-01-03 21:51:57 +00005248
Hanno Beckere5e7e782019-07-11 12:29:35 +01005249 /*
5250 * Parse record length.
5251 */
5252
Hanno Beckere5e7e782019-07-11 12:29:35 +01005253 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005254 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5255 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005256 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005257
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005259 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005260 rec->type,
5261 major_ver, minor_ver, rec->data_len ) );
5262
5263 rec->buf = buf;
5264 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005265
Hanno Beckerd417cc92019-07-26 08:20:27 +01005266 if( rec->data_len == 0 )
5267 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005268
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005269 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005270 * DTLS-related tests.
5271 * Check epoch before checking length constraint because
5272 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5273 * message gets duplicated before the corresponding Finished message,
5274 * the second ChangeCipherSpec should be discarded because it belongs
5275 * to an old epoch, but not because its length is shorter than
5276 * the minimum record length for packets using the new record transform.
5277 * Note that these two kinds of failures are handled differently,
5278 * as an unexpected record is silently skipped but an invalid
5279 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005280 */
5281#if defined(MBEDTLS_SSL_PROTO_DTLS)
5282 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5283 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005284 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005285
Hanno Becker955a5c92019-07-10 17:12:07 +01005286 /* Check that the datagram is large enough to contain a record
5287 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005288 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005289 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5291 (unsigned) len,
5292 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005293 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5294 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005295
Hanno Becker37cfe732019-07-10 17:20:01 +01005296 /* Records from other, non-matching epochs are silently discarded.
5297 * (The case of same-port Client reconnects must be considered in
5298 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005299 if( rec_epoch != ssl->in_epoch )
5300 {
5301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5302 "expected %d, received %d",
5303 ssl->in_epoch, rec_epoch ) );
5304
Hanno Becker552f7472019-07-19 10:59:12 +01005305 /* Records from the next epoch are considered for buffering
5306 * (concretely: early Finished messages). */
5307 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005308 {
Hanno Becker552f7472019-07-19 10:59:12 +01005309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5310 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005311 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005312
Hanno Becker2fddd372019-07-10 14:37:41 +01005313 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005314 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005315#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005316 /* For records from the correct epoch, check whether their
5317 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005318 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005319 {
5320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5321 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5322 }
5323#endif
5324 }
5325#endif /* MBEDTLS_SSL_PROTO_DTLS */
5326
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005327 return( 0 );
5328}
Paul Bakker5121ce52009-01-03 21:22:43 +00005329
Paul Bakker5121ce52009-01-03 21:22:43 +00005330
Hanno Becker2fddd372019-07-10 14:37:41 +01005331#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5332static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5333{
5334 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5335
5336 /*
5337 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5338 * access the first byte of record content (handshake type), as we
5339 * have an active transform (possibly iv_len != 0), so use the
5340 * fact that the record header len is 13 instead.
5341 */
5342 if( rec_epoch == 0 &&
5343 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5344 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5345 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5346 ssl->in_left > 13 &&
5347 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5348 {
5349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5350 "from the same port" ) );
5351 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005352 }
5353
5354 return( 0 );
5355}
Hanno Becker2fddd372019-07-10 14:37:41 +01005356#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005357
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005358/*
5359 * If applicable, decrypt (and decompress) record content
5360 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005361static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5362 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005363{
5364 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005367 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5370 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 ret = mbedtls_ssl_hw_record_read( ssl );
5375 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5378 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005379 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005380
5381 if( ret == 0 )
5382 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005383 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005385 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005386 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005387 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005388
Hanno Beckera18d1322018-01-03 14:27:32 +00005389 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005390 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005393
Hanno Beckera0e20d02019-05-15 14:03:01 +01005394#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005395 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5396 ssl->conf->ignore_unexpected_cid
5397 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5398 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005400 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005401 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005402#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005403
Paul Bakker5121ce52009-01-03 21:22:43 +00005404 return( ret );
5405 }
5406
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005407 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005408 {
5409 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005410 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005411 }
5412
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005413 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005414 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005415
Hanno Beckera0e20d02019-05-15 14:03:01 +01005416#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005417 /* We have already checked the record content type
5418 * in ssl_parse_record_header(), failing or silently
5419 * dropping the record in the case of an unknown type.
5420 *
5421 * Since with the use of CIDs, the record content type
5422 * might change during decryption, re-check the record
5423 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005424 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005425 {
5426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5427 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5428 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005429#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005430
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005431 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005432 {
5433#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5434 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005435 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005436 {
5437 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5439 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5440 }
5441#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5442
5443 ssl->nb_zero++;
5444
5445 /*
5446 * Three or more empty messages may be a DoS attack
5447 * (excessive CPU consumption).
5448 */
5449 if( ssl->nb_zero > 3 )
5450 {
5451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005452 "messages, possible DoS attack" ) );
5453 /* Treat the records as if they were not properly authenticated,
5454 * thereby failing the connection if we see more than allowed
5455 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005456 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5457 }
5458 }
5459 else
5460 ssl->nb_zero = 0;
5461
5462#if defined(MBEDTLS_SSL_PROTO_DTLS)
5463 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5464 {
5465 ; /* in_ctr read from peer, not maintained internally */
5466 }
5467 else
5468#endif
5469 {
5470 unsigned i;
5471 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5472 if( ++ssl->in_ctr[i - 1] != 0 )
5473 break;
5474
5475 /* The loop goes to its end iff the counter is wrapping */
5476 if( i == ssl_ep_len( ssl ) )
5477 {
5478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5479 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5480 }
5481 }
5482
Paul Bakker5121ce52009-01-03 21:22:43 +00005483 }
5484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005486 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005488 {
5489 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005492 return( ret );
5493 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005494 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005498 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005501 }
5502#endif
5503
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005504 /* Check actual (decrypted) record content length against
5505 * configured maximum. */
5506 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5507 {
5508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5509 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5510 }
5511
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005512 return( 0 );
5513}
5514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005516
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005517/*
5518 * Read a record.
5519 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005520 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5521 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5522 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005523 */
Hanno Becker1097b342018-08-15 14:09:41 +01005524
5525/* Helper functions for mbedtls_ssl_read_record(). */
5526static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005527static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5528static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005529
Hanno Becker327c93b2018-08-15 13:56:18 +01005530int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005531 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005532{
5533 int ret;
5534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005536
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005537 if( ssl->keep_current_message == 0 )
5538 {
5539 do {
Simon Butcher99000142016-10-13 17:21:01 +01005540
Hanno Becker26994592018-08-15 14:14:59 +01005541 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005542 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005543 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005544
Hanno Beckere74d5562018-08-15 14:26:08 +01005545 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005546 {
Hanno Becker40f50842018-08-15 14:48:01 +01005547#if defined(MBEDTLS_SSL_PROTO_DTLS)
5548 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005549
Hanno Becker40f50842018-08-15 14:48:01 +01005550 /* We only check for buffered messages if the
5551 * current datagram is fully consumed. */
5552 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005553 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005554 {
Hanno Becker40f50842018-08-15 14:48:01 +01005555 if( ssl_load_buffered_message( ssl ) == 0 )
5556 have_buffered = 1;
5557 }
5558
5559 if( have_buffered == 0 )
5560#endif /* MBEDTLS_SSL_PROTO_DTLS */
5561 {
5562 ret = ssl_get_next_record( ssl );
5563 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5564 continue;
5565
5566 if( ret != 0 )
5567 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005568 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005569 return( ret );
5570 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005571 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005572 }
5573
5574 ret = mbedtls_ssl_handle_message_type( ssl );
5575
Hanno Becker40f50842018-08-15 14:48:01 +01005576#if defined(MBEDTLS_SSL_PROTO_DTLS)
5577 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5578 {
5579 /* Buffer future message */
5580 ret = ssl_buffer_message( ssl );
5581 if( ret != 0 )
5582 return( ret );
5583
5584 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5585 }
5586#endif /* MBEDTLS_SSL_PROTO_DTLS */
5587
Hanno Becker90333da2017-10-10 11:27:13 +01005588 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5589 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005590
5591 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005592 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005593 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005594 return( ret );
5595 }
5596
Hanno Becker327c93b2018-08-15 13:56:18 +01005597 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005598 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005599 {
5600 mbedtls_ssl_update_handshake_status( ssl );
5601 }
Simon Butcher99000142016-10-13 17:21:01 +01005602 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005603 else
Simon Butcher99000142016-10-13 17:21:01 +01005604 {
Hanno Becker02f59072018-08-15 14:00:24 +01005605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005606 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005607 }
5608
5609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5610
5611 return( 0 );
5612}
5613
Hanno Becker40f50842018-08-15 14:48:01 +01005614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005615static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005616{
Hanno Becker40f50842018-08-15 14:48:01 +01005617 if( ssl->in_left > ssl->next_record_offset )
5618 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005619
Hanno Becker40f50842018-08-15 14:48:01 +01005620 return( 0 );
5621}
5622
5623static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5624{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005625 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005626 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005627 int ret = 0;
5628
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005629 if( hs == NULL )
5630 return( -1 );
5631
Hanno Beckere00ae372018-08-20 09:39:42 +01005632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5633
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005634 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5635 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5636 {
5637 /* Check if we have seen a ChangeCipherSpec before.
5638 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005639 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005640 {
5641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5642 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005643 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005644 }
5645
Hanno Becker39b8bc92018-08-28 17:17:13 +01005646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005647 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5648 ssl->in_msglen = 1;
5649 ssl->in_msg[0] = 1;
5650
5651 /* As long as they are equal, the exact value doesn't matter. */
5652 ssl->in_left = 0;
5653 ssl->next_record_offset = 0;
5654
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005655 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005656 goto exit;
5657 }
Hanno Becker37f95322018-08-16 13:55:32 +01005658
Hanno Beckerb8f50142018-08-28 10:01:34 +01005659#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005660 /* Debug only */
5661 {
5662 unsigned offset;
5663 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5664 {
5665 hs_buf = &hs->buffering.hs[offset];
5666 if( hs_buf->is_valid == 1 )
5667 {
5668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5669 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005670 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005671 }
5672 }
5673 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005674#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005675
5676 /* Check if we have buffered and/or fully reassembled the
5677 * next handshake message. */
5678 hs_buf = &hs->buffering.hs[0];
5679 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5680 {
5681 /* Synthesize a record containing the buffered HS message. */
5682 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5683 ( hs_buf->data[2] << 8 ) |
5684 hs_buf->data[3];
5685
5686 /* Double-check that we haven't accidentally buffered
5687 * a message that doesn't fit into the input buffer. */
5688 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5689 {
5690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5692 }
5693
5694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5695 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5696 hs_buf->data, msg_len + 12 );
5697
5698 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5699 ssl->in_hslen = msg_len + 12;
5700 ssl->in_msglen = msg_len + 12;
5701 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5702
5703 ret = 0;
5704 goto exit;
5705 }
5706 else
5707 {
5708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5709 hs->in_msg_seq ) );
5710 }
5711
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005712 ret = -1;
5713
5714exit:
5715
5716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5717 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005718}
5719
Hanno Beckera02b0b42018-08-21 17:20:27 +01005720static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5721 size_t desired )
5722{
5723 int offset;
5724 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5726 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005727
Hanno Becker01315ea2018-08-21 17:22:17 +01005728 /* Get rid of future records epoch first, if such exist. */
5729 ssl_free_buffered_record( ssl );
5730
5731 /* Check if we have enough space available now. */
5732 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5733 hs->buffering.total_bytes_buffered ) )
5734 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005736 return( 0 );
5737 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005738
Hanno Becker4f432ad2018-08-28 10:02:32 +01005739 /* We don't have enough space to buffer the next expected handshake
5740 * message. Remove buffers used for future messages to gain space,
5741 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005742 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5743 offset >= 0; offset-- )
5744 {
5745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5746 offset ) );
5747
Hanno Beckerb309b922018-08-23 13:18:05 +01005748 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005749
5750 /* Check if we have enough space available now. */
5751 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5752 hs->buffering.total_bytes_buffered ) )
5753 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005755 return( 0 );
5756 }
5757 }
5758
5759 return( -1 );
5760}
5761
Hanno Becker40f50842018-08-15 14:48:01 +01005762static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5763{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005764 int ret = 0;
5765 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5766
5767 if( hs == NULL )
5768 return( 0 );
5769
5770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5771
5772 switch( ssl->in_msgtype )
5773 {
5774 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005776
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005777 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005778 break;
5779
5780 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005781 {
5782 unsigned recv_msg_seq_offset;
5783 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5784 mbedtls_ssl_hs_buffer *hs_buf;
5785 size_t msg_len = ssl->in_hslen - 12;
5786
5787 /* We should never receive an old handshake
5788 * message - double-check nonetheless. */
5789 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5790 {
5791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5792 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5793 }
5794
5795 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5796 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5797 {
5798 /* Silently ignore -- message too far in the future */
5799 MBEDTLS_SSL_DEBUG_MSG( 2,
5800 ( "Ignore future HS message with sequence number %u, "
5801 "buffering window %u - %u",
5802 recv_msg_seq, ssl->handshake->in_msg_seq,
5803 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5804
5805 goto exit;
5806 }
5807
5808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5809 recv_msg_seq, recv_msg_seq_offset ) );
5810
5811 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5812
5813 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005814 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005815 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005816 size_t reassembly_buf_sz;
5817
Hanno Becker37f95322018-08-16 13:55:32 +01005818 hs_buf->is_fragmented =
5819 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5820
5821 /* We copy the message back into the input buffer
5822 * after reassembly, so check that it's not too large.
5823 * This is an implementation-specific limitation
5824 * and not one from the standard, hence it is not
5825 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005826 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005827 {
5828 /* Ignore message */
5829 goto exit;
5830 }
5831
Hanno Beckere0b150f2018-08-21 15:51:03 +01005832 /* Check if we have enough space to buffer the message. */
5833 if( hs->buffering.total_bytes_buffered >
5834 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5835 {
5836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5838 }
5839
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005840 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5841 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005842
5843 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5844 hs->buffering.total_bytes_buffered ) )
5845 {
5846 if( recv_msg_seq_offset > 0 )
5847 {
5848 /* If we can't buffer a future message because
5849 * of space limitations -- ignore. */
5850 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",
5851 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5852 (unsigned) hs->buffering.total_bytes_buffered ) );
5853 goto exit;
5854 }
Hanno Beckere1801392018-08-21 16:51:05 +01005855 else
5856 {
5857 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",
5858 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5859 (unsigned) hs->buffering.total_bytes_buffered ) );
5860 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005861
Hanno Beckera02b0b42018-08-21 17:20:27 +01005862 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005863 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005864 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",
5865 (unsigned) msg_len,
5866 (unsigned) reassembly_buf_sz,
5867 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005868 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005869 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5870 goto exit;
5871 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005872 }
5873
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5875 msg_len ) );
5876
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005877 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5878 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005879 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005880 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005881 goto exit;
5882 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005883 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005884
5885 /* Prepare final header: copy msg_type, length and message_seq,
5886 * then add standardised fragment_offset and fragment_length */
5887 memcpy( hs_buf->data, ssl->in_msg, 6 );
5888 memset( hs_buf->data + 6, 0, 3 );
5889 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5890
5891 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005892
5893 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005894 }
5895 else
5896 {
5897 /* Make sure msg_type and length are consistent */
5898 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5899 {
5900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5901 /* Ignore */
5902 goto exit;
5903 }
5904 }
5905
Hanno Becker4422bbb2018-08-20 09:40:19 +01005906 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005907 {
5908 size_t frag_len, frag_off;
5909 unsigned char * const msg = hs_buf->data + 12;
5910
5911 /*
5912 * Check and copy current fragment
5913 */
5914
5915 /* Validation of header fields already done in
5916 * mbedtls_ssl_prepare_handshake_record(). */
5917 frag_off = ssl_get_hs_frag_off( ssl );
5918 frag_len = ssl_get_hs_frag_len( ssl );
5919
5920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5921 frag_off, frag_len ) );
5922 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5923
5924 if( hs_buf->is_fragmented )
5925 {
5926 unsigned char * const bitmask = msg + msg_len;
5927 ssl_bitmask_set( bitmask, frag_off, frag_len );
5928 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5929 msg_len ) == 0 );
5930 }
5931 else
5932 {
5933 hs_buf->is_complete = 1;
5934 }
5935
5936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5937 hs_buf->is_complete ? "" : "not yet " ) );
5938 }
5939
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005940 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005941 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005942
5943 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005944 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005945 break;
5946 }
5947
5948exit:
5949
5950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5951 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005952}
5953#endif /* MBEDTLS_SSL_PROTO_DTLS */
5954
Hanno Becker1097b342018-08-15 14:09:41 +01005955static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005956{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005957 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005958 * Consume last content-layer message and potentially
5959 * update in_msglen which keeps track of the contents'
5960 * consumption state.
5961 *
5962 * (1) Handshake messages:
5963 * Remove last handshake message, move content
5964 * and adapt in_msglen.
5965 *
5966 * (2) Alert messages:
5967 * Consume whole record content, in_msglen = 0.
5968 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005969 * (3) Change cipher spec:
5970 * Consume whole record content, in_msglen = 0.
5971 *
5972 * (4) Application data:
5973 * Don't do anything - the record layer provides
5974 * the application data as a stream transport
5975 * and consumes through mbedtls_ssl_read only.
5976 *
5977 */
5978
5979 /* Case (1): Handshake messages */
5980 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005981 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005982 /* Hard assertion to be sure that no application data
5983 * is in flight, as corrupting ssl->in_msglen during
5984 * ssl->in_offt != NULL is fatal. */
5985 if( ssl->in_offt != NULL )
5986 {
5987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5989 }
5990
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005991 /*
5992 * Get next Handshake message in the current record
5993 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005994
Hanno Becker4a810fb2017-05-24 16:27:30 +01005995 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005996 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005997 * current handshake content: If DTLS handshake
5998 * fragmentation is used, that's the fragment
5999 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006000 * size here is faulty and should be changed at
6001 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006002 * (2) While it doesn't seem to cause problems, one
6003 * has to be very careful not to assume that in_hslen
6004 * is always <= in_msglen in a sensible communication.
6005 * Again, it's wrong for DTLS handshake fragmentation.
6006 * The following check is therefore mandatory, and
6007 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006008 * Additionally, ssl->in_hslen might be arbitrarily out of
6009 * bounds after handling a DTLS message with an unexpected
6010 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006011 */
6012 if( ssl->in_hslen < ssl->in_msglen )
6013 {
6014 ssl->in_msglen -= ssl->in_hslen;
6015 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6016 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006017
Hanno Becker4a810fb2017-05-24 16:27:30 +01006018 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6019 ssl->in_msg, ssl->in_msglen );
6020 }
6021 else
6022 {
6023 ssl->in_msglen = 0;
6024 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006025
Hanno Becker4a810fb2017-05-24 16:27:30 +01006026 ssl->in_hslen = 0;
6027 }
6028 /* Case (4): Application data */
6029 else if( ssl->in_offt != NULL )
6030 {
6031 return( 0 );
6032 }
6033 /* Everything else (CCS & Alerts) */
6034 else
6035 {
6036 ssl->in_msglen = 0;
6037 }
6038
Hanno Becker1097b342018-08-15 14:09:41 +01006039 return( 0 );
6040}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006041
Hanno Beckere74d5562018-08-15 14:26:08 +01006042static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6043{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006044 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006045 return( 1 );
6046
6047 return( 0 );
6048}
6049
Hanno Becker5f066e72018-08-16 14:56:31 +01006050#if defined(MBEDTLS_SSL_PROTO_DTLS)
6051
6052static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6053{
6054 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6055 if( hs == NULL )
6056 return;
6057
Hanno Becker01315ea2018-08-21 17:22:17 +01006058 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006059 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006060 hs->buffering.total_bytes_buffered -=
6061 hs->buffering.future_record.len;
6062
6063 mbedtls_free( hs->buffering.future_record.data );
6064 hs->buffering.future_record.data = NULL;
6065 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006066}
6067
6068static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6069{
6070 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6071 unsigned char * rec;
6072 size_t rec_len;
6073 unsigned rec_epoch;
6074
6075 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6076 return( 0 );
6077
6078 if( hs == NULL )
6079 return( 0 );
6080
Hanno Becker5f066e72018-08-16 14:56:31 +01006081 rec = hs->buffering.future_record.data;
6082 rec_len = hs->buffering.future_record.len;
6083 rec_epoch = hs->buffering.future_record.epoch;
6084
6085 if( rec == NULL )
6086 return( 0 );
6087
Hanno Becker4cb782d2018-08-20 11:19:05 +01006088 /* Only consider loading future records if the
6089 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006090 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006091 return( 0 );
6092
Hanno Becker5f066e72018-08-16 14:56:31 +01006093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6094
6095 if( rec_epoch != ssl->in_epoch )
6096 {
6097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6098 goto exit;
6099 }
6100
6101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6102
6103 /* Double-check that the record is not too large */
6104 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6105 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6106 {
6107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6108 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6109 }
6110
6111 memcpy( ssl->in_hdr, rec, rec_len );
6112 ssl->in_left = rec_len;
6113 ssl->next_record_offset = 0;
6114
6115 ssl_free_buffered_record( ssl );
6116
6117exit:
6118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6119 return( 0 );
6120}
6121
Hanno Becker519f15d2019-07-11 12:43:20 +01006122static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6123 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006124{
6125 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006126
6127 /* Don't buffer future records outside handshakes. */
6128 if( hs == NULL )
6129 return( 0 );
6130
6131 /* Only buffer handshake records (we are only interested
6132 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006133 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006134 return( 0 );
6135
6136 /* Don't buffer more than one future epoch record. */
6137 if( hs->buffering.future_record.data != NULL )
6138 return( 0 );
6139
Hanno Becker01315ea2018-08-21 17:22:17 +01006140 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006141 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006142 hs->buffering.total_bytes_buffered ) )
6143 {
6144 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",
Hanno Becker519f15d2019-07-11 12:43:20 +01006145 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006146 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006147 return( 0 );
6148 }
6149
Hanno Becker5f066e72018-08-16 14:56:31 +01006150 /* Buffer record */
6151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6152 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006153 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006154
6155 /* ssl_parse_record_header() only considers records
6156 * of the next epoch as candidates for buffering. */
6157 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006158 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006159
6160 hs->buffering.future_record.data =
6161 mbedtls_calloc( 1, hs->buffering.future_record.len );
6162 if( hs->buffering.future_record.data == NULL )
6163 {
6164 /* If we run out of RAM trying to buffer a
6165 * record from the next epoch, just ignore. */
6166 return( 0 );
6167 }
6168
Hanno Becker519f15d2019-07-11 12:43:20 +01006169 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006170
Hanno Becker519f15d2019-07-11 12:43:20 +01006171 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006172 return( 0 );
6173}
6174
6175#endif /* MBEDTLS_SSL_PROTO_DTLS */
6176
Hanno Beckere74d5562018-08-15 14:26:08 +01006177static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006178{
6179 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006180 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006181
Hanno Becker5f066e72018-08-16 14:56:31 +01006182#if defined(MBEDTLS_SSL_PROTO_DTLS)
6183 /* We might have buffered a future record; if so,
6184 * and if the epoch matches now, load it.
6185 * On success, this call will set ssl->in_left to
6186 * the length of the buffered record, so that
6187 * the calls to ssl_fetch_input() below will
6188 * essentially be no-ops. */
6189 ret = ssl_load_buffered_record( ssl );
6190 if( ret != 0 )
6191 return( ret );
6192#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006193
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006194 /* Ensure that we have enough space available for the default form
6195 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6196 * with no space for CIDs counted in). */
6197 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6198 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006201 return( ret );
6202 }
6203
Hanno Beckere5e7e782019-07-11 12:29:35 +01006204 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6205 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006208 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006209 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006210 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6211 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006212 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006213 if( ret != 0 )
6214 return( ret );
6215
6216 /* Fall through to handling of unexpected records */
6217 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6218 }
6219
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006220 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6221 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006222#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006223 /* Reset in pointers to default state for TLS/DTLS records,
6224 * assuming no CID and no offset between record content and
6225 * record plaintext. */
6226 ssl_update_in_pointers( ssl );
6227
Hanno Becker7ae20e02019-07-12 08:33:49 +01006228 /* Setup internal message pointers from record structure. */
6229 ssl->in_msgtype = rec.type;
6230#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6231 ssl->in_len = ssl->in_cid + rec.cid_len;
6232#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6233 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6234 ssl->in_msglen = rec.data_len;
6235
Hanno Becker2fddd372019-07-10 14:37:41 +01006236 ret = ssl_check_client_reconnect( ssl );
6237 if( ret != 0 )
6238 return( ret );
6239#endif
6240
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006241 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006242 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006243
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6245 "(header)" ) );
6246 }
6247 else
6248 {
6249 /* Skip invalid record and the rest of the datagram */
6250 ssl->next_record_offset = 0;
6251 ssl->in_left = 0;
6252
6253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6254 "(header)" ) );
6255 }
6256
6257 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006258 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006259 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006260 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006261#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006262 {
6263 return( ret );
6264 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006265 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006268 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006269 {
Hanno Beckera8814792019-07-10 15:01:45 +01006270 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006271 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006272 if( ssl->next_record_offset < ssl->in_left )
6273 {
6274 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6275 }
6276 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006277 else
6278#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006279 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006280 /*
6281 * Fetch record contents from underlying transport.
6282 */
Hanno Beckera3175662019-07-11 12:50:29 +01006283 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006284 if( ret != 0 )
6285 {
6286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6287 return( ret );
6288 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006289
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006290 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006291 }
6292
6293 /*
6294 * Decrypt record contents.
6295 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006296
Hanno Beckerfdf66042019-07-11 13:07:45 +01006297 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006300 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006301 {
6302 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006303 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006304 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006305 /* Except when waiting for Finished as a bad mac here
6306 * probably means something went wrong in the handshake
6307 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6308 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6309 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6310 {
6311#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6312 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6313 {
6314 mbedtls_ssl_send_alert_message( ssl,
6315 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6316 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6317 }
6318#endif
6319 return( ret );
6320 }
6321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006323 if( ssl->conf->badmac_limit != 0 &&
6324 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6327 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006328 }
6329#endif
6330
Hanno Becker4a810fb2017-05-24 16:27:30 +01006331 /* As above, invalid records cause
6332 * dismissal of the whole datagram. */
6333
6334 ssl->next_record_offset = 0;
6335 ssl->in_left = 0;
6336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006338 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006339 }
6340
6341 return( ret );
6342 }
6343 else
6344#endif
6345 {
6346 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6348 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350 mbedtls_ssl_send_alert_message( ssl,
6351 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6352 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006353 }
6354#endif
6355 return( ret );
6356 }
6357 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006358
Hanno Becker44d89b22019-07-12 09:40:44 +01006359
6360 /* Reset in pointers to default state for TLS/DTLS records,
6361 * assuming no CID and no offset between record content and
6362 * record plaintext. */
6363 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006364#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6365 ssl->in_len = ssl->in_cid + rec.cid_len;
6366#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6367 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006368
Hanno Becker8685c822019-07-12 09:37:30 +01006369 /* The record content type may change during decryption,
6370 * so re-read it. */
6371 ssl->in_msgtype = rec.type;
6372 /* Also update the input buffer, because unfortunately
6373 * the server-side ssl_parse_client_hello() reparses the
6374 * record header when receiving a ClientHello initiating
6375 * a renegotiation. */
6376 ssl->in_hdr[0] = rec.type;
6377 ssl->in_msg = rec.buf + rec.data_offset;
6378 ssl->in_msglen = rec.data_len;
6379 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6380 ssl->in_len[1] = (unsigned char)( rec.data_len );
6381
Simon Butcher99000142016-10-13 17:21:01 +01006382 return( 0 );
6383}
6384
6385int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6386{
6387 int ret;
6388
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006389 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006390 * Handle particular types of records
6391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 {
Simon Butcher99000142016-10-13 17:21:01 +01006394 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6395 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006396 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006398 }
6399
Hanno Beckere678eaa2018-08-21 14:57:46 +01006400 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006401 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006402 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006403 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6405 ssl->in_msglen ) );
6406 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006407 }
6408
Hanno Beckere678eaa2018-08-21 14:57:46 +01006409 if( ssl->in_msg[0] != 1 )
6410 {
6411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6412 ssl->in_msg[0] ) );
6413 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6414 }
6415
6416#if defined(MBEDTLS_SSL_PROTO_DTLS)
6417 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6418 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6419 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6420 {
6421 if( ssl->handshake == NULL )
6422 {
6423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6424 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6425 }
6426
6427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6428 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6429 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006430#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006431 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006434 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006435 if( ssl->in_msglen != 2 )
6436 {
6437 /* Note: Standard allows for more than one 2 byte alert
6438 to be packed in a single message, but Mbed TLS doesn't
6439 currently support this. */
6440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6441 ssl->in_msglen ) );
6442 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6443 }
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006446 ssl->in_msg[0], ssl->in_msg[1] ) );
6447
6448 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006449 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006454 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006456 }
6457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6459 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6462 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006463 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006464
6465#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6466 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6467 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6468 {
Hanno Becker90333da2017-10-10 11:27:13 +01006469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006470 /* Will be handled when trying to parse ServerHello */
6471 return( 0 );
6472 }
6473#endif
6474
6475#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6476 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6477 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6478 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6479 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6480 {
6481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6482 /* Will be handled in mbedtls_ssl_parse_certificate() */
6483 return( 0 );
6484 }
6485#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6486
6487 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006488 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 }
6490
Hanno Beckerc76c6192017-06-06 10:03:17 +01006491#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006492 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006493 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006494 /* Drop unexpected ApplicationData records,
6495 * except at the beginning of renegotiations */
6496 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6497 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6498#if defined(MBEDTLS_SSL_RENEGOTIATION)
6499 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6500 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006501#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006502 )
6503 {
6504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6505 return( MBEDTLS_ERR_SSL_NON_FATAL );
6506 }
6507
6508 if( ssl->handshake != NULL &&
6509 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6510 {
6511 ssl_handshake_wrapup_free_hs_transform( ssl );
6512 }
6513 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006514#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006515
Paul Bakker5121ce52009-01-03 21:22:43 +00006516 return( 0 );
6517}
6518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006520{
6521 int ret;
6522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6524 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6525 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006526 {
6527 return( ret );
6528 }
6529
6530 return( 0 );
6531}
6532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006534 unsigned char level,
6535 unsigned char message )
6536{
6537 int ret;
6538
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006539 if( ssl == NULL || ssl->conf == NULL )
6540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006546 ssl->out_msglen = 2;
6547 ssl->out_msg[0] = level;
6548 ssl->out_msg[1] = message;
6549
Hanno Becker67bc7c32018-08-06 11:33:50 +01006550 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006553 return( ret );
6554 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006556
6557 return( 0 );
6558}
6559
Hanno Beckerb9d44792019-02-08 07:19:04 +00006560#if defined(MBEDTLS_X509_CRT_PARSE_C)
6561static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6562{
6563#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6564 if( session->peer_cert != NULL )
6565 {
6566 mbedtls_x509_crt_free( session->peer_cert );
6567 mbedtls_free( session->peer_cert );
6568 session->peer_cert = NULL;
6569 }
6570#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6571 if( session->peer_cert_digest != NULL )
6572 {
6573 /* Zeroization is not necessary. */
6574 mbedtls_free( session->peer_cert_digest );
6575 session->peer_cert_digest = NULL;
6576 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6577 session->peer_cert_digest_len = 0;
6578 }
6579#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6580}
6581#endif /* MBEDTLS_X509_CRT_PARSE_C */
6582
Paul Bakker5121ce52009-01-03 21:22:43 +00006583/*
6584 * Handshake functions
6585 */
Hanno Becker21489932019-02-05 13:20:55 +00006586#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006587/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006589{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006590 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6591 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594
Hanno Becker7177a882019-02-05 13:36:46 +00006595 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006598 ssl->state++;
6599 return( 0 );
6600 }
6601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006604}
6605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006607{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006608 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6609 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006612
Hanno Becker7177a882019-02-05 13:36:46 +00006613 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006616 ssl->state++;
6617 return( 0 );
6618 }
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6621 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006622}
Gilles Peskinef9828522017-05-03 12:28:43 +02006623
Hanno Becker21489932019-02-05 13:20:55 +00006624#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006625/* Some certificate support -> implement write and parse */
6626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006628{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006630 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006632 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6633 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006636
Hanno Becker7177a882019-02-05 13:36:46 +00006637 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006640 ssl->state++;
6641 return( 0 );
6642 }
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006645 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006646 {
6647 if( ssl->client_auth == 0 )
6648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006650 ssl->state++;
6651 return( 0 );
6652 }
6653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 /*
6656 * If using SSLv3 and got no cert, send an Alert message
6657 * (otherwise an empty Certificate message will be sent).
6658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6660 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 {
6662 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6664 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6665 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006668 goto write_msg;
6669 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006671 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672#endif /* MBEDTLS_SSL_CLI_C */
6673#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006674 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6679 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006680 }
6681 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006682#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006685
6686 /*
6687 * 0 . 0 handshake type
6688 * 1 . 3 handshake length
6689 * 4 . 6 length of all certs
6690 * 7 . 9 length of cert. 1
6691 * 10 . n-1 peer certificate
6692 * n . n+2 length of cert. 2
6693 * n+3 . ... upper level cert, etc.
6694 */
6695 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006697
Paul Bakker29087132010-03-21 21:03:34 +00006698 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006699 {
6700 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006701 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006704 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006706 }
6707
6708 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6709 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6710 ssl->out_msg[i + 2] = (unsigned char)( n );
6711
6712 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6713 i += n; crt = crt->next;
6714 }
6715
6716 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6717 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6718 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6719
6720 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6722 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006723
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006724#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006725write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006726#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006727
6728 ssl->state++;
6729
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006730 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006731 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006733 return( ret );
6734 }
6735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006737
Paul Bakkered27a042013-04-18 22:46:23 +02006738 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006739}
6740
Hanno Becker84879e32019-01-31 07:44:03 +00006741#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006742
6743#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006744static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6745 unsigned char *crt_buf,
6746 size_t crt_buf_len )
6747{
6748 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6749
6750 if( peer_crt == NULL )
6751 return( -1 );
6752
6753 if( peer_crt->raw.len != crt_buf_len )
6754 return( -1 );
6755
Hanno Becker46f34d02019-02-08 14:00:04 +00006756 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006757}
Hanno Becker177475a2019-02-05 17:02:46 +00006758#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6759static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6760 unsigned char *crt_buf,
6761 size_t crt_buf_len )
6762{
6763 int ret;
6764 unsigned char const * const peer_cert_digest =
6765 ssl->session->peer_cert_digest;
6766 mbedtls_md_type_t const peer_cert_digest_type =
6767 ssl->session->peer_cert_digest_type;
6768 mbedtls_md_info_t const * const digest_info =
6769 mbedtls_md_info_from_type( peer_cert_digest_type );
6770 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6771 size_t digest_len;
6772
6773 if( peer_cert_digest == NULL || digest_info == NULL )
6774 return( -1 );
6775
6776 digest_len = mbedtls_md_get_size( digest_info );
6777 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6778 return( -1 );
6779
6780 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6781 if( ret != 0 )
6782 return( -1 );
6783
6784 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6785}
6786#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006787#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006788
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006789/*
6790 * Once the certificate message is read, parse it into a cert chain and
6791 * perform basic checks, but leave actual verification to the caller
6792 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006793static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6794 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006795{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006796 int ret;
6797#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6798 int crt_cnt=0;
6799#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006800 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006801 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006806 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6807 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006809 }
6810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6812 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006815 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6816 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818 }
6819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006821
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006824 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006825 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006826
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006827 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6832 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834 }
6835
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006836 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6837 i += 3;
6838
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006839 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006840 while( i < ssl->in_hslen )
6841 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006842 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006843 if ( i + 3 > ssl->in_hslen ) {
6844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006845 mbedtls_ssl_send_alert_message( ssl,
6846 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6847 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006848 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6849 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006850 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6851 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006852 if( ssl->in_msg[i] != 0 )
6853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006855 mbedtls_ssl_send_alert_message( ssl,
6856 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6857 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006859 }
6860
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006861 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006862 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6863 | (unsigned int) ssl->in_msg[i + 2];
6864 i += 3;
6865
6866 if( n < 128 || i + n > ssl->in_hslen )
6867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006869 mbedtls_ssl_send_alert_message( ssl,
6870 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6871 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006873 }
6874
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006875 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006876#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6877 if( crt_cnt++ == 0 &&
6878 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6879 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006880 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006881 /* During client-side renegotiation, check that the server's
6882 * end-CRTs hasn't changed compared to the initial handshake,
6883 * mitigating the triple handshake attack. On success, reuse
6884 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6886 if( ssl_check_peer_crt_unchanged( ssl,
6887 &ssl->in_msg[i],
6888 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006889 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6891 mbedtls_ssl_send_alert_message( ssl,
6892 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6893 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6894 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006895 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006896
6897 /* Now we can safely free the original chain. */
6898 ssl_clear_peer_cert( ssl->session );
6899 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006900#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6901
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006902 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006903#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006904 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006905#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006906 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006907 * it in-place from the input buffer instead of making a copy. */
6908 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6909#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006910 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006911 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006912 case 0: /*ok*/
6913 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6914 /* Ignore certificate with an unknown algorithm: maybe a
6915 prior certificate was already trusted. */
6916 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006917
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006918 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6919 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6920 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006921
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006922 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6923 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6924 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006925
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006926 default:
6927 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6928 crt_parse_der_failed:
6929 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6930 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6931 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006932 }
6933
6934 i += n;
6935 }
6936
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006937 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006938 return( 0 );
6939}
6940
Hanno Becker4a55f632019-02-05 12:49:06 +00006941#if defined(MBEDTLS_SSL_SRV_C)
6942static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6943{
6944 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6945 return( -1 );
6946
6947#if defined(MBEDTLS_SSL_PROTO_SSL3)
6948 /*
6949 * Check if the client sent an empty certificate
6950 */
6951 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6952 {
6953 if( ssl->in_msglen == 2 &&
6954 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6955 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6956 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6957 {
6958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6959 return( 0 );
6960 }
6961
6962 return( -1 );
6963 }
6964#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6965
6966#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6967 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6968 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6969 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6970 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6971 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6972 {
6973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6974 return( 0 );
6975 }
6976
6977 return( -1 );
6978#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6979 MBEDTLS_SSL_PROTO_TLS1_2 */
6980}
6981#endif /* MBEDTLS_SSL_SRV_C */
6982
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006983/* Check if a certificate message is expected.
6984 * Return either
6985 * - SSL_CERTIFICATE_EXPECTED, or
6986 * - SSL_CERTIFICATE_SKIP
6987 * indicating whether a Certificate message is expected or not.
6988 */
6989#define SSL_CERTIFICATE_EXPECTED 0
6990#define SSL_CERTIFICATE_SKIP 1
6991static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6992 int authmode )
6993{
6994 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006995 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006996
6997 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6998 return( SSL_CERTIFICATE_SKIP );
6999
7000#if defined(MBEDTLS_SSL_SRV_C)
7001 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7002 {
7003 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7004 return( SSL_CERTIFICATE_SKIP );
7005
7006 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7007 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007008 ssl->session_negotiate->verify_result =
7009 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7010 return( SSL_CERTIFICATE_SKIP );
7011 }
7012 }
Hanno Becker84d9d272019-03-01 08:10:46 +00007013#else
7014 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007015#endif /* MBEDTLS_SSL_SRV_C */
7016
7017 return( SSL_CERTIFICATE_EXPECTED );
7018}
7019
Hanno Becker68636192019-02-05 14:36:34 +00007020static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7021 int authmode,
7022 mbedtls_x509_crt *chain,
7023 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007024{
Hanno Becker6bdfab22019-02-05 13:11:17 +00007025 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007026 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00007027 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007028 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00007029
Hanno Becker8927c832019-04-03 12:52:50 +01007030 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7031 void *p_vrfy;
7032
Hanno Becker68636192019-02-05 14:36:34 +00007033 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7034 return( 0 );
7035
Hanno Becker8927c832019-04-03 12:52:50 +01007036 if( ssl->f_vrfy != NULL )
7037 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007038 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007039 f_vrfy = ssl->f_vrfy;
7040 p_vrfy = ssl->p_vrfy;
7041 }
7042 else
7043 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007044 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007045 f_vrfy = ssl->conf->f_vrfy;
7046 p_vrfy = ssl->conf->p_vrfy;
7047 }
7048
Hanno Becker68636192019-02-05 14:36:34 +00007049 /*
7050 * Main check: verify certificate
7051 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007052#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7053 if( ssl->conf->f_ca_cb != NULL )
7054 {
7055 ((void) rs_ctx);
7056 have_ca_chain = 1;
7057
7058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03007059 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007060 chain,
7061 ssl->conf->f_ca_cb,
7062 ssl->conf->p_ca_cb,
7063 ssl->conf->cert_profile,
7064 ssl->hostname,
7065 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007066 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007067 }
7068 else
7069#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7070 {
7071 mbedtls_x509_crt *ca_chain;
7072 mbedtls_x509_crl *ca_crl;
7073
7074#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7075 if( ssl->handshake->sni_ca_chain != NULL )
7076 {
7077 ca_chain = ssl->handshake->sni_ca_chain;
7078 ca_crl = ssl->handshake->sni_ca_crl;
7079 }
7080 else
7081#endif
7082 {
7083 ca_chain = ssl->conf->ca_chain;
7084 ca_crl = ssl->conf->ca_crl;
7085 }
7086
7087 if( ca_chain != NULL )
7088 have_ca_chain = 1;
7089
7090 ret = mbedtls_x509_crt_verify_restartable(
7091 chain,
7092 ca_chain, ca_crl,
7093 ssl->conf->cert_profile,
7094 ssl->hostname,
7095 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007096 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007097 }
Hanno Becker68636192019-02-05 14:36:34 +00007098
7099 if( ret != 0 )
7100 {
7101 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7102 }
7103
7104#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7105 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7106 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7107#endif
7108
7109 /*
7110 * Secondary checks: always done, but change 'ret' only if it was 0
7111 */
7112
7113#if defined(MBEDTLS_ECP_C)
7114 {
7115 const mbedtls_pk_context *pk = &chain->pk;
7116
7117 /* If certificate uses an EC key, make sure the curve is OK */
7118 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7119 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7120 {
7121 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7122
7123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7124 if( ret == 0 )
7125 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7126 }
7127 }
7128#endif /* MBEDTLS_ECP_C */
7129
7130 if( mbedtls_ssl_check_cert_usage( chain,
7131 ciphersuite_info,
7132 ! ssl->conf->endpoint,
7133 &ssl->session_negotiate->verify_result ) != 0 )
7134 {
7135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7136 if( ret == 0 )
7137 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7138 }
7139
7140 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7141 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7142 * with details encoded in the verification flags. All other kinds
7143 * of error codes, including those from the user provided f_vrfy
7144 * functions, are treated as fatal and lead to a failure of
7145 * ssl_parse_certificate even if verification was optional. */
7146 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7147 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7148 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7149 {
7150 ret = 0;
7151 }
7152
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007153 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007154 {
7155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7156 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7157 }
7158
7159 if( ret != 0 )
7160 {
7161 uint8_t alert;
7162
7163 /* The certificate may have been rejected for several reasons.
7164 Pick one and send the corresponding alert. Which alert to send
7165 may be a subject of debate in some cases. */
7166 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7167 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7168 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7169 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7170 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7171 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7172 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7173 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7174 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7175 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7176 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7177 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7178 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7179 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7180 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7181 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7182 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7183 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7184 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7185 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7186 else
7187 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7188 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7189 alert );
7190 }
7191
7192#if defined(MBEDTLS_DEBUG_C)
7193 if( ssl->session_negotiate->verify_result != 0 )
7194 {
7195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7196 ssl->session_negotiate->verify_result ) );
7197 }
7198 else
7199 {
7200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7201 }
7202#endif /* MBEDTLS_DEBUG_C */
7203
7204 return( ret );
7205}
7206
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007207#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7208static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7209 unsigned char *start, size_t len )
7210{
7211 int ret;
7212 /* Remember digest of the peer's end-CRT. */
7213 ssl->session_negotiate->peer_cert_digest =
7214 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7215 if( ssl->session_negotiate->peer_cert_digest == NULL )
7216 {
7217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7218 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7219 mbedtls_ssl_send_alert_message( ssl,
7220 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7221 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7222
7223 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7224 }
7225
7226 ret = mbedtls_md( mbedtls_md_info_from_type(
7227 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7228 start, len,
7229 ssl->session_negotiate->peer_cert_digest );
7230
7231 ssl->session_negotiate->peer_cert_digest_type =
7232 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7233 ssl->session_negotiate->peer_cert_digest_len =
7234 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7235
7236 return( ret );
7237}
7238
7239static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7240 unsigned char *start, size_t len )
7241{
7242 unsigned char *end = start + len;
7243 int ret;
7244
7245 /* Make a copy of the peer's raw public key. */
7246 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7247 ret = mbedtls_pk_parse_subpubkey( &start, end,
7248 &ssl->handshake->peer_pubkey );
7249 if( ret != 0 )
7250 {
7251 /* We should have parsed the public key before. */
7252 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7253 }
7254
7255 return( 0 );
7256}
7257#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7258
Hanno Becker68636192019-02-05 14:36:34 +00007259int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7260{
7261 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007262 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007263#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7264 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7265 ? ssl->handshake->sni_authmode
7266 : ssl->conf->authmode;
7267#else
7268 const int authmode = ssl->conf->authmode;
7269#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007270 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007271 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007272
7273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7274
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007275 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7276 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007277 {
7278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007279 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007280 }
7281
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007282#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7283 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007284 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007285 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007286 chain = ssl->handshake->ecrs_peer_cert;
7287 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007288 goto crt_verify;
7289 }
7290#endif
7291
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007292 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007293 {
7294 /* mbedtls_ssl_read_record may have sent an alert already. We
7295 let it decide whether to alert. */
7296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007297 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007298 }
7299
Hanno Becker4a55f632019-02-05 12:49:06 +00007300#if defined(MBEDTLS_SSL_SRV_C)
7301 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7302 {
7303 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007304
7305 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007306 ret = 0;
7307 else
7308 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007309
Hanno Becker6bdfab22019-02-05 13:11:17 +00007310 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007311 }
7312#endif /* MBEDTLS_SSL_SRV_C */
7313
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007314 /* Clear existing peer CRT structure in case we tried to
7315 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007316 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007317
7318 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7319 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007320 {
7321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7322 sizeof( mbedtls_x509_crt ) ) );
7323 mbedtls_ssl_send_alert_message( ssl,
7324 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7325 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007326
Hanno Becker3dad3112019-02-05 17:19:52 +00007327 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7328 goto exit;
7329 }
7330 mbedtls_x509_crt_init( chain );
7331
7332 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007333 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007334 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007335
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007336#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7337 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007338 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007339
7340crt_verify:
7341 if( ssl->handshake->ecrs_enabled)
7342 rs_ctx = &ssl->handshake->ecrs_ctx;
7343#endif
7344
Hanno Becker68636192019-02-05 14:36:34 +00007345 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007346 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007347 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007348 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007349
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007350#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007351 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007352 unsigned char *crt_start, *pk_start;
7353 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007354
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007355 /* We parse the CRT chain without copying, so
7356 * these pointers point into the input buffer,
7357 * and are hence still valid after freeing the
7358 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007359
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007360 crt_start = chain->raw.p;
7361 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007362
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007363 pk_start = chain->pk_raw.p;
7364 pk_len = chain->pk_raw.len;
7365
7366 /* Free the CRT structures before computing
7367 * digest and copying the peer's public key. */
7368 mbedtls_x509_crt_free( chain );
7369 mbedtls_free( chain );
7370 chain = NULL;
7371
7372 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007373 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007374 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007375
7376 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7377 if( ret != 0 )
7378 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007379 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007380#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7381 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007382 ssl->session_negotiate->peer_cert = chain;
7383 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007384#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007387
Hanno Becker6bdfab22019-02-05 13:11:17 +00007388exit:
7389
Hanno Becker3dad3112019-02-05 17:19:52 +00007390 if( ret == 0 )
7391 ssl->state++;
7392
7393#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7394 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7395 {
7396 ssl->handshake->ecrs_peer_cert = chain;
7397 chain = NULL;
7398 }
7399#endif
7400
7401 if( chain != NULL )
7402 {
7403 mbedtls_x509_crt_free( chain );
7404 mbedtls_free( chain );
7405 }
7406
Paul Bakker5121ce52009-01-03 21:22:43 +00007407 return( ret );
7408}
Hanno Becker21489932019-02-05 13:20:55 +00007409#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007412{
7413 int ret;
7414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007418 ssl->out_msglen = 1;
7419 ssl->out_msg[0] = 1;
7420
Paul Bakker5121ce52009-01-03 21:22:43 +00007421 ssl->state++;
7422
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007423 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007424 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007426 return( ret );
7427 }
7428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007430
7431 return( 0 );
7432}
7433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007435{
7436 int ret;
7437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007439
Hanno Becker327c93b2018-08-15 13:56:18 +01007440 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007443 return( ret );
7444 }
7445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007449 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7450 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007452 }
7453
Hanno Beckere678eaa2018-08-21 14:57:46 +01007454 /* CCS records are only accepted if they have length 1 and content '1',
7455 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007456
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007457 /*
7458 * Switch to our negotiated transform and session parameters for inbound
7459 * data.
7460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007462 ssl->transform_in = ssl->transform_negotiate;
7463 ssl->session_in = ssl->session_negotiate;
7464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007469 ssl_dtls_replay_reset( ssl );
7470#endif
7471
7472 /* Increment epoch */
7473 if( ++ssl->in_epoch == 0 )
7474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007476 /* This is highly unlikely to happen for legitimate reasons, so
7477 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007479 }
7480 }
7481 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007483 memset( ssl->in_ctr, 0, 8 );
7484
Hanno Becker79594fd2019-05-08 09:38:41 +01007485 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7488 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007493 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7494 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007496 }
7497 }
7498#endif
7499
Paul Bakker5121ce52009-01-03 21:22:43 +00007500 ssl->state++;
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007503
7504 return( 0 );
7505}
7506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7508 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007509{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007510 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7513 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7514 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007515 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007516 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7519#if defined(MBEDTLS_SHA512_C)
7520 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007521 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7522 else
7523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#if defined(MBEDTLS_SHA256_C)
7525 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007526 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007527 else
7528#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007532 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007533 }
Paul Bakker380da532012-04-18 16:10:25 +00007534}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7539 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007540 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7541 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007542#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7544#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007545#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007546 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007547 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7548#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007549 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007550#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007551#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007553#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007554 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007555 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007556#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007557 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007558#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007559#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007561}
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007564 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7567 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007568 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7569 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7572#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007573#if defined(MBEDTLS_USE_PSA_CRYPTO)
7574 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7575#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007576 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007577#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007580#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007581 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007582#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007583 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007584#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007587}
7588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7590 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7591static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007592 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007593{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007594 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7595 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007596}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007597#endif
Paul Bakker380da532012-04-18 16:10:25 +00007598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7600#if defined(MBEDTLS_SHA256_C)
7601static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007602 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007603{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007604#if defined(MBEDTLS_USE_PSA_CRYPTO)
7605 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7606#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007607 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007608#endif
Paul Bakker380da532012-04-18 16:10:25 +00007609}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007610#endif
Paul Bakker380da532012-04-18 16:10:25 +00007611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612#if defined(MBEDTLS_SHA512_C)
7613static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007614 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007615{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007616#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007617 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007618#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007619 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007620#endif
Paul Bakker380da532012-04-18 16:10:25 +00007621}
Paul Bakker769075d2012-11-24 11:26:46 +01007622#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007626static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007628{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007629 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007630 mbedtls_md5_context md5;
7631 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007632
Paul Bakker5121ce52009-01-03 21:22:43 +00007633 unsigned char padbuf[48];
7634 unsigned char md5sum[16];
7635 unsigned char sha1sum[20];
7636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007638 if( !session )
7639 session = ssl->session;
7640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007642
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007643 mbedtls_md5_init( &md5 );
7644 mbedtls_sha1_init( &sha1 );
7645
7646 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7647 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007648
7649 /*
7650 * SSLv3:
7651 * hash =
7652 * MD5( master + pad2 +
7653 * MD5( handshake + sender + master + pad1 ) )
7654 * + SHA1( master + pad2 +
7655 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007656 */
7657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007659 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7660 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007661#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007664 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7665 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007666#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007669 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007670
Paul Bakker1ef83d62012-04-11 12:09:53 +00007671 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007672
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007673 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7674 mbedtls_md5_update_ret( &md5, session->master, 48 );
7675 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7676 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007677
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007678 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7679 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7680 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7681 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007682
Paul Bakker1ef83d62012-04-11 12:09:53 +00007683 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007684
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007685 mbedtls_md5_starts_ret( &md5 );
7686 mbedtls_md5_update_ret( &md5, session->master, 48 );
7687 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7688 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7689 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007690
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007691 mbedtls_sha1_starts_ret( &sha1 );
7692 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7693 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7694 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7695 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007698
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007699 mbedtls_md5_free( &md5 );
7700 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007701
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007702 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7703 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7704 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007711static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007713{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007714 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007715 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007716 mbedtls_md5_context md5;
7717 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007718 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007721 if( !session )
7722 session = ssl->session;
7723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007725
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007726 mbedtls_md5_init( &md5 );
7727 mbedtls_sha1_init( &sha1 );
7728
7729 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7730 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007731
Paul Bakker1ef83d62012-04-11 12:09:53 +00007732 /*
7733 * TLSv1:
7734 * hash = PRF( master, finished_label,
7735 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7736 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007739 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7740 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007741#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007744 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7745 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007746#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007749 ? "client finished"
7750 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007751
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007752 mbedtls_md5_finish_ret( &md5, padbuf );
7753 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007754
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007755 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007756 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007759
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007760 mbedtls_md5_free( &md5 );
7761 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007762
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007763 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007766}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7770#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007771static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007773{
7774 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007775 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007776 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007777#if defined(MBEDTLS_USE_PSA_CRYPTO)
7778 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007779 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007780 psa_status_t status;
7781#else
7782 mbedtls_sha256_context sha256;
7783#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007786 if( !session )
7787 session = ssl->session;
7788
Andrzej Kurekeb342242019-01-29 09:14:33 -05007789 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7790 ? "client finished"
7791 : "server finished";
7792
7793#if defined(MBEDTLS_USE_PSA_CRYPTO)
7794 sha256_psa = psa_hash_operation_init();
7795
7796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7797
7798 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7799 if( status != PSA_SUCCESS )
7800 {
7801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7802 return;
7803 }
7804
7805 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7806 if( status != PSA_SUCCESS )
7807 {
7808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7809 return;
7810 }
7811 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7812#else
7813
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007814 mbedtls_sha256_init( &sha256 );
7815
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007817
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007818 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007819
7820 /*
7821 * TLSv1.2:
7822 * hash = PRF( master, finished_label,
7823 * Hash( handshake ) )[0.11]
7824 */
7825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826#if !defined(MBEDTLS_SHA256_ALT)
7827 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007828 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007829#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007830
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007831 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007832 mbedtls_sha256_free( &sha256 );
7833#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007834
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007835 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007836 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007839
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007840 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007843}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007847static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007849{
7850 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007851 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007852 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007853#if defined(MBEDTLS_USE_PSA_CRYPTO)
7854 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007855 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007856 psa_status_t status;
7857#else
7858 mbedtls_sha512_context sha512;
7859#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007862 if( !session )
7863 session = ssl->session;
7864
Andrzej Kurekeb342242019-01-29 09:14:33 -05007865 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7866 ? "client finished"
7867 : "server finished";
7868
7869#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007870 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007871
7872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7873
Andrzej Kurek972fba52019-01-30 03:29:12 -05007874 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007875 if( status != PSA_SUCCESS )
7876 {
7877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7878 return;
7879 }
7880
Andrzej Kurek972fba52019-01-30 03:29:12 -05007881 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007882 if( status != PSA_SUCCESS )
7883 {
7884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7885 return;
7886 }
7887 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7888#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007889 mbedtls_sha512_init( &sha512 );
7890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007892
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007893 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007894
7895 /*
7896 * TLSv1.2:
7897 * hash = PRF( master, finished_label,
7898 * Hash( handshake ) )[0.11]
7899 */
7900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007902 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7903 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007904#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007905
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007906 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007907 mbedtls_sha512_free( &sha512 );
7908#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007909
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007910 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007911 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007914
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007915 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919#endif /* MBEDTLS_SHA512_C */
7920#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007923{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007925
7926 /*
7927 * Free our handshake params
7928 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007929 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007931 ssl->handshake = NULL;
7932
7933 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007934 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007935 */
7936 if( ssl->transform )
7937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 mbedtls_ssl_transform_free( ssl->transform );
7939 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007940 }
7941 ssl->transform = ssl->transform_negotiate;
7942 ssl->transform_negotiate = NULL;
7943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007945}
7946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007948{
7949 int resume = ssl->handshake->resume;
7950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953#if defined(MBEDTLS_SSL_RENEGOTIATION)
7954 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007957 ssl->renego_records_seen = 0;
7958 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007959#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007960
7961 /*
7962 * Free the previous session and switch in the current one
7963 */
Paul Bakker0a597072012-09-25 21:55:46 +00007964 if( ssl->session )
7965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007967 /* RFC 7366 3.1: keep the EtM state */
7968 ssl->session_negotiate->encrypt_then_mac =
7969 ssl->session->encrypt_then_mac;
7970#endif
7971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 mbedtls_ssl_session_free( ssl->session );
7973 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007974 }
7975 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007976 ssl->session_negotiate = NULL;
7977
Paul Bakker0a597072012-09-25 21:55:46 +00007978 /*
7979 * Add cache entry
7980 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007981 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007982 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007983 resume == 0 )
7984 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007985 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007987 }
Paul Bakker0a597072012-09-25 21:55:46 +00007988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007990 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007991 ssl->handshake->flight != NULL )
7992 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007993 /* Cancel handshake timer */
7994 ssl_set_timer( ssl, 0 );
7995
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007996 /* Keep last flight around in case we need to resend it:
7997 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007999 }
8000 else
8001#endif
8002 ssl_handshake_wrapup_free_hs_transform( ssl );
8003
Paul Bakker48916f92012-09-16 19:57:18 +00008004 ssl->state++;
8005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008007}
8008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008010{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008011 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008014
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008015 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008016
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008017 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008018
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008019 /*
8020 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8021 * may define some other value. Currently (early 2016), no defined
8022 * ciphersuite does this (and this is unlikely to change as activity has
8023 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008028 ssl->verify_data_len = hash_len;
8029 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008030#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008031
Paul Bakker5121ce52009-01-03 21:22:43 +00008032 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8034 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008035
8036 /*
8037 * In case of session resuming, invert the client and server
8038 * ChangeCipherSpec messages order.
8039 */
Paul Bakker0a597072012-09-25 21:55:46 +00008040 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008043 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008047 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008050 }
8051 else
8052 ssl->state++;
8053
Paul Bakker48916f92012-09-16 19:57:18 +00008054 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008055 * Switch to our negotiated transform and session parameters for outbound
8056 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008062 {
8063 unsigned char i;
8064
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008065 /* Remember current epoch settings for resending */
8066 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008067 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008068
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008069 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008070 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008071
8072 /* Increment epoch */
8073 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008074 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008075 break;
8076
8077 /* The loop goes to its end iff the counter is wrapping */
8078 if( i == 0 )
8079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8081 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008082 }
8083 }
8084 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008086 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008087
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008088 ssl->transform_out = ssl->transform_negotiate;
8089 ssl->session_out = ssl->session_negotiate;
8090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8092 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8097 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008098 }
8099 }
8100#endif
8101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008103 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008105#endif
8106
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008107 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008108 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008110 return( ret );
8111 }
8112
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008113#if defined(MBEDTLS_SSL_PROTO_DTLS)
8114 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8115 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8116 {
8117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8118 return( ret );
8119 }
8120#endif
8121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008123
8124 return( 0 );
8125}
8126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008128#define SSL_MAX_HASH_LEN 36
8129#else
8130#define SSL_MAX_HASH_LEN 12
8131#endif
8132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008134{
Paul Bakker23986e52011-04-24 08:57:21 +00008135 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008136 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008137 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008140
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008141 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008142
Hanno Becker327c93b2018-08-15 13:56:18 +01008143 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008146 return( ret );
8147 }
8148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008152 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8153 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008155 }
8156
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008157 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#if defined(MBEDTLS_SSL_PROTO_SSL3)
8159 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008160 hash_len = 36;
8161 else
8162#endif
8163 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8166 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008169 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8170 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008172 }
8173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008175 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008178 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8179 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008181 }
8182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008184 ssl->verify_data_len = hash_len;
8185 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008186#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008187
Paul Bakker0a597072012-09-25 21:55:46 +00008188 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008191 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008195 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008197#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008198 }
8199 else
8200 ssl->state++;
8201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008202#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008203 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008205#endif
8206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008208
8209 return( 0 );
8210}
8211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8217 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8218 mbedtls_md5_init( &handshake->fin_md5 );
8219 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008220 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8221 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8224#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008225#if defined(MBEDTLS_USE_PSA_CRYPTO)
8226 handshake->fin_sha256_psa = psa_hash_operation_init();
8227 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8228#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008230 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008231#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008232#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008234#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008235 handshake->fin_sha384_psa = psa_hash_operation_init();
8236 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008237#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008239 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008240#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008241#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008243
8244 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008245
8246#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8247 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8248 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8249#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251#if defined(MBEDTLS_DHM_C)
8252 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008253#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254#if defined(MBEDTLS_ECDH_C)
8255 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008256#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008257#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008258 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008259#if defined(MBEDTLS_SSL_CLI_C)
8260 handshake->ecjpake_cache = NULL;
8261 handshake->ecjpake_cache_len = 0;
8262#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008263#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008264
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008265#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008266 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008267#endif
8268
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008269#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8270 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8271#endif
Hanno Becker75173122019-02-06 16:18:31 +00008272
8273#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8274 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8275 mbedtls_pk_init( &handshake->peer_pubkey );
8276#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008277}
8278
Hanno Beckera18d1322018-01-03 14:27:32 +00008279void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008283 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8284 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008285
Hanno Beckerd56ed242018-01-03 15:32:51 +00008286#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 mbedtls_md_init( &transform->md_ctx_enc );
8288 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008289#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008290}
8291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008295}
8296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008298{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008299 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008300 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008302 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008304 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008305 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008306
8307 /*
8308 * Either the pointers are now NULL or cleared properly and can be freed.
8309 * Now allocate missing structures.
8310 */
8311 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008312 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008313 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008314 }
Paul Bakker48916f92012-09-16 19:57:18 +00008315
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008316 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008317 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008318 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008319 }
Paul Bakker48916f92012-09-16 19:57:18 +00008320
Paul Bakker82788fb2014-10-20 13:59:19 +02008321 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008322 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008323 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008324 }
Paul Bakker48916f92012-09-16 19:57:18 +00008325
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008326 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008327 if( ssl->handshake == NULL ||
8328 ssl->transform_negotiate == NULL ||
8329 ssl->session_negotiate == NULL )
8330 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 mbedtls_free( ssl->handshake );
8334 mbedtls_free( ssl->transform_negotiate );
8335 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008336
8337 ssl->handshake = NULL;
8338 ssl->transform_negotiate = NULL;
8339 ssl->session_negotiate = NULL;
8340
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008341 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008342 }
8343
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008344 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008346 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008347 ssl_handshake_params_init( ssl->handshake );
8348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8351 {
8352 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008353
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008354 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8355 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8356 else
8357 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008358
8359 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008360 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008361#endif
8362
Paul Bakker48916f92012-09-16 19:57:18 +00008363 return( 0 );
8364}
8365
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008366#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008367/* Dummy cookie callbacks for defaults */
8368static int ssl_cookie_write_dummy( void *ctx,
8369 unsigned char **p, unsigned char *end,
8370 const unsigned char *cli_id, size_t cli_id_len )
8371{
8372 ((void) ctx);
8373 ((void) p);
8374 ((void) end);
8375 ((void) cli_id);
8376 ((void) cli_id_len);
8377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008379}
8380
8381static int ssl_cookie_check_dummy( void *ctx,
8382 const unsigned char *cookie, size_t cookie_len,
8383 const unsigned char *cli_id, size_t cli_id_len )
8384{
8385 ((void) ctx);
8386 ((void) cookie);
8387 ((void) cookie_len);
8388 ((void) cli_id);
8389 ((void) cli_id_len);
8390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008392}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008393#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008394
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008395/* Once ssl->out_hdr as the address of the beginning of the
8396 * next outgoing record is set, deduce the other pointers.
8397 *
8398 * Note: For TLS, we save the implicit record sequence number
8399 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8400 * and the caller has to make sure there's space for this.
8401 */
8402
8403static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8404 mbedtls_ssl_transform *transform )
8405{
8406#if defined(MBEDTLS_SSL_PROTO_DTLS)
8407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8408 {
8409 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008410#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008411 ssl->out_cid = ssl->out_ctr + 8;
8412 ssl->out_len = ssl->out_cid;
8413 if( transform != NULL )
8414 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008415#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008416 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008417#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008418 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008419 }
8420 else
8421#endif
8422 {
8423 ssl->out_ctr = ssl->out_hdr - 8;
8424 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008425#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008426 ssl->out_cid = ssl->out_len;
8427#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008428 ssl->out_iv = ssl->out_hdr + 5;
8429 }
8430
8431 /* Adjust out_msg to make space for explicit IV, if used. */
8432 if( transform != NULL &&
8433 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8434 {
8435 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8436 }
8437 else
8438 ssl->out_msg = ssl->out_iv;
8439}
8440
8441/* Once ssl->in_hdr as the address of the beginning of the
8442 * next incoming record is set, deduce the other pointers.
8443 *
8444 * Note: For TLS, we save the implicit record sequence number
8445 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8446 * and the caller has to make sure there's space for this.
8447 */
8448
Hanno Becker79594fd2019-05-08 09:38:41 +01008449static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008450{
Hanno Becker79594fd2019-05-08 09:38:41 +01008451 /* This function sets the pointers to match the case
8452 * of unprotected TLS/DTLS records, with both ssl->in_iv
8453 * and ssl->in_msg pointing to the beginning of the record
8454 * content.
8455 *
8456 * When decrypting a protected record, ssl->in_msg
8457 * will be shifted to point to the beginning of the
8458 * record plaintext.
8459 */
8460
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008461#if defined(MBEDTLS_SSL_PROTO_DTLS)
8462 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8463 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008464 /* This sets the header pointers to match records
8465 * without CID. When we receive a record containing
8466 * a CID, the fields are shifted accordingly in
8467 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008468 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008469#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008470 ssl->in_cid = ssl->in_ctr + 8;
8471 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008472#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008473 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008474#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008475 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008476 }
8477 else
8478#endif
8479 {
8480 ssl->in_ctr = ssl->in_hdr - 8;
8481 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008482#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008483 ssl->in_cid = ssl->in_len;
8484#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008485 ssl->in_iv = ssl->in_hdr + 5;
8486 }
8487
Hanno Becker79594fd2019-05-08 09:38:41 +01008488 /* This will be adjusted at record decryption time. */
8489 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008490}
8491
Paul Bakker5121ce52009-01-03 21:22:43 +00008492/*
8493 * Initialize an SSL context
8494 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008495void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8496{
8497 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8498}
8499
8500/*
8501 * Setup an SSL context
8502 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008503
8504static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8505{
8506 /* Set the incoming and outgoing record pointers. */
8507#if defined(MBEDTLS_SSL_PROTO_DTLS)
8508 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8509 {
8510 ssl->out_hdr = ssl->out_buf;
8511 ssl->in_hdr = ssl->in_buf;
8512 }
8513 else
8514#endif /* MBEDTLS_SSL_PROTO_DTLS */
8515 {
8516 ssl->out_hdr = ssl->out_buf + 8;
8517 ssl->in_hdr = ssl->in_buf + 8;
8518 }
8519
8520 /* Derive other internal pointers. */
8521 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008522 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008523}
8524
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008525int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008526 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008527{
Paul Bakker48916f92012-09-16 19:57:18 +00008528 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008529
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008530 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008531
8532 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008533 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008534 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008535
8536 /* Set to NULL in case of an error condition */
8537 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008538
Angus Grattond8213d02016-05-25 20:56:48 +10008539 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8540 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008541 {
Angus Grattond8213d02016-05-25 20:56:48 +10008542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008543 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008544 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008545 }
8546
8547 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8548 if( ssl->out_buf == NULL )
8549 {
8550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008551 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008552 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008553 }
8554
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008555 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008556
Paul Bakker48916f92012-09-16 19:57:18 +00008557 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008558 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008559
8560 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008561
8562error:
8563 mbedtls_free( ssl->in_buf );
8564 mbedtls_free( ssl->out_buf );
8565
8566 ssl->conf = NULL;
8567
8568 ssl->in_buf = NULL;
8569 ssl->out_buf = NULL;
8570
8571 ssl->in_hdr = NULL;
8572 ssl->in_ctr = NULL;
8573 ssl->in_len = NULL;
8574 ssl->in_iv = NULL;
8575 ssl->in_msg = NULL;
8576
8577 ssl->out_hdr = NULL;
8578 ssl->out_ctr = NULL;
8579 ssl->out_len = NULL;
8580 ssl->out_iv = NULL;
8581 ssl->out_msg = NULL;
8582
k-stachowiak9f7798e2018-07-31 16:52:32 +02008583 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008584}
8585
8586/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008587 * Reset an initialized and used SSL context for re-use while retaining
8588 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008589 *
8590 * If partial is non-zero, keep data in the input buffer and client ID.
8591 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008592 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008593static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008594{
Paul Bakker48916f92012-09-16 19:57:18 +00008595 int ret;
8596
Hanno Becker7e772132018-08-10 12:38:21 +01008597#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8598 !defined(MBEDTLS_SSL_SRV_C)
8599 ((void) partial);
8600#endif
8601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008603
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008604 /* Cancel any possibly running timer */
8605 ssl_set_timer( ssl, 0 );
8606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607#if defined(MBEDTLS_SSL_RENEGOTIATION)
8608 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008609 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008610
8611 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8613 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008616
Paul Bakker7eb013f2011-10-06 12:37:39 +00008617 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008618 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008619
8620 ssl->in_msgtype = 0;
8621 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008623 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008624 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008627 ssl_dtls_replay_reset( ssl );
8628#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008629
8630 ssl->in_hslen = 0;
8631 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008632
8633 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008634
8635 ssl->out_msgtype = 0;
8636 ssl->out_msglen = 0;
8637 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8639 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008640 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008641#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008642
Hanno Becker19859472018-08-06 09:40:20 +01008643 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8644
Paul Bakker48916f92012-09-16 19:57:18 +00008645 ssl->transform_in = NULL;
8646 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008647
Hanno Becker78640902018-08-13 16:35:15 +01008648 ssl->session_in = NULL;
8649 ssl->session_out = NULL;
8650
Angus Grattond8213d02016-05-25 20:56:48 +10008651 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008652
8653#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008654 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008655#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8656 {
8657 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008658 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008659 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8662 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8665 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8668 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008669 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008670 }
8671#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008672
Paul Bakker48916f92012-09-16 19:57:18 +00008673 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675 mbedtls_ssl_transform_free( ssl->transform );
8676 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008677 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008678 }
Paul Bakker48916f92012-09-16 19:57:18 +00008679
Paul Bakkerc0463502013-02-14 11:19:38 +01008680 if( ssl->session )
8681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682 mbedtls_ssl_session_free( ssl->session );
8683 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008684 ssl->session = NULL;
8685 }
8686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008688 ssl->alpn_chosen = NULL;
8689#endif
8690
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008691#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008692#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008693 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008694#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008695 {
8696 mbedtls_free( ssl->cli_id );
8697 ssl->cli_id = NULL;
8698 ssl->cli_id_len = 0;
8699 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008700#endif
8701
Paul Bakker48916f92012-09-16 19:57:18 +00008702 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8703 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008704
8705 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008706}
8707
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008708/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008709 * Reset an initialized and used SSL context for re-use while retaining
8710 * all application-set variables, function pointers and data.
8711 */
8712int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8713{
8714 return( ssl_session_reset_int( ssl, 0 ) );
8715}
8716
8717/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008718 * SSL set accessors
8719 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008720void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008721{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008722 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008723}
8724
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008725void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008726{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008727 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008728}
8729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008731void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008732{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008733 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008734}
8735#endif
8736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008738void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008739{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008740 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008741}
8742#endif
8743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008745
Hanno Becker1841b0a2018-08-24 11:13:57 +01008746void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8747 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008748{
8749 ssl->disable_datagram_packing = !allow_packing;
8750}
8751
8752void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8753 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008754{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008755 conf->hs_timeout_min = min;
8756 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008757}
8758#endif
8759
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008760void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008761{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008762 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008763}
8764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008766void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008767 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008768 void *p_vrfy )
8769{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008770 conf->f_vrfy = f_vrfy;
8771 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008774
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008775void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008776 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008777 void *p_rng )
8778{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008779 conf->f_rng = f_rng;
8780 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008781}
8782
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008783void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008784 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008785 void *p_dbg )
8786{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008787 conf->f_dbg = f_dbg;
8788 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008789}
8790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008792 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008793 mbedtls_ssl_send_t *f_send,
8794 mbedtls_ssl_recv_t *f_recv,
8795 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008796{
8797 ssl->p_bio = p_bio;
8798 ssl->f_send = f_send;
8799 ssl->f_recv = f_recv;
8800 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008801}
8802
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008803#if defined(MBEDTLS_SSL_PROTO_DTLS)
8804void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8805{
8806 ssl->mtu = mtu;
8807}
8808#endif
8809
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008810void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008811{
8812 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008813}
8814
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008815void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8816 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008817 mbedtls_ssl_set_timer_t *f_set_timer,
8818 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008819{
8820 ssl->p_timer = p_timer;
8821 ssl->f_set_timer = f_set_timer;
8822 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008823
8824 /* Make sure we start with no timer running */
8825 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008826}
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008829void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008830 void *p_cache,
8831 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8832 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008833{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008834 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008835 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008836 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008840#if defined(MBEDTLS_SSL_CLI_C)
8841int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008842{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008843 int ret;
8844
8845 if( ssl == NULL ||
8846 session == NULL ||
8847 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008848 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008851 }
8852
Hanno Becker52055ae2019-02-06 14:30:46 +00008853 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8854 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008855 return( ret );
8856
Paul Bakker0a597072012-09-25 21:55:46 +00008857 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008858
8859 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008860}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008862
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008863void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008864 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008865{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008866 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8867 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8868 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8869 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008870}
8871
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008872void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008873 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008874 int major, int minor )
8875{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008876 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008877 return;
8878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008880 return;
8881
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008882 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008883}
8884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008886void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008887 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008888{
8889 conf->cert_profile = profile;
8890}
8891
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008892/* Append a new keycert entry to a (possibly empty) list */
8893static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8894 mbedtls_x509_crt *cert,
8895 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008896{
niisato8ee24222018-06-25 19:05:48 +09008897 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008898
niisato8ee24222018-06-25 19:05:48 +09008899 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8900 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008901 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008902
niisato8ee24222018-06-25 19:05:48 +09008903 new_cert->cert = cert;
8904 new_cert->key = key;
8905 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008906
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008907 /* Update head is the list was null, else add to the end */
8908 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008909 {
niisato8ee24222018-06-25 19:05:48 +09008910 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008911 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008912 else
8913 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008914 mbedtls_ssl_key_cert *cur = *head;
8915 while( cur->next != NULL )
8916 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008917 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008918 }
8919
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008920 return( 0 );
8921}
8922
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008923int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008924 mbedtls_x509_crt *own_cert,
8925 mbedtls_pk_context *pk_key )
8926{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008927 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008928}
8929
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008930void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008931 mbedtls_x509_crt *ca_chain,
8932 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008933{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008934 conf->ca_chain = ca_chain;
8935 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008936
8937#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8938 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8939 * cannot be used together. */
8940 conf->f_ca_cb = NULL;
8941 conf->p_ca_cb = NULL;
8942#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008943}
Hanno Becker5adaad92019-03-27 16:54:37 +00008944
8945#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8946void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008947 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008948 void *p_ca_cb )
8949{
8950 conf->f_ca_cb = f_ca_cb;
8951 conf->p_ca_cb = p_ca_cb;
8952
8953 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8954 * cannot be used together. */
8955 conf->ca_chain = NULL;
8956 conf->ca_crl = NULL;
8957}
8958#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008960
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008961#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8962int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8963 mbedtls_x509_crt *own_cert,
8964 mbedtls_pk_context *pk_key )
8965{
8966 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8967 own_cert, pk_key ) );
8968}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008969
8970void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8971 mbedtls_x509_crt *ca_chain,
8972 mbedtls_x509_crl *ca_crl )
8973{
8974 ssl->handshake->sni_ca_chain = ca_chain;
8975 ssl->handshake->sni_ca_crl = ca_crl;
8976}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008977
8978void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8979 int authmode )
8980{
8981 ssl->handshake->sni_authmode = authmode;
8982}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008983#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8984
Hanno Becker8927c832019-04-03 12:52:50 +01008985#if defined(MBEDTLS_X509_CRT_PARSE_C)
8986void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8987 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8988 void *p_vrfy )
8989{
8990 ssl->f_vrfy = f_vrfy;
8991 ssl->p_vrfy = p_vrfy;
8992}
8993#endif
8994
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008995#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008996/*
8997 * Set EC J-PAKE password for current handshake
8998 */
8999int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9000 const unsigned char *pw,
9001 size_t pw_len )
9002{
9003 mbedtls_ecjpake_role role;
9004
Janos Follath8eb64132016-06-03 15:40:57 +01009005 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9007
9008 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
9009 role = MBEDTLS_ECJPAKE_SERVER;
9010 else
9011 role = MBEDTLS_ECJPAKE_CLIENT;
9012
9013 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9014 role,
9015 MBEDTLS_MD_SHA256,
9016 MBEDTLS_ECP_DP_SECP256R1,
9017 pw, pw_len ) );
9018}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009019#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009022
9023static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
9024{
9025 /* Remove reference to existing PSK, if any. */
9026#if defined(MBEDTLS_USE_PSA_CRYPTO)
9027 if( conf->psk_opaque != 0 )
9028 {
9029 /* The maintenance of the PSK key slot is the
9030 * user's responsibility. */
9031 conf->psk_opaque = 0;
9032 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00009033 /* This and the following branch should never
9034 * be taken simultaenously as we maintain the
9035 * invariant that raw and opaque PSKs are never
9036 * configured simultaneously. As a safeguard,
9037 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009038#endif /* MBEDTLS_USE_PSA_CRYPTO */
9039 if( conf->psk != NULL )
9040 {
9041 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
9042
9043 mbedtls_free( conf->psk );
9044 conf->psk = NULL;
9045 conf->psk_len = 0;
9046 }
9047
9048 /* Remove reference to PSK identity, if any. */
9049 if( conf->psk_identity != NULL )
9050 {
9051 mbedtls_free( conf->psk_identity );
9052 conf->psk_identity = NULL;
9053 conf->psk_identity_len = 0;
9054 }
9055}
9056
Hanno Becker7390c712018-11-15 13:33:04 +00009057/* This function assumes that PSK identity in the SSL config is unset.
9058 * It checks that the provided identity is well-formed and attempts
9059 * to make a copy of it in the SSL config.
9060 * On failure, the PSK identity in the config remains unset. */
9061static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
9062 unsigned char const *psk_identity,
9063 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009064{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009065 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00009066 if( psk_identity == NULL ||
9067 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009068 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009069 {
9070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9071 }
9072
Hanno Becker7390c712018-11-15 13:33:04 +00009073 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9074 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009075 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009076
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009077 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009078 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009079
9080 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009081}
9082
Hanno Becker7390c712018-11-15 13:33:04 +00009083int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9084 const unsigned char *psk, size_t psk_len,
9085 const unsigned char *psk_identity, size_t psk_identity_len )
9086{
9087 int ret;
9088 /* Remove opaque/raw PSK + PSK Identity */
9089 ssl_conf_remove_psk( conf );
9090
9091 /* Check and set raw PSK */
9092 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9094 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9095 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9096 conf->psk_len = psk_len;
9097 memcpy( conf->psk, psk, conf->psk_len );
9098
9099 /* Check and set PSK Identity */
9100 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9101 if( ret != 0 )
9102 ssl_conf_remove_psk( conf );
9103
9104 return( ret );
9105}
9106
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009107static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9108{
9109#if defined(MBEDTLS_USE_PSA_CRYPTO)
9110 if( ssl->handshake->psk_opaque != 0 )
9111 {
9112 ssl->handshake->psk_opaque = 0;
9113 }
9114 else
9115#endif /* MBEDTLS_USE_PSA_CRYPTO */
9116 if( ssl->handshake->psk != NULL )
9117 {
9118 mbedtls_platform_zeroize( ssl->handshake->psk,
9119 ssl->handshake->psk_len );
9120 mbedtls_free( ssl->handshake->psk );
9121 ssl->handshake->psk_len = 0;
9122 }
9123}
9124
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009125int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9126 const unsigned char *psk, size_t psk_len )
9127{
9128 if( psk == NULL || ssl->handshake == NULL )
9129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9130
9131 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9133
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009134 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009135
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009136 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009137 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009138
9139 ssl->handshake->psk_len = psk_len;
9140 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9141
9142 return( 0 );
9143}
9144
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009145#if defined(MBEDTLS_USE_PSA_CRYPTO)
9146int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009147 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009148 const unsigned char *psk_identity,
9149 size_t psk_identity_len )
9150{
Hanno Becker7390c712018-11-15 13:33:04 +00009151 int ret;
9152 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009153 ssl_conf_remove_psk( conf );
9154
Hanno Becker7390c712018-11-15 13:33:04 +00009155 /* Check and set opaque PSK */
9156 if( psk_slot == 0 )
9157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009158 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009159
9160 /* Check and set PSK Identity */
9161 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9162 psk_identity_len );
9163 if( ret != 0 )
9164 ssl_conf_remove_psk( conf );
9165
9166 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009167}
9168
9169int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009170 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009171{
9172 if( psk_slot == 0 || ssl->handshake == NULL )
9173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9174
9175 ssl_remove_psk( ssl );
9176 ssl->handshake->psk_opaque = psk_slot;
9177 return( 0 );
9178}
9179#endif /* MBEDTLS_USE_PSA_CRYPTO */
9180
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009181void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009183 size_t),
9184 void *p_psk )
9185{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009186 conf->f_psk = f_psk;
9187 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009190
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009191#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009192
9193#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009194int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009195{
9196 int ret;
9197
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009198 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9199 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9200 {
9201 mbedtls_mpi_free( &conf->dhm_P );
9202 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009203 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009204 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009205
9206 return( 0 );
9207}
Hanno Becker470a8c42017-10-04 15:28:46 +01009208#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009209
Hanno Beckera90658f2017-10-04 15:29:08 +01009210int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9211 const unsigned char *dhm_P, size_t P_len,
9212 const unsigned char *dhm_G, size_t G_len )
9213{
9214 int ret;
9215
9216 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9217 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9218 {
9219 mbedtls_mpi_free( &conf->dhm_P );
9220 mbedtls_mpi_free( &conf->dhm_G );
9221 return( ret );
9222 }
9223
9224 return( 0 );
9225}
Paul Bakker5121ce52009-01-03 21:22:43 +00009226
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009227int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009228{
9229 int ret;
9230
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009231 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9232 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9233 {
9234 mbedtls_mpi_free( &conf->dhm_P );
9235 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009236 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009237 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009238
9239 return( 0 );
9240}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009241#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009242
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009243#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9244/*
9245 * Set the minimum length for Diffie-Hellman parameters
9246 */
9247void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9248 unsigned int bitlen )
9249{
9250 conf->dhm_min_bitlen = bitlen;
9251}
9252#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9253
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009254#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009255/*
9256 * Set allowed/preferred hashes for handshake signatures
9257 */
9258void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9259 const int *hashes )
9260{
9261 conf->sig_hashes = hashes;
9262}
Hanno Becker947194e2017-04-07 13:25:49 +01009263#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009264
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009265#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009266/*
9267 * Set the allowed elliptic curves
9268 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009269void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009270 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009271{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009272 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009273}
Hanno Becker947194e2017-04-07 13:25:49 +01009274#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009275
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009276#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009277int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009278{
Hanno Becker947194e2017-04-07 13:25:49 +01009279 /* Initialize to suppress unnecessary compiler warning */
9280 size_t hostname_len = 0;
9281
9282 /* Check if new hostname is valid before
9283 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009284 if( hostname != NULL )
9285 {
9286 hostname_len = strlen( hostname );
9287
9288 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9290 }
9291
9292 /* Now it's clear that we will overwrite the old hostname,
9293 * so we can free it safely */
9294
9295 if( ssl->hostname != NULL )
9296 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009297 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009298 mbedtls_free( ssl->hostname );
9299 }
9300
9301 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009302
Paul Bakker5121ce52009-01-03 21:22:43 +00009303 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009304 {
9305 ssl->hostname = NULL;
9306 }
9307 else
9308 {
9309 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009310 if( ssl->hostname == NULL )
9311 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009312
Hanno Becker947194e2017-04-07 13:25:49 +01009313 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009314
Hanno Becker947194e2017-04-07 13:25:49 +01009315 ssl->hostname[hostname_len] = '\0';
9316 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009317
9318 return( 0 );
9319}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009320#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009321
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009322#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009323void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009325 const unsigned char *, size_t),
9326 void *p_sni )
9327{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009328 conf->f_sni = f_sni;
9329 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009330}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009334int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009335{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009336 size_t cur_len, tot_len;
9337 const char **p;
9338
9339 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009340 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9341 * MUST NOT be truncated."
9342 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009343 */
9344 tot_len = 0;
9345 for( p = protos; *p != NULL; p++ )
9346 {
9347 cur_len = strlen( *p );
9348 tot_len += cur_len;
9349
9350 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009352 }
9353
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009354 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009355
9356 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009357}
9358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009360{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009361 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009364
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009365void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009366{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009367 conf->max_major_ver = major;
9368 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009369}
9370
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009371void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009373 conf->min_major_ver = major;
9374 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009375}
9376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009378void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009379{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009380 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009381}
9382#endif
9383
Janos Follath088ce432017-04-10 12:42:31 +01009384#if defined(MBEDTLS_SSL_SRV_C)
9385void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9386 char cert_req_ca_list )
9387{
9388 conf->cert_req_ca_list = cert_req_ca_list;
9389}
9390#endif
9391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009393void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009395 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009396}
9397#endif
9398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009400void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009401{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009402 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009403}
9404#endif
9405
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009406#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009407void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009408{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009409 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009410}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009411#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009414int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009417 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009420 }
9421
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009422 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009423
9424 return( 0 );
9425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009429void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009430{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009431 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009432}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009436void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009437{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009438 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009439}
9440#endif
9441
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009442void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009443{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009444 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009445}
9446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009448void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009449{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009450 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009451}
9452
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009453void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009454{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009455 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009456}
9457
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009458void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009459 const unsigned char period[8] )
9460{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009461 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009466#if defined(MBEDTLS_SSL_CLI_C)
9467void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009468{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009469 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009470}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009471#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009472
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009473#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009474void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9475 mbedtls_ssl_ticket_write_t *f_ticket_write,
9476 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9477 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009478{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009479 conf->f_ticket_write = f_ticket_write;
9480 conf->f_ticket_parse = f_ticket_parse;
9481 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009482}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009485
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009486#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9487void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9488 mbedtls_ssl_export_keys_t *f_export_keys,
9489 void *p_export_keys )
9490{
9491 conf->f_export_keys = f_export_keys;
9492 conf->p_export_keys = p_export_keys;
9493}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009494
9495void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9496 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9497 void *p_export_keys )
9498{
9499 conf->f_export_keys_ext = f_export_keys_ext;
9500 conf->p_export_keys = p_export_keys;
9501}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009502#endif
9503
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009504#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009505void mbedtls_ssl_conf_async_private_cb(
9506 mbedtls_ssl_config *conf,
9507 mbedtls_ssl_async_sign_t *f_async_sign,
9508 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9509 mbedtls_ssl_async_resume_t *f_async_resume,
9510 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009511 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009512{
9513 conf->f_async_sign_start = f_async_sign;
9514 conf->f_async_decrypt_start = f_async_decrypt;
9515 conf->f_async_resume = f_async_resume;
9516 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009517 conf->p_async_config_data = async_config_data;
9518}
9519
Gilles Peskine8f97af72018-04-26 11:46:10 +02009520void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9521{
9522 return( conf->p_async_config_data );
9523}
9524
Gilles Peskine1febfef2018-04-30 11:54:39 +02009525void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009526{
9527 if( ssl->handshake == NULL )
9528 return( NULL );
9529 else
9530 return( ssl->handshake->user_async_ctx );
9531}
9532
Gilles Peskine1febfef2018-04-30 11:54:39 +02009533void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009534 void *ctx )
9535{
9536 if( ssl->handshake != NULL )
9537 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009538}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009539#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009540
Paul Bakker5121ce52009-01-03 21:22:43 +00009541/*
9542 * SSL get accessors
9543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009545{
9546 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9547}
9548
Hanno Becker8b170a02017-10-10 11:51:19 +01009549int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9550{
9551 /*
9552 * Case A: We're currently holding back
9553 * a message for further processing.
9554 */
9555
9556 if( ssl->keep_current_message == 1 )
9557 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009559 return( 1 );
9560 }
9561
9562 /*
9563 * Case B: Further records are pending in the current datagram.
9564 */
9565
9566#if defined(MBEDTLS_SSL_PROTO_DTLS)
9567 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9568 ssl->in_left > ssl->next_record_offset )
9569 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009571 return( 1 );
9572 }
9573#endif /* MBEDTLS_SSL_PROTO_DTLS */
9574
9575 /*
9576 * Case C: A handshake message is being processed.
9577 */
9578
Hanno Becker8b170a02017-10-10 11:51:19 +01009579 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9580 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009581 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009582 return( 1 );
9583 }
9584
9585 /*
9586 * Case D: An application data message is being processed
9587 */
9588 if( ssl->in_offt != NULL )
9589 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009591 return( 1 );
9592 }
9593
9594 /*
9595 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009596 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009597 * we implement support for multiple alerts in single records.
9598 */
9599
9600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9601 return( 0 );
9602}
9603
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009604uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009605{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009606 if( ssl->session != NULL )
9607 return( ssl->session->verify_result );
9608
9609 if( ssl->session_negotiate != NULL )
9610 return( ssl->session_negotiate->verify_result );
9611
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009612 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009613}
9614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009616{
Paul Bakker926c8e42013-03-06 10:23:34 +01009617 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009618 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009621}
9622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009624{
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 )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009627 {
9628 switch( ssl->minor_ver )
9629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009631 return( "DTLSv1.0" );
9632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009634 return( "DTLSv1.2" );
9635
9636 default:
9637 return( "unknown (DTLS)" );
9638 }
9639 }
9640#endif
9641
Paul Bakker43ca69c2011-01-15 17:35:19 +00009642 switch( ssl->minor_ver )
9643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009645 return( "SSLv3.0" );
9646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009648 return( "TLSv1.0" );
9649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009651 return( "TLSv1.1" );
9652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009654 return( "TLSv1.2" );
9655
Paul Bakker43ca69c2011-01-15 17:35:19 +00009656 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009657 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009658 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009659}
9660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009662{
Hanno Becker3136ede2018-08-17 15:28:19 +01009663 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009665 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009666
Hanno Becker5903de42019-05-03 14:46:38 +01009667 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9668
Hanno Becker78640902018-08-13 16:35:15 +01009669 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009670 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672#if defined(MBEDTLS_ZLIB_SUPPORT)
9673 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9674 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009675#endif
9676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 case MBEDTLS_MODE_GCM:
9680 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009681 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009683 transform_expansion = transform->minlen;
9684 break;
9685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009687
9688 block_size = mbedtls_cipher_get_block_size(
9689 &transform->cipher_ctx_enc );
9690
Hanno Becker3136ede2018-08-17 15:28:19 +01009691 /* Expansion due to the addition of the MAC. */
9692 transform_expansion += transform->maclen;
9693
9694 /* Expansion due to the addition of CBC padding;
9695 * Theoretically up to 256 bytes, but we never use
9696 * more than the block size of the underlying cipher. */
9697 transform_expansion += block_size;
9698
9699 /* For TLS 1.1 or higher, an explicit IV is added
9700 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009701#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9702 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009703 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009704#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009705
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009706 break;
9707
9708 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009711 }
9712
Hanno Beckera0e20d02019-05-15 14:03:01 +01009713#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009714 if( transform->out_cid_len != 0 )
9715 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009716#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009717
Hanno Becker5903de42019-05-03 14:46:38 +01009718 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009719}
9720
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009721#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9722size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9723{
9724 size_t max_len;
9725
9726 /*
9727 * Assume mfl_code is correct since it was checked when set
9728 */
Angus Grattond8213d02016-05-25 20:56:48 +10009729 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009730
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009731 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009732 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009733 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009734 {
Angus Grattond8213d02016-05-25 20:56:48 +10009735 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009736 }
9737
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009738 /* During a handshake, use the value being negotiated */
9739 if( ssl->session_negotiate != NULL &&
9740 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9741 {
9742 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9743 }
9744
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009745 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009746}
9747#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9748
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009749#if defined(MBEDTLS_SSL_PROTO_DTLS)
9750static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9751{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009752 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9753 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9754 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9755 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9756 return ( 0 );
9757
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009758 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9759 return( ssl->mtu );
9760
9761 if( ssl->mtu == 0 )
9762 return( ssl->handshake->mtu );
9763
9764 return( ssl->mtu < ssl->handshake->mtu ?
9765 ssl->mtu : ssl->handshake->mtu );
9766}
9767#endif /* MBEDTLS_SSL_PROTO_DTLS */
9768
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009769int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9770{
9771 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9772
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009773#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9774 !defined(MBEDTLS_SSL_PROTO_DTLS)
9775 (void) ssl;
9776#endif
9777
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009778#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9779 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9780
9781 if( max_len > mfl )
9782 max_len = mfl;
9783#endif
9784
9785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009786 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009787 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009788 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009789 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9790 const size_t overhead = (size_t) ret;
9791
9792 if( ret < 0 )
9793 return( ret );
9794
9795 if( mtu <= overhead )
9796 {
9797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9798 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9799 }
9800
9801 if( max_len > mtu - overhead )
9802 max_len = mtu - overhead;
9803 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009804#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009805
Hanno Becker0defedb2018-08-10 12:35:02 +01009806#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9807 !defined(MBEDTLS_SSL_PROTO_DTLS)
9808 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009809#endif
9810
9811 return( (int) max_len );
9812}
9813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814#if defined(MBEDTLS_X509_CRT_PARSE_C)
9815const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009816{
9817 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009818 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009819
Hanno Beckere6824572019-02-07 13:18:46 +00009820#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009821 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009822#else
9823 return( NULL );
9824#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009825}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009829int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9830 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009831{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009832 if( ssl == NULL ||
9833 dst == NULL ||
9834 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009835 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009838 }
9839
Hanno Becker52055ae2019-02-06 14:30:46 +00009840 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009841}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009843
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02009844const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9845{
9846 if( ssl == NULL )
9847 return( NULL );
9848
9849 return( ssl->session );
9850}
9851
Paul Bakker5121ce52009-01-03 21:22:43 +00009852/*
Hanno Beckera835da52019-05-16 12:39:07 +01009853 * Define ticket header determining Mbed TLS version
9854 * and structure of the ticket.
9855 */
9856
Hanno Becker94ef3b32019-05-16 12:50:45 +01009857/*
Hanno Becker50b59662019-05-28 14:30:45 +01009858 * Define bitflag determining compile-time settings influencing
9859 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01009860 */
9861
Hanno Becker50b59662019-05-28 14:30:45 +01009862#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01009863#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01009864#else
Hanno Becker3e088662019-05-29 11:10:18 +01009865#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01009866#endif /* MBEDTLS_HAVE_TIME */
9867
9868#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01009869#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01009870#else
Hanno Becker3e088662019-05-29 11:10:18 +01009871#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01009872#endif /* MBEDTLS_X509_CRT_PARSE_C */
9873
9874#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01009875#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01009876#else
Hanno Becker3e088662019-05-29 11:10:18 +01009877#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01009878#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9879
9880#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01009881#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01009882#else
Hanno Becker3e088662019-05-29 11:10:18 +01009883#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01009884#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9885
9886#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01009887#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01009888#else
Hanno Becker3e088662019-05-29 11:10:18 +01009889#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01009890#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9891
9892#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01009893#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01009894#else
Hanno Becker3e088662019-05-29 11:10:18 +01009895#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01009896#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9897
Hanno Becker94ef3b32019-05-16 12:50:45 +01009898#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9899#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9900#else
9901#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9902#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9903
Hanno Becker3e088662019-05-29 11:10:18 +01009904#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9905#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9906#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9907#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9908#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9909#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9910#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker3e088662019-05-29 11:10:18 +01009911
Hanno Becker50b59662019-05-28 14:30:45 +01009912#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01009913 ( (uint16_t) ( \
9914 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9915 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9916 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9917 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9918 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9919 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01009920 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01009921
Hanno Beckerf8787072019-05-16 12:41:07 +01009922static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01009923 MBEDTLS_VERSION_MAJOR,
9924 MBEDTLS_VERSION_MINOR,
9925 MBEDTLS_VERSION_PATCH,
Hanno Becker50b59662019-05-28 14:30:45 +01009926 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9927 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Beckerf8787072019-05-16 12:41:07 +01009928};
Hanno Beckera835da52019-05-16 12:39:07 +01009929
9930/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009931 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009932 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009933 *
Hanno Becker50b59662019-05-28 14:30:45 +01009934 * opaque mbedtls_version[3]; // major, minor, patch
9935 * opaque session_format[2]; // version-specific 16-bit field determining
9936 * // the format of the remaining
9937 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01009938 *
9939 * Note: When updating the format, remember to keep
9940 * these version+format bytes.
9941 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01009942 * // In this version, `session_format` determines
9943 * // the setting of those compile-time
9944 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01009945 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009946 * uint64 start_time;
Hanno Becker50b59662019-05-28 14:30:45 +01009947 * uint8 ciphersuite[2]; // defined by the standard
9948 * uint8 compression; // 0 or 1
9949 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009950 * opaque session_id[32];
Hanno Becker50b59662019-05-28 14:30:45 +01009951 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009952 * uint32 verify_result;
Hanno Becker50b59662019-05-28 14:30:45 +01009953 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
9954 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009955 * uint32 ticket_lifetime;
Hanno Becker50b59662019-05-28 14:30:45 +01009956 * uint8 mfl_code; // up to 255 according to standard
9957 * uint8 trunc_hmac; // 0 or 1
9958 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009959 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009960 * The order is the same as in the definition of the structure, except
9961 * verify_result is put before peer_cert so that all mandatory fields come
9962 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009963 */
9964int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9965 unsigned char *buf,
9966 size_t buf_len,
9967 size_t *olen )
9968{
9969 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009970 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009971#if defined(MBEDTLS_HAVE_TIME)
9972 uint64_t start;
9973#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009974#if defined(MBEDTLS_X509_CRT_PARSE_C)
9975#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9976 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009977#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9978#endif /* MBEDTLS_X509_CRT_PARSE_C */
9979
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009980
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009981 /*
Hanno Beckera835da52019-05-16 12:39:07 +01009982 * Add version identifier
9983 */
9984
9985 used += sizeof( ssl_serialized_session_header );
9986
9987 if( used <= buf_len )
9988 {
9989 memcpy( p, ssl_serialized_session_header,
9990 sizeof( ssl_serialized_session_header ) );
9991 p += sizeof( ssl_serialized_session_header );
9992 }
9993
9994 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009995 * Time
9996 */
9997#if defined(MBEDTLS_HAVE_TIME)
9998 used += 8;
9999
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010000 if( used <= buf_len )
10001 {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010002 start = (uint64_t) session->start;
10003
10004 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10005 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10006 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10007 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10008 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10009 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10010 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10011 *p++ = (unsigned char)( ( start ) & 0xFF );
10012 }
10013#endif /* MBEDTLS_HAVE_TIME */
10014
10015 /*
10016 * Basic mandatory fields
10017 */
10018 used += 2 /* ciphersuite */
10019 + 1 /* compression */
10020 + 1 /* id_len */
10021 + sizeof( session->id )
10022 + sizeof( session->master )
10023 + 4; /* verify_result */
10024
10025 if( used <= buf_len )
10026 {
10027 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
10028 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
10029
10030 *p++ = (unsigned char)( session->compression & 0xFF );
10031
10032 *p++ = (unsigned char)( session->id_len & 0xFF );
10033 memcpy( p, session->id, 32 );
10034 p += 32;
10035
10036 memcpy( p, session->master, 48 );
10037 p += 48;
10038
10039 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
10040 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
10041 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
10042 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010043 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010044
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010045 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010046 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010047 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010048#if defined(MBEDTLS_X509_CRT_PARSE_C)
10049#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10050 if( session->peer_cert == NULL )
10051 cert_len = 0;
10052 else
10053 cert_len = session->peer_cert->raw.len;
10054
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010055 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010056
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010057 if( used <= buf_len )
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010058 {
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010059 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
10060 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
10061 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010062
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010063 if( session->peer_cert != NULL )
10064 {
10065 memcpy( p, session->peer_cert->raw.p, cert_len );
10066 p += cert_len;
10067 }
10068 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010069#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010070 if( session->peer_cert_digest != NULL )
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010071 {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010072 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10073 if( used <= buf_len )
10074 {
10075 *p++ = (unsigned char) session->peer_cert_digest_type;
10076 *p++ = (unsigned char) session->peer_cert_digest_len;
10077 memcpy( p, session->peer_cert_digest,
10078 session->peer_cert_digest_len );
10079 p += session->peer_cert_digest_len;
10080 }
10081 }
10082 else
10083 {
10084 used += 2;
10085 if( used <= buf_len )
10086 {
10087 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10088 *p++ = 0;
10089 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010090 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010091#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10092#endif /* MBEDTLS_X509_CRT_PARSE_C */
10093
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010094 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010095 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010096 */
10097#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010098 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010099
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010100 if( used <= buf_len )
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010101 {
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010102 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
10103 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
10104 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
10105
10106 if( session->ticket != NULL )
10107 {
10108 memcpy( p, session->ticket, session->ticket_len );
10109 p += session->ticket_len;
10110 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010111
10112 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
10113 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
10114 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
10115 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010116 }
10117#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10118
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010119 /*
10120 * Misc extension-related info
10121 */
10122#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10123 used += 1;
10124
10125 if( used <= buf_len )
10126 *p++ = session->mfl_code;
10127#endif
10128
10129#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10130 used += 1;
10131
10132 if( used <= buf_len )
10133 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10134#endif
10135
10136#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10137 used += 1;
10138
10139 if( used <= buf_len )
10140 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10141#endif
10142
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010143 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010144 *olen = used;
10145
10146 if( used > buf_len )
10147 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010148
10149 return( 0 );
10150}
10151
10152/*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010153 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +020010154 *
10155 * This internal version is wrapped by a public function that cleans up in
10156 * case of error.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010157 */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +020010158static int ssl_session_load( mbedtls_ssl_session *session,
10159 const unsigned char *buf,
10160 size_t len )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010161{
10162 const unsigned char *p = buf;
10163 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010164#if defined(MBEDTLS_HAVE_TIME)
10165 uint64_t start;
10166#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010167#if defined(MBEDTLS_X509_CRT_PARSE_C)
10168#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10169 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010170#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10171#endif /* MBEDTLS_X509_CRT_PARSE_C */
10172
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010173 /*
Hanno Beckera835da52019-05-16 12:39:07 +010010174 * Check version identifier
10175 */
10176
10177 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Beckerf37d9182019-05-28 13:59:44 +010010178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckera835da52019-05-16 12:39:07 +010010179
10180 if( memcmp( p, ssl_serialized_session_header,
10181 sizeof( ssl_serialized_session_header ) ) != 0 )
10182 {
Hanno Beckerf9b33032019-06-03 12:58:39 +010010183 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckera835da52019-05-16 12:39:07 +010010184 }
10185 p += sizeof( ssl_serialized_session_header );
10186
10187 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010188 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010189 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010190#if defined(MBEDTLS_HAVE_TIME)
10191 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10193
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010194 start = ( (uint64_t) p[0] << 56 ) |
10195 ( (uint64_t) p[1] << 48 ) |
10196 ( (uint64_t) p[2] << 40 ) |
10197 ( (uint64_t) p[3] << 32 ) |
10198 ( (uint64_t) p[4] << 24 ) |
10199 ( (uint64_t) p[5] << 16 ) |
10200 ( (uint64_t) p[6] << 8 ) |
10201 ( (uint64_t) p[7] );
10202 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010203
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010204 session->start = (time_t) start;
10205#endif /* MBEDTLS_HAVE_TIME */
10206
10207 /*
10208 * Basic mandatory fields
10209 */
10210 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
10211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10212
10213 session->ciphersuite = ( p[0] << 8 ) | p[1];
10214 p += 2;
10215
10216 session->compression = *p++;
10217
10218 session->id_len = *p++;
10219 memcpy( session->id, p, 32 );
10220 p += 32;
10221
10222 memcpy( session->master, p, 48 );
10223 p += 48;
10224
10225 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10226 ( (uint32_t) p[1] << 16 ) |
10227 ( (uint32_t) p[2] << 8 ) |
10228 ( (uint32_t) p[3] );
10229 p += 4;
10230
10231 /* Immediately clear invalid pointer values that have been read, in case
10232 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010233#if defined(MBEDTLS_X509_CRT_PARSE_C)
10234#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10235 session->peer_cert = NULL;
10236#else
10237 session->peer_cert_digest = NULL;
10238#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10239#endif /* MBEDTLS_X509_CRT_PARSE_C */
10240#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10241 session->ticket = NULL;
10242#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10243
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010244 /*
10245 * Peer certificate
10246 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010247#if defined(MBEDTLS_X509_CRT_PARSE_C)
10248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10249 /* Deserialize CRT from the end of the ticket. */
10250 if( 3 > (size_t)( end - p ) )
10251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10252
10253 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10254 p += 3;
10255
10256 if( cert_len != 0 )
10257 {
10258 int ret;
10259
10260 if( cert_len > (size_t)( end - p ) )
10261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10262
10263 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10264
10265 if( session->peer_cert == NULL )
10266 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10267
10268 mbedtls_x509_crt_init( session->peer_cert );
10269
10270 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10271 p, cert_len ) ) != 0 )
10272 {
10273 mbedtls_x509_crt_free( session->peer_cert );
10274 mbedtls_free( session->peer_cert );
10275 session->peer_cert = NULL;
10276 return( ret );
10277 }
10278
10279 p += cert_len;
10280 }
10281#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10282 /* Deserialize CRT digest from the end of the ticket. */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010283 if( 2 > (size_t)( end - p ) )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10285
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010286 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10287 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010288
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010289 if( session->peer_cert_digest_len != 0 )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010290 {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010291 const mbedtls_md_info_t *md_info =
10292 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10293 if( md_info == NULL )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010295 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010297
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010298 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10300
10301 session->peer_cert_digest =
10302 mbedtls_calloc( 1, session->peer_cert_digest_len );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010303 if( session->peer_cert_digest == NULL )
10304 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10305
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010306 memcpy( session->peer_cert_digest, p,
10307 session->peer_cert_digest_len );
10308 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010309 }
10310#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10311#endif /* MBEDTLS_X509_CRT_PARSE_C */
10312
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010313 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010314 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010315 */
10316#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10317 if( 3 > (size_t)( end - p ) )
10318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10319
10320 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10321 p += 3;
10322
10323 if( session->ticket_len != 0 )
10324 {
10325 if( session->ticket_len > (size_t)( end - p ) )
10326 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10327
10328 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10329 if( session->ticket == NULL )
10330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10331
10332 memcpy( session->ticket, p, session->ticket_len );
10333 p += session->ticket_len;
10334 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010335
10336 if( 4 > (size_t)( end - p ) )
10337 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10338
10339 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10340 ( (uint32_t) p[1] << 16 ) |
10341 ( (uint32_t) p[2] << 8 ) |
10342 ( (uint32_t) p[3] );
10343 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010344#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10345
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010346 /*
10347 * Misc extension-related info
10348 */
10349#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10350 if( 1 > (size_t)( end - p ) )
10351 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10352
10353 session->mfl_code = *p++;
10354#endif
10355
10356#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10357 if( 1 > (size_t)( end - p ) )
10358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10359
10360 session->trunc_hmac = *p++;
10361#endif
10362
10363#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10364 if( 1 > (size_t)( end - p ) )
10365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10366
10367 session->encrypt_then_mac = *p++;
10368#endif
10369
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010370 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010371 if( p != end )
10372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10373
10374 return( 0 );
10375}
10376
10377/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +020010378 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +020010379 */
10380int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10381 const unsigned char *buf,
10382 size_t len )
10383{
10384 int ret = ssl_session_load( session, buf, len );
10385
10386 if( ret != 0 )
10387 mbedtls_ssl_session_free( session );
10388
10389 return( ret );
10390}
10391
10392/*
Paul Bakker1961b702013-01-25 14:49:24 +010010393 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010395int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010398
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010399 if( ssl == NULL || ssl->conf == NULL )
10400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010402#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010403 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010405#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010407 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010409#endif
10410
Paul Bakker1961b702013-01-25 14:49:24 +010010411 return( ret );
10412}
10413
10414/*
10415 * Perform the SSL handshake
10416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010418{
10419 int ret = 0;
10420
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010421 if( ssl == NULL || ssl->conf == NULL )
10422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010426 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010428 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010429
10430 if( ret != 0 )
10431 break;
10432 }
10433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010435
10436 return( ret );
10437}
10438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439#if defined(MBEDTLS_SSL_RENEGOTIATION)
10440#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010441/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010442 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010445{
10446 int ret;
10447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010449
10450 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10452 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010453
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010454 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010455 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010457 return( ret );
10458 }
10459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010461
10462 return( 0 );
10463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010464#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010465
10466/*
10467 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468 * - any side: calling mbedtls_ssl_renegotiate(),
10469 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10470 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010471 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010472 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010476{
10477 int ret;
10478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010480
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010481 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10482 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010483
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010484 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10485 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010486#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010487 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010489 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010490 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010491 ssl->handshake->out_msg_seq = 1;
10492 else
10493 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010494 }
10495#endif
10496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10498 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010503 return( ret );
10504 }
10505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010507
10508 return( 0 );
10509}
10510
10511/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010512 * Renegotiate current connection on client,
10513 * or request renegotiation on server
10514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010515int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010517 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010518
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010519 if( ssl == NULL || ssl->conf == NULL )
10520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010522#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010523 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010524 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010526 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010529 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010530
10531 /* Did we already try/start sending HelloRequest? */
10532 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010533 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010534
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010535 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010539#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010540 /*
10541 * On client, either start the renegotiation process or,
10542 * if already in progress, continue the handshake
10543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010544 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010546 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010548
10549 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010551 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010552 return( ret );
10553 }
10554 }
10555 else
10556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010557 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010560 return( ret );
10561 }
10562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010563#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010564
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010565 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010566}
10567
10568/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010569 * Check record counters and renegotiate if they're above the limit.
10570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010571static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010572{
Andres AG2196c7f2016-12-15 17:01:16 +000010573 size_t ep_len = ssl_ep_len( ssl );
10574 int in_ctr_cmp;
10575 int out_ctr_cmp;
10576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10578 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010579 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010580 {
10581 return( 0 );
10582 }
10583
Andres AG2196c7f2016-12-15 17:01:16 +000010584 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10585 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010586 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010587 ssl->conf->renego_period + ep_len, 8 - ep_len );
10588
10589 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010590 {
10591 return( 0 );
10592 }
10593
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010596}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010598
10599/*
10600 * Receive application data decrypted from the SSL layer
10601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010602int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010603{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010604 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010605 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010606
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010607 if( ssl == NULL || ssl->conf == NULL )
10608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010613 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010615 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010616 return( ret );
10617
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010618 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010620 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010621 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010622 return( ret );
10623 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010624 }
10625#endif
10626
Hanno Becker4a810fb2017-05-24 16:27:30 +010010627 /*
10628 * Check if renegotiation is necessary and/or handshake is
10629 * in process. If yes, perform/continue, and fall through
10630 * if an unexpected packet is received while the client
10631 * is waiting for the ServerHello.
10632 *
10633 * (There is no equivalent to the last condition on
10634 * the server-side as it is not treated as within
10635 * a handshake while waiting for the ClientHello
10636 * after a renegotiation request.)
10637 */
10638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010640 ret = ssl_check_ctr_renegotiate( ssl );
10641 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10642 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010645 return( ret );
10646 }
10647#endif
10648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010651 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010652 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10653 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010656 return( ret );
10657 }
10658 }
10659
Hanno Beckere41158b2017-10-23 13:30:32 +010010660 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010661 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010662 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010663 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010664 if( ssl->f_get_timer != NULL &&
10665 ssl->f_get_timer( ssl->p_timer ) == -1 )
10666 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010667 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010668 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010669
Hanno Becker327c93b2018-08-15 13:56:18 +010010670 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010671 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010672 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10673 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010674
Hanno Becker4a810fb2017-05-24 16:27:30 +010010675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10676 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010677 }
10678
10679 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010681 {
10682 /*
10683 * OpenSSL sends empty messages to randomize the IV
10684 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010685 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010688 return( 0 );
10689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010691 return( ret );
10692 }
10693 }
10694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010695 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010698
Hanno Becker4a810fb2017-05-24 16:27:30 +010010699 /*
10700 * - For client-side, expect SERVER_HELLO_REQUEST.
10701 * - For server-side, expect CLIENT_HELLO.
10702 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10703 */
10704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010706 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010708 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010711
10712 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010714 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010715 {
10716 continue;
10717 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010718#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010720 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010721#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010722
Hanno Becker4a810fb2017-05-24 16:27:30 +010010723#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010724 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010728
10729 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010731 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010732 {
10733 continue;
10734 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010736 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010737 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010738#endif /* MBEDTLS_SSL_SRV_C */
10739
Hanno Becker21df7f92017-10-17 11:03:26 +010010740#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010741 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010742 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10743 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10744 ssl->conf->allow_legacy_renegotiation ==
10745 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10746 {
10747 /*
10748 * Accept renegotiation request
10749 */
Paul Bakker48916f92012-09-16 19:57:18 +000010750
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010751 /* DTLS clients need to know renego is server-initiated */
10752#if defined(MBEDTLS_SSL_PROTO_DTLS)
10753 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10754 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10755 {
10756 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10757 }
10758#endif
10759 ret = ssl_start_renegotiation( ssl );
10760 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10761 ret != 0 )
10762 {
10763 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10764 return( ret );
10765 }
10766 }
10767 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010768#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010769 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010770 /*
10771 * Refuse renegotiation
10772 */
10773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776#if defined(MBEDTLS_SSL_PROTO_SSL3)
10777 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010778 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010779 /* SSLv3 does not have a "no_renegotiation" warning, so
10780 we send a fatal alert and abort the connection. */
10781 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10782 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10783 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010784 }
10785 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010786#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10787#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10788 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10789 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10792 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10793 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010794 {
10795 return( ret );
10796 }
Paul Bakker48916f92012-09-16 19:57:18 +000010797 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010798 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10800 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010804 }
Paul Bakker48916f92012-09-16 19:57:18 +000010805 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010806
Hanno Becker90333da2017-10-10 11:27:13 +010010807 /* At this point, we don't know whether the renegotiation has been
10808 * completed or not. The cases to consider are the following:
10809 * 1) The renegotiation is complete. In this case, no new record
10810 * has been read yet.
10811 * 2) The renegotiation is incomplete because the client received
10812 * an application data record while awaiting the ServerHello.
10813 * 3) The renegotiation is incomplete because the client received
10814 * a non-handshake, non-application data message while awaiting
10815 * the ServerHello.
10816 * In each of these case, looping will be the proper action:
10817 * - For 1), the next iteration will read a new record and check
10818 * if it's application data.
10819 * - For 2), the loop condition isn't satisfied as application data
10820 * is present, hence continue is the same as break
10821 * - For 3), the loop condition is satisfied and read_record
10822 * will re-deliver the message that was held back by the client
10823 * when expecting the ServerHello.
10824 */
10825 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010826 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010827#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010828 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010829 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010830 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010831 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010832 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010835 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010837 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010838 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010840#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010842 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10843 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010846 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010847 }
10848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10852 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010853 }
10854
10855 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010856
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010857 /* We're going to return something now, cancel timer,
10858 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010859 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010860 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010861
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010863 /* If we requested renego but received AppData, resend HelloRequest.
10864 * Do it now, after setting in_offt, to avoid taking this branch
10865 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010866#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010867 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010869 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010870 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010873 return( ret );
10874 }
10875 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010876#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010877#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010878 }
10879
10880 n = ( len < ssl->in_msglen )
10881 ? len : ssl->in_msglen;
10882
10883 memcpy( buf, ssl->in_offt, n );
10884 ssl->in_msglen -= n;
10885
10886 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010887 {
10888 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010889 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010890 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010891 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010892 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010893 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010894 /* more data available */
10895 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010896 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010899
Paul Bakker23986e52011-04-24 08:57:21 +000010900 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010901}
10902
10903/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010904 * Send application data to be encrypted by the SSL layer, taking care of max
10905 * fragment length and buffer size.
10906 *
10907 * According to RFC 5246 Section 6.2.1:
10908 *
10909 * Zero-length fragments of Application data MAY be sent as they are
10910 * potentially useful as a traffic analysis countermeasure.
10911 *
10912 * Therefore, it is possible that the input message length is 0 and the
10913 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010914 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010915static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010916 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010917{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010918 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10919 const size_t max_len = (size_t) ret;
10920
10921 if( ret < 0 )
10922 {
10923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10924 return( ret );
10925 }
10926
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010927 if( len > max_len )
10928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010930 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010933 "maximum fragment length: %d > %d",
10934 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010936 }
10937 else
10938#endif
10939 len = max_len;
10940 }
Paul Bakker887bd502011-06-08 13:10:54 +000010941
Paul Bakker5121ce52009-01-03 21:22:43 +000010942 if( ssl->out_left != 0 )
10943 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010944 /*
10945 * The user has previously tried to send the data and
10946 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10947 * written. In this case, we expect the high-level write function
10948 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10949 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010950 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010953 return( ret );
10954 }
10955 }
Paul Bakker887bd502011-06-08 13:10:54 +000010956 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010957 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010958 /*
10959 * The user is trying to send a message the first time, so we need to
10960 * copy the data into the internal buffers and setup the data structure
10961 * to keep track of partial writes
10962 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010963 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010965 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010966
Hanno Becker67bc7c32018-08-06 11:33:50 +010010967 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010970 return( ret );
10971 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010972 }
10973
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010974 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010975}
10976
10977/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010978 * Write application data, doing 1/n-1 splitting if necessary.
10979 *
10980 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010981 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010982 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010984#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010985static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010986 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010987{
10988 int ret;
10989
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010990 if( ssl->conf->cbc_record_splitting ==
10991 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010992 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10994 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10995 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010996 {
10997 return( ssl_write_real( ssl, buf, len ) );
10998 }
10999
11000 if( ssl->split_done == 0 )
11001 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011002 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011003 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011004 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011005 }
11006
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011007 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11008 return( ret );
11009 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011010
11011 return( ret + 1 );
11012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011013#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011014
11015/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011016 * Write application data (public-facing wrapper)
11017 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011018int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011019{
11020 int ret;
11021
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011023
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011024 if( ssl == NULL || ssl->conf == NULL )
11025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11026
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011027#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011028 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11029 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011030 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011031 return( ret );
11032 }
11033#endif
11034
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011035 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011036 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011037 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011038 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011040 return( ret );
11041 }
11042 }
11043
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011044#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011045 ret = ssl_write_split( ssl, buf, len );
11046#else
11047 ret = ssl_write_real( ssl, buf, len );
11048#endif
11049
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011051
11052 return( ret );
11053}
11054
11055/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011056 * Notify the peer that the connection is being closed
11057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011058int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011059{
11060 int ret;
11061
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011062 if( ssl == NULL || ssl->conf == NULL )
11063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011066
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011067 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11073 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11074 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011077 return( ret );
11078 }
11079 }
11080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011082
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011083 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011084}
11085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011086void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011087{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011088 if( transform == NULL )
11089 return;
11090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011091#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011092 deflateEnd( &transform->ctx_deflate );
11093 inflateEnd( &transform->ctx_inflate );
11094#endif
11095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011096 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11097 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011098
Hanno Beckerd56ed242018-01-03 15:32:51 +000011099#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011100 mbedtls_md_free( &transform->md_ctx_enc );
11101 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000011102#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011103
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011104 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011105}
11106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011107#if defined(MBEDTLS_X509_CRT_PARSE_C)
11108static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011110 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011111
11112 while( cur != NULL )
11113 {
11114 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011115 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011116 cur = next;
11117 }
11118}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011119#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011120
Hanno Becker0271f962018-08-16 13:23:47 +010011121#if defined(MBEDTLS_SSL_PROTO_DTLS)
11122
11123static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11124{
11125 unsigned offset;
11126 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11127
11128 if( hs == NULL )
11129 return;
11130
Hanno Becker283f5ef2018-08-24 09:34:47 +010011131 ssl_free_buffered_record( ssl );
11132
Hanno Becker0271f962018-08-16 13:23:47 +010011133 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011134 ssl_buffering_free_slot( ssl, offset );
11135}
11136
11137static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11138 uint8_t slot )
11139{
11140 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11141 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011142
11143 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11144 return;
11145
Hanno Beckere605b192018-08-21 15:59:07 +010011146 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011147 {
Hanno Beckere605b192018-08-21 15:59:07 +010011148 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011149 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011150 mbedtls_free( hs_buf->data );
11151 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011152 }
11153}
11154
11155#endif /* MBEDTLS_SSL_PROTO_DTLS */
11156
Gilles Peskine9b562d52018-04-25 20:32:43 +020011157void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011158{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011159 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11160
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011161 if( handshake == NULL )
11162 return;
11163
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011164#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11165 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11166 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011167 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011168 handshake->async_in_progress = 0;
11169 }
11170#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11171
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011172#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11173 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11174 mbedtls_md5_free( &handshake->fin_md5 );
11175 mbedtls_sha1_free( &handshake->fin_sha1 );
11176#endif
11177#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11178#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050011179#if defined(MBEDTLS_USE_PSA_CRYPTO)
11180 psa_hash_abort( &handshake->fin_sha256_psa );
11181#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011182 mbedtls_sha256_free( &handshake->fin_sha256 );
11183#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050011184#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011185#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050011186#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050011187 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050011188#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011189 mbedtls_sha512_free( &handshake->fin_sha512 );
11190#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050011191#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011192#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011194#if defined(MBEDTLS_DHM_C)
11195 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011197#if defined(MBEDTLS_ECDH_C)
11198 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011199#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011200#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011201 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011202#if defined(MBEDTLS_SSL_CLI_C)
11203 mbedtls_free( handshake->ecjpake_cache );
11204 handshake->ecjpake_cache = NULL;
11205 handshake->ecjpake_cache_len = 0;
11206#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011207#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011208
Janos Follath4ae5c292016-02-10 11:27:43 +000011209#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
11210 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020011211 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011212 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020011213#endif
11214
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011215#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11216 if( handshake->psk != NULL )
11217 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011218 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011219 mbedtls_free( handshake->psk );
11220 }
11221#endif
11222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011223#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11224 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011225 /*
11226 * Free only the linked list wrapper, not the keys themselves
11227 * since the belong to the SNI callback
11228 */
11229 if( handshake->sni_key_cert != NULL )
11230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011231 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011232
11233 while( cur != NULL )
11234 {
11235 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011236 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011237 cur = next;
11238 }
11239 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011240#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011241
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011242#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011243 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000011244 if( handshake->ecrs_peer_cert != NULL )
11245 {
11246 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11247 mbedtls_free( handshake->ecrs_peer_cert );
11248 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011249#endif
11250
Hanno Becker75173122019-02-06 16:18:31 +000011251#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11252 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11253 mbedtls_pk_free( &handshake->peer_pubkey );
11254#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011256#if defined(MBEDTLS_SSL_PROTO_DTLS)
11257 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011258 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011259 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011260#endif
11261
Hanno Becker4a63ed42019-01-08 11:39:35 +000011262#if defined(MBEDTLS_ECDH_C) && \
11263 defined(MBEDTLS_USE_PSA_CRYPTO)
11264 psa_destroy_key( handshake->ecdh_psa_privkey );
11265#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
11266
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011267 mbedtls_platform_zeroize( handshake,
11268 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011269}
11270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011271void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011272{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011273 if( session == NULL )
11274 return;
11275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011276#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000011277 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011278#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011279
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011280#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011281 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011282#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011283
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011284 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011285}
11286
Paul Bakker5121ce52009-01-03 21:22:43 +000011287/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +020011288 * Serialize a full SSL context
11289 */
11290int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11291 unsigned char *buf,
11292 size_t buf_len,
11293 size_t *olen )
11294{
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +020011295 /*
11296 * Enforce current usage restrictions
11297 */
11298 if( /* The initial handshake is over ... */
11299 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
11300 ssl->handshake != NULL ||
11301 /* ... and the various sub-structures are indeed ready. */
11302 ssl->transform == NULL ||
11303 ssl->session == NULL ||
11304 /* There is no pending incoming or outgoing data ... */
11305 mbedtls_ssl_check_pending( ssl ) != 0 ||
11306 ssl->out_left != 0 ||
11307 /* We're using DTLS 1.2 ... */
11308 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
11309 ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ||
11310 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ||
11311 /* ... with an AEAD ciphersuite. */
11312 mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ||
11313 /* Renegotation is disabled. */
11314#if defined(MBEDTLS_SSL_RENEGOTIATION)
11315 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED
11316#endif
11317 )
11318 {
11319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11320 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +020011321
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +020011322 /* Unimplemented */
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +020011323 if( buf != NULL )
11324 memset( buf, 0, buf_len );
11325
11326 *olen = 0;
11327
11328 return( 0 );
11329}
11330
11331/*
11332 * Deserialize a full SSL context
11333 */
11334int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
11335 const unsigned char *buf,
11336 size_t len )
11337{
Manuel Pégourié-Gonnard6d8f1282019-06-05 09:47:18 +020011338 /* Unimplemented */
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +020011339 (void) ssl;
11340 (void) buf;
11341 (void) len;
11342
11343 return( 0 );
11344}
11345
11346/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011347 * Free an SSL context
11348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011349void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011350{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011351 if( ssl == NULL )
11352 return;
11353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011355
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011356 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011357 {
Angus Grattond8213d02016-05-25 20:56:48 +100011358 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011359 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011360 }
11361
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011362 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011363 {
Angus Grattond8213d02016-05-25 20:56:48 +100011364 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011365 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011366 }
11367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011368#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011369 if( ssl->compress_buf != NULL )
11370 {
Angus Grattond8213d02016-05-25 20:56:48 +100011371 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011372 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011373 }
11374#endif
11375
Paul Bakker48916f92012-09-16 19:57:18 +000011376 if( ssl->transform )
11377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011378 mbedtls_ssl_transform_free( ssl->transform );
11379 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011380 }
11381
11382 if( ssl->handshake )
11383 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011384 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011385 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11386 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011388 mbedtls_free( ssl->handshake );
11389 mbedtls_free( ssl->transform_negotiate );
11390 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011391 }
11392
Paul Bakkerc0463502013-02-14 11:19:38 +010011393 if( ssl->session )
11394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011395 mbedtls_ssl_session_free( ssl->session );
11396 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011397 }
11398
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020011399#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020011400 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011401 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011402 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011403 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011404 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011405#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011407#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11408 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11411 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011412 }
11413#endif
11414
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011415#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011416 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011417#endif
11418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011420
Paul Bakker86f04f42013-02-14 11:20:09 +010011421 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011422 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011423}
11424
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011425/*
11426 * Initialze mbedtls_ssl_config
11427 */
11428void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11429{
11430 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
11431}
11432
Simon Butcherc97b6972015-12-27 23:48:17 +000011433#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011434static int ssl_preset_default_hashes[] = {
11435#if defined(MBEDTLS_SHA512_C)
11436 MBEDTLS_MD_SHA512,
11437 MBEDTLS_MD_SHA384,
11438#endif
11439#if defined(MBEDTLS_SHA256_C)
11440 MBEDTLS_MD_SHA256,
11441 MBEDTLS_MD_SHA224,
11442#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011443#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011444 MBEDTLS_MD_SHA1,
11445#endif
11446 MBEDTLS_MD_NONE
11447};
Simon Butcherc97b6972015-12-27 23:48:17 +000011448#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011449
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011450static int ssl_preset_suiteb_ciphersuites[] = {
11451 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11452 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11453 0
11454};
11455
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011456#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011457static int ssl_preset_suiteb_hashes[] = {
11458 MBEDTLS_MD_SHA256,
11459 MBEDTLS_MD_SHA384,
11460 MBEDTLS_MD_NONE
11461};
11462#endif
11463
11464#if defined(MBEDTLS_ECP_C)
11465static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010011466#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011467 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010011468#endif
11469#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011470 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010011471#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011472 MBEDTLS_ECP_DP_NONE
11473};
11474#endif
11475
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011476/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011477 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011478 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011479int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011480 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011481{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011482#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011483 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011484#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011485
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011486 /* Use the functions here so that they are covered in tests,
11487 * but otherwise access member directly for efficiency */
11488 mbedtls_ssl_conf_endpoint( conf, endpoint );
11489 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011490
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011491 /*
11492 * Things that are common to all presets
11493 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011494#if defined(MBEDTLS_SSL_CLI_C)
11495 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11496 {
11497 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
11498#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11499 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11500#endif
11501 }
11502#endif
11503
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011504#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011505 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011506#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011507
11508#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11509 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11510#endif
11511
11512#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
11513 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
11514#endif
11515
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011516#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11517 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11518#endif
11519
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011520#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011521 conf->f_cookie_write = ssl_cookie_write_dummy;
11522 conf->f_cookie_check = ssl_cookie_check_dummy;
11523#endif
11524
11525#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11526 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11527#endif
11528
Janos Follath088ce432017-04-10 12:42:31 +010011529#if defined(MBEDTLS_SSL_SRV_C)
11530 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
11531#endif
11532
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011533#if defined(MBEDTLS_SSL_PROTO_DTLS)
11534 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
11535 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
11536#endif
11537
11538#if defined(MBEDTLS_SSL_RENEGOTIATION)
11539 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011540 memset( conf->renego_period, 0x00, 2 );
11541 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011542#endif
11543
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011544#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11545 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11546 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011547 const unsigned char dhm_p[] =
11548 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11549 const unsigned char dhm_g[] =
11550 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11551
Hanno Beckera90658f2017-10-04 15:29:08 +010011552 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11553 dhm_p, sizeof( dhm_p ),
11554 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011555 {
11556 return( ret );
11557 }
11558 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011559#endif
11560
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011561 /*
11562 * Preset-specific defaults
11563 */
11564 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011565 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011566 /*
11567 * NSA Suite B
11568 */
11569 case MBEDTLS_SSL_PRESET_SUITEB:
11570 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
11571 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
11572 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11573 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11574
11575 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11576 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11577 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11578 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11579 ssl_preset_suiteb_ciphersuites;
11580
11581#if defined(MBEDTLS_X509_CRT_PARSE_C)
11582 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011583#endif
11584
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011585#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011586 conf->sig_hashes = ssl_preset_suiteb_hashes;
11587#endif
11588
11589#if defined(MBEDTLS_ECP_C)
11590 conf->curve_list = ssl_preset_suiteb_curves;
11591#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011592 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011593
11594 /*
11595 * Default
11596 */
11597 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011598 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11599 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11600 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11601 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
11602 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11603 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11604 MBEDTLS_SSL_MIN_MINOR_VERSION :
11605 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011606 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11607 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11608
11609#if defined(MBEDTLS_SSL_PROTO_DTLS)
11610 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
11611 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11612#endif
11613
11614 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11615 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11616 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11617 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11618 mbedtls_ssl_list_ciphersuites();
11619
11620#if defined(MBEDTLS_X509_CRT_PARSE_C)
11621 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11622#endif
11623
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011624#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011625 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011626#endif
11627
11628#if defined(MBEDTLS_ECP_C)
11629 conf->curve_list = mbedtls_ecp_grp_id_list();
11630#endif
11631
11632#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
11633 conf->dhm_min_bitlen = 1024;
11634#endif
11635 }
11636
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011637 return( 0 );
11638}
11639
11640/*
11641 * Free mbedtls_ssl_config
11642 */
11643void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
11644{
11645#if defined(MBEDTLS_DHM_C)
11646 mbedtls_mpi_free( &conf->dhm_P );
11647 mbedtls_mpi_free( &conf->dhm_G );
11648#endif
11649
11650#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11651 if( conf->psk != NULL )
11652 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011653 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011654 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000011655 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011656 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090011657 }
11658
11659 if( conf->psk_identity != NULL )
11660 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011661 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090011662 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000011663 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011664 conf->psk_identity_len = 0;
11665 }
11666#endif
11667
11668#if defined(MBEDTLS_X509_CRT_PARSE_C)
11669 ssl_key_cert_free( conf->key_cert );
11670#endif
11671
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011672 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011673}
11674
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011675#if defined(MBEDTLS_PK_C) && \
11676 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011677/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011678 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011680unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011682#if defined(MBEDTLS_RSA_C)
11683 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11684 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011685#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011686#if defined(MBEDTLS_ECDSA_C)
11687 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11688 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011689#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011690 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011691}
11692
Hanno Becker7e5437a2017-04-28 17:15:26 +010011693unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11694{
11695 switch( type ) {
11696 case MBEDTLS_PK_RSA:
11697 return( MBEDTLS_SSL_SIG_RSA );
11698 case MBEDTLS_PK_ECDSA:
11699 case MBEDTLS_PK_ECKEY:
11700 return( MBEDTLS_SSL_SIG_ECDSA );
11701 default:
11702 return( MBEDTLS_SSL_SIG_ANON );
11703 }
11704}
11705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011706mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011707{
11708 switch( sig )
11709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011710#if defined(MBEDTLS_RSA_C)
11711 case MBEDTLS_SSL_SIG_RSA:
11712 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011713#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011714#if defined(MBEDTLS_ECDSA_C)
11715 case MBEDTLS_SSL_SIG_ECDSA:
11716 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011717#endif
11718 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011719 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011720 }
11721}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011722#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011723
Hanno Becker7e5437a2017-04-28 17:15:26 +010011724#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11725 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11726
11727/* Find an entry in a signature-hash set matching a given hash algorithm. */
11728mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11729 mbedtls_pk_type_t sig_alg )
11730{
11731 switch( sig_alg )
11732 {
11733 case MBEDTLS_PK_RSA:
11734 return( set->rsa );
11735 case MBEDTLS_PK_ECDSA:
11736 return( set->ecdsa );
11737 default:
11738 return( MBEDTLS_MD_NONE );
11739 }
11740}
11741
11742/* Add a signature-hash-pair to a signature-hash set */
11743void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11744 mbedtls_pk_type_t sig_alg,
11745 mbedtls_md_type_t md_alg )
11746{
11747 switch( sig_alg )
11748 {
11749 case MBEDTLS_PK_RSA:
11750 if( set->rsa == MBEDTLS_MD_NONE )
11751 set->rsa = md_alg;
11752 break;
11753
11754 case MBEDTLS_PK_ECDSA:
11755 if( set->ecdsa == MBEDTLS_MD_NONE )
11756 set->ecdsa = md_alg;
11757 break;
11758
11759 default:
11760 break;
11761 }
11762}
11763
11764/* Allow exactly one hash algorithm for each signature. */
11765void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11766 mbedtls_md_type_t md_alg )
11767{
11768 set->rsa = md_alg;
11769 set->ecdsa = md_alg;
11770}
11771
11772#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11773 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11774
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011775/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011776 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011778mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011779{
11780 switch( hash )
11781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011782#if defined(MBEDTLS_MD5_C)
11783 case MBEDTLS_SSL_HASH_MD5:
11784 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011786#if defined(MBEDTLS_SHA1_C)
11787 case MBEDTLS_SSL_HASH_SHA1:
11788 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011790#if defined(MBEDTLS_SHA256_C)
11791 case MBEDTLS_SSL_HASH_SHA224:
11792 return( MBEDTLS_MD_SHA224 );
11793 case MBEDTLS_SSL_HASH_SHA256:
11794 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011796#if defined(MBEDTLS_SHA512_C)
11797 case MBEDTLS_SSL_HASH_SHA384:
11798 return( MBEDTLS_MD_SHA384 );
11799 case MBEDTLS_SSL_HASH_SHA512:
11800 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011801#endif
11802 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011803 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011804 }
11805}
11806
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011807/*
11808 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11809 */
11810unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11811{
11812 switch( md )
11813 {
11814#if defined(MBEDTLS_MD5_C)
11815 case MBEDTLS_MD_MD5:
11816 return( MBEDTLS_SSL_HASH_MD5 );
11817#endif
11818#if defined(MBEDTLS_SHA1_C)
11819 case MBEDTLS_MD_SHA1:
11820 return( MBEDTLS_SSL_HASH_SHA1 );
11821#endif
11822#if defined(MBEDTLS_SHA256_C)
11823 case MBEDTLS_MD_SHA224:
11824 return( MBEDTLS_SSL_HASH_SHA224 );
11825 case MBEDTLS_MD_SHA256:
11826 return( MBEDTLS_SSL_HASH_SHA256 );
11827#endif
11828#if defined(MBEDTLS_SHA512_C)
11829 case MBEDTLS_MD_SHA384:
11830 return( MBEDTLS_SSL_HASH_SHA384 );
11831 case MBEDTLS_MD_SHA512:
11832 return( MBEDTLS_SSL_HASH_SHA512 );
11833#endif
11834 default:
11835 return( MBEDTLS_SSL_HASH_NONE );
11836 }
11837}
11838
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011839#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011840/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011841 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011842 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011843 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011844int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011846 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011847
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011848 if( ssl->conf->curve_list == NULL )
11849 return( -1 );
11850
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011851 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011852 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011853 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011854
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011855 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011856}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011857#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011858
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011859#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011860/*
11861 * Check if a hash proposed by the peer is in our list.
11862 * Return 0 if we're willing to use it, -1 otherwise.
11863 */
11864int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11865 mbedtls_md_type_t md )
11866{
11867 const int *cur;
11868
11869 if( ssl->conf->sig_hashes == NULL )
11870 return( -1 );
11871
11872 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11873 if( *cur == (int) md )
11874 return( 0 );
11875
11876 return( -1 );
11877}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011878#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011880#if defined(MBEDTLS_X509_CRT_PARSE_C)
11881int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11882 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011883 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011884 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011885{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011886 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011887#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011888 int usage = 0;
11889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011890#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011891 const char *ext_oid;
11892 size_t ext_len;
11893#endif
11894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011895#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11896 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011897 ((void) cert);
11898 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011899 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011900#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011902#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11903 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011904 {
11905 /* Server part of the key exchange */
11906 switch( ciphersuite->key_exchange )
11907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011908 case MBEDTLS_KEY_EXCHANGE_RSA:
11909 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011910 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011911 break;
11912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011913 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11914 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11915 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11916 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011917 break;
11918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011919 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11920 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011921 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011922 break;
11923
11924 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011925 case MBEDTLS_KEY_EXCHANGE_NONE:
11926 case MBEDTLS_KEY_EXCHANGE_PSK:
11927 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11928 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011929 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011930 usage = 0;
11931 }
11932 }
11933 else
11934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011935 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11936 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011937 }
11938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011939 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011940 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011941 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011942 ret = -1;
11943 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011944#else
11945 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011946#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011948#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11949 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011951 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11952 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011953 }
11954 else
11955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011956 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11957 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011958 }
11959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011960 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011961 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011962 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011963 ret = -1;
11964 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011965#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011966
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011967 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011968}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011969#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011970
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011971/*
11972 * Convert version numbers to/from wire format
11973 * and, for DTLS, to/from TLS equivalent.
11974 *
11975 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011976 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011977 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11978 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011980void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011981 unsigned char ver[2] )
11982{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011983#if defined(MBEDTLS_SSL_PROTO_DTLS)
11984 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011986 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011987 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11988
11989 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11990 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11991 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011992 else
11993#else
11994 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011995#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011996 {
11997 ver[0] = (unsigned char) major;
11998 ver[1] = (unsigned char) minor;
11999 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010012000}
12001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012002void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010012003 const unsigned char ver[2] )
12004{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012005#if defined(MBEDTLS_SSL_PROTO_DTLS)
12006 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010012007 {
12008 *major = 255 - ver[0] + 2;
12009 *minor = 255 - ver[1] + 1;
12010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012011 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010012012 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
12013 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010012014 else
12015#else
12016 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010012017#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010012018 {
12019 *major = ver[0];
12020 *minor = ver[1];
12021 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010012022}
12023
Simon Butcher99000142016-10-13 17:21:01 +010012024int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
12025{
12026#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
12027 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
12028 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
12029
12030 switch( md )
12031 {
12032#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
12033#if defined(MBEDTLS_MD5_C)
12034 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010012035 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010012036#endif
12037#if defined(MBEDTLS_SHA1_C)
12038 case MBEDTLS_SSL_HASH_SHA1:
12039 ssl->handshake->calc_verify = ssl_calc_verify_tls;
12040 break;
12041#endif
12042#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
12043#if defined(MBEDTLS_SHA512_C)
12044 case MBEDTLS_SSL_HASH_SHA384:
12045 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
12046 break;
12047#endif
12048#if defined(MBEDTLS_SHA256_C)
12049 case MBEDTLS_SSL_HASH_SHA256:
12050 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
12051 break;
12052#endif
12053 default:
12054 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
12055 }
12056
12057 return 0;
12058#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
12059 (void) ssl;
12060 (void) md;
12061
12062 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
12063#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
12064}
12065
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012066#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12067 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12068int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12069 unsigned char *output,
12070 unsigned char *data, size_t data_len )
12071{
12072 int ret = 0;
12073 mbedtls_md5_context mbedtls_md5;
12074 mbedtls_sha1_context mbedtls_sha1;
12075
12076 mbedtls_md5_init( &mbedtls_md5 );
12077 mbedtls_sha1_init( &mbedtls_sha1 );
12078
12079 /*
12080 * digitally-signed struct {
12081 * opaque md5_hash[16];
12082 * opaque sha_hash[20];
12083 * };
12084 *
12085 * md5_hash
12086 * MD5(ClientHello.random + ServerHello.random
12087 * + ServerParams);
12088 * sha_hash
12089 * SHA(ClientHello.random + ServerHello.random
12090 * + ServerParams);
12091 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012092 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012093 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012095 goto exit;
12096 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012097 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012098 ssl->handshake->randbytes, 64 ) ) != 0 )
12099 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012101 goto exit;
12102 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012103 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012104 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012106 goto exit;
12107 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012108 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012109 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012111 goto exit;
12112 }
12113
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012114 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012115 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012117 goto exit;
12118 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012119 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012120 ssl->handshake->randbytes, 64 ) ) != 0 )
12121 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012123 goto exit;
12124 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012125 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012126 data_len ) ) != 0 )
12127 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012129 goto exit;
12130 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012131 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012132 output + 16 ) ) != 0 )
12133 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012135 goto exit;
12136 }
12137
12138exit:
12139 mbedtls_md5_free( &mbedtls_md5 );
12140 mbedtls_sha1_free( &mbedtls_sha1 );
12141
12142 if( ret != 0 )
12143 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
12144 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
12145
12146 return( ret );
12147
12148}
12149#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12150 MBEDTLS_SSL_PROTO_TLS1_1 */
12151
12152#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12153 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012154
12155#if defined(MBEDTLS_USE_PSA_CRYPTO)
12156int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
12157 unsigned char *hash, size_t *hashlen,
12158 unsigned char *data, size_t data_len,
12159 mbedtls_md_type_t md_alg )
12160{
Andrzej Kurek814feff2019-01-14 04:35:19 -050012161 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000012162 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012163 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
12164
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010012165 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050012166
12167 if( ( status = psa_hash_setup( &hash_operation,
12168 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012169 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050012170 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012171 goto exit;
12172 }
12173
Andrzej Kurek814feff2019-01-14 04:35:19 -050012174 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
12175 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012176 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050012177 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012178 goto exit;
12179 }
12180
Andrzej Kurek814feff2019-01-14 04:35:19 -050012181 if( ( status = psa_hash_update( &hash_operation,
12182 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012183 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050012184 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012185 goto exit;
12186 }
12187
Andrzej Kurek814feff2019-01-14 04:35:19 -050012188 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
12189 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012190 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050012191 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012192 goto exit;
12193 }
12194
12195exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050012196 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012197 {
12198 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
12199 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050012200 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012201 {
12202 case PSA_ERROR_NOT_SUPPORTED:
12203 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050012204 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012205 case PSA_ERROR_BUFFER_TOO_SMALL:
12206 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
12207 case PSA_ERROR_INSUFFICIENT_MEMORY:
12208 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
12209 default:
12210 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
12211 }
12212 }
12213 return( 0 );
12214}
12215
12216#else
12217
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012218int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012219 unsigned char *hash, size_t *hashlen,
12220 unsigned char *data, size_t data_len,
12221 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012222{
12223 int ret = 0;
12224 mbedtls_md_context_t ctx;
12225 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012226 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012227
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010012228 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050012229
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012230 mbedtls_md_init( &ctx );
12231
12232 /*
12233 * digitally-signed struct {
12234 * opaque client_random[32];
12235 * opaque server_random[32];
12236 * ServerDHParams params;
12237 * };
12238 */
12239 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12240 {
12241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12242 goto exit;
12243 }
12244 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12245 {
12246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12247 goto exit;
12248 }
12249 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12250 {
12251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12252 goto exit;
12253 }
12254 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12255 {
12256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12257 goto exit;
12258 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012259 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012260 {
12261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12262 goto exit;
12263 }
12264
12265exit:
12266 mbedtls_md_free( &ctx );
12267
12268 if( ret != 0 )
12269 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
12270 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
12271
12272 return( ret );
12273}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012274#endif /* MBEDTLS_USE_PSA_CRYPTO */
12275
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012276#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12277 MBEDTLS_SSL_PROTO_TLS1_2 */
12278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012279#endif /* MBEDTLS_SSL_TLS_C */