blob: 378137c7e03c994d348aeb99e9844df58fa6e08d [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"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Becker2a43f6f2018-08-10 11:12:52 +010057static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
58
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010059/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020063 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010064 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065#else
66 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067#endif
68 return( 0 );
69}
70
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020071/*
72 * Start a timer.
73 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020077 if( ssl->f_set_timer == NULL )
78 return;
79
80 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
81 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082}
83
84/*
85 * Return -1 is timer is expired, 0 if it isn't.
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020089 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020090 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020091
92 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020093 {
94 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020096 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020097
98 return( 0 );
99}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100101static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
102 mbedtls_ssl_transform *transform );
103static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
104 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100105
106#define SSL_DONT_FORCE_FLUSH 0
107#define SSL_FORCE_FLUSH 1
108
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100110
Hanno Beckera67dee22018-08-22 10:05:20 +0100111static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100112static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100113{
Hanno Becker11682cc2018-08-22 14:41:02 +0100114 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100115
116 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100117 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100118
119 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
120}
121
Hanno Becker67bc7c32018-08-06 11:33:50 +0100122static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
123{
Hanno Becker11682cc2018-08-22 14:41:02 +0100124 size_t const bytes_written = ssl->out_left;
125 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100126
127 /* Double-check that the write-index hasn't gone
128 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100129 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100130 {
131 /* Should never happen... */
132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
133 }
134
135 return( (int) ( mtu - bytes_written ) );
136}
137
138static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
139{
140 int ret;
141 size_t remaining, expansion;
142 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
143
144#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
145 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
146
147 if( max_len > mfl )
148 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100149
150 /* By the standard (RFC 6066 Sect. 4), the MFL extension
151 * only limits the maximum record payload size, so in theory
152 * we would be allowed to pack multiple records of payload size
153 * MFL into a single datagram. However, this would mean that there's
154 * no way to explicitly communicate MTU restrictions to the peer.
155 *
156 * The following reduction of max_len makes sure that we never
157 * write datagrams larger than MFL + Record Expansion Overhead.
158 */
159 if( max_len <= ssl->out_left )
160 return( 0 );
161
162 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100163#endif
164
165 ret = ssl_get_remaining_space_in_datagram( ssl );
166 if( ret < 0 )
167 return( ret );
168 remaining = (size_t) ret;
169
170 ret = mbedtls_ssl_get_record_expansion( ssl );
171 if( ret < 0 )
172 return( ret );
173 expansion = (size_t) ret;
174
175 if( remaining <= expansion )
176 return( 0 );
177
178 remaining -= expansion;
179 if( remaining >= max_len )
180 remaining = max_len;
181
182 return( (int) remaining );
183}
184
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200185/*
186 * Double the retransmit timeout value, within the allowed range,
187 * returning -1 if the maximum value has already been reached.
188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200190{
191 uint32_t new_timeout;
192
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200193 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200194 return( -1 );
195
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200196 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
197 * in the following way: after the initial transmission and a first
198 * retransmission, back off to a temporary estimated MTU of 508 bytes.
199 * This value is guaranteed to be deliverable (if not guaranteed to be
200 * delivered) of any compliant IPv4 (and IPv6) network, and should work
201 * on most non-IP stacks too. */
202 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
203 ssl->handshake->mtu = 508;
204
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200205 new_timeout = 2 * ssl->handshake->retransmit_timeout;
206
207 /* Avoid arithmetic overflow and range overflow */
208 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200209 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200210 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200211 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200212 }
213
214 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200216 ssl->handshake->retransmit_timeout ) );
217
218 return( 0 );
219}
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200222{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200223 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200225 ssl->handshake->retransmit_timeout ) );
226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200230/*
231 * Convert max_fragment_length codes to length.
232 * RFC 6066 says:
233 * enum{
234 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
235 * } MaxFragmentLength;
236 * and we add 0 -> extension unused
237 */
Angus Grattond8213d02016-05-25 20:56:48 +1000238static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200239{
Angus Grattond8213d02016-05-25 20:56:48 +1000240 switch( mfl )
241 {
242 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
243 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
244 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
245 return 512;
246 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
247 return 1024;
248 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
249 return 2048;
250 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
251 return 4096;
252 default:
253 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
254 }
255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200257
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200258#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_ssl_session_free( dst );
262 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200265 if( src->peer_cert != NULL )
266 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200267 int ret;
268
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200269 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200270 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200271 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200276 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200279 dst->peer_cert = NULL;
280 return( ret );
281 }
282 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200285#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200286 if( src->ticket != NULL )
287 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200288 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200289 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200290 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291
292 memcpy( dst->ticket, src->ticket, src->ticket_len );
293 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200294#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200295
296 return( 0 );
297}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200298#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
301int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200302 const unsigned char *key_enc, const unsigned char *key_dec,
303 size_t keylen,
304 const unsigned char *iv_enc, const unsigned char *iv_dec,
305 size_t ivlen,
306 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200307 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
309int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
310int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
311int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
312int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
313#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000314
Paul Bakker5121ce52009-01-03 21:22:43 +0000315/*
316 * Key material generation
317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200319static int ssl3_prf( const unsigned char *secret, size_t slen,
320 const char *label,
321 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000322 unsigned char *dstbuf, size_t dlen )
323{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100324 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000325 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200326 mbedtls_md5_context md5;
327 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000328 unsigned char padding[16];
329 unsigned char sha1sum[20];
330 ((void)label);
331
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200332 mbedtls_md5_init( &md5 );
333 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200334
Paul Bakker5f70b252012-09-13 14:23:06 +0000335 /*
336 * SSLv3:
337 * block =
338 * MD5( secret + SHA1( 'A' + secret + random ) ) +
339 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
340 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
341 * ...
342 */
343 for( i = 0; i < dlen / 16; i++ )
344 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200345 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000346
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100347 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100348 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100349 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100350 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100351 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100352 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100353 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100354 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100355 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100356 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000357
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100358 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100359 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100360 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100361 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100362 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100363 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100364 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100365 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000366 }
367
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100368exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200369 mbedtls_md5_free( &md5 );
370 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000371
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500372 mbedtls_platform_zeroize( padding, sizeof( padding ) );
373 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000374
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100375 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200380static int tls1_prf( const unsigned char *secret, size_t slen,
381 const char *label,
382 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000383 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000384{
Paul Bakker23986e52011-04-24 08:57:21 +0000385 size_t nb, hs;
386 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200387 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 unsigned char tmp[128];
389 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 const mbedtls_md_info_t *md_info;
391 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100392 int ret;
393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
399 hs = ( slen + 1 ) / 2;
400 S1 = secret;
401 S2 = secret + slen - hs;
402
403 nb = strlen( label );
404 memcpy( tmp + 20, label, nb );
405 memcpy( tmp + 20 + nb, random, rlen );
406 nb += rlen;
407
408 /*
409 * First compute P_md5(secret,label+random)[0..dlen]
410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100415 return( ret );
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
418 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
419 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
421 for( i = 0; i < dlen; i += 16 )
422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_md_hmac_reset ( &md_ctx );
424 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
425 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_md_hmac_reset ( &md_ctx );
428 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
429 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
432
433 for( j = 0; j < k; j++ )
434 dstbuf[i + j] = h_i[j];
435 }
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100438
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 /*
440 * XOR out with P_sha1(secret,label+random)[0..dlen]
441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100446 return( ret );
447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
449 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
450 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452 for( i = 0; i < dlen; i += 20 )
453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_md_hmac_reset ( &md_ctx );
455 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
456 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_md_hmac_reset ( &md_ctx );
459 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
460 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
462 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
463
464 for( j = 0; j < k; j++ )
465 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
466 }
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100469
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500470 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
471 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473 return( 0 );
474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
478static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100479 const unsigned char *secret, size_t slen,
480 const char *label,
481 const unsigned char *random, size_t rlen,
482 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000483{
484 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100485 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000486 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
488 const mbedtls_md_info_t *md_info;
489 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100490 int ret;
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100498
499 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000501
502 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100503 memcpy( tmp + md_len, label, nb );
504 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000505 nb += rlen;
506
507 /*
508 * Compute P_<hash>(secret, label + random)[0..dlen]
509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100511 return( ret );
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
514 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
515 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100516
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100517 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_md_hmac_reset ( &md_ctx );
520 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
521 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_md_hmac_reset ( &md_ctx );
524 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
525 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000526
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100527 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000528
529 for( j = 0; j < k; j++ )
530 dstbuf[i + j] = h_i[j];
531 }
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100534
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500535 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
536 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000537
538 return( 0 );
539}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100542static int tls_prf_sha256( const unsigned char *secret, size_t slen,
543 const char *label,
544 const unsigned char *random, size_t rlen,
545 unsigned char *dstbuf, size_t dlen )
546{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100548 label, random, rlen, dstbuf, dlen ) );
549}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200553static int tls_prf_sha384( const unsigned char *secret, size_t slen,
554 const char *label,
555 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000556 unsigned char *dstbuf, size_t dlen )
557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100559 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_SHA512_C */
562#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
567 defined(MBEDTLS_SSL_PROTO_TLS1_1)
568static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200569#endif
Paul Bakker380da532012-04-18 16:10:25 +0000570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#if defined(MBEDTLS_SSL_PROTO_SSL3)
572static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
573static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200574#endif
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
577static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
578static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200579#endif
580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
582#if defined(MBEDTLS_SHA256_C)
583static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
584static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
585static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200586#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#if defined(MBEDTLS_SHA512_C)
589static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
590static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
591static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000596{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200597 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 unsigned char keyblk[256];
600 unsigned char *key1;
601 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100602 unsigned char *mac_enc;
603 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000604 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200605 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 const mbedtls_cipher_info_t *cipher_info;
607 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_ssl_session *session = ssl->session_negotiate;
610 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
611 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100616 if( cipher_info == NULL )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100619 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100621 }
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100624 if( md_info == NULL )
625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100627 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100629 }
630
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000632 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_SSL_PROTO_SSL3)
635 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000636 {
Paul Bakker48916f92012-09-16 19:57:18 +0000637 handshake->tls_prf = ssl3_prf;
638 handshake->calc_verify = ssl_calc_verify_ssl;
639 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000640 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200641 else
642#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
644 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000645 {
Paul Bakker48916f92012-09-16 19:57:18 +0000646 handshake->tls_prf = tls1_prf;
647 handshake->calc_verify = ssl_calc_verify_tls;
648 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000649 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200650 else
651#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
653#if defined(MBEDTLS_SHA512_C)
654 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
655 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000656 {
Paul Bakker48916f92012-09-16 19:57:18 +0000657 handshake->tls_prf = tls_prf_sha384;
658 handshake->calc_verify = ssl_calc_verify_tls_sha384;
659 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000660 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000661 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200662#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_SHA256_C)
664 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000665 {
Paul Bakker48916f92012-09-16 19:57:18 +0000666 handshake->tls_prf = tls_prf_sha256;
667 handshake->calc_verify = ssl_calc_verify_tls_sha256;
668 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000669 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200670 else
671#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
675 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200676 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000677
678 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 * SSLv3:
680 * master =
681 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
682 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
683 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200684 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200685 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 * master = PRF( premaster, "master secret", randbytes )[0..47]
687 */
Paul Bakker0a597072012-09-25 21:55:46 +0000688 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000691 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
694 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200695 {
696 unsigned char session_hash[48];
697 size_t hash_len;
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200700
701 ssl->handshake->calc_verify( ssl, session_hash );
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
704 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200707 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200709 {
710 hash_len = 48;
711 }
712 else
713#endif
714 hash_len = 32;
715 }
716 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200718 hash_len = 36;
719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200721
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100722 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
723 "extended master secret",
724 session_hash, hash_len,
725 session->master, 48 );
726 if( ret != 0 )
727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100729 return( ret );
730 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200731
732 }
733 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200734#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100735 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
736 "master secret",
737 handshake->randbytes, 64,
738 session->master, 48 );
739 if( ret != 0 )
740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100742 return( ret );
743 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200744
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500745 mbedtls_platform_zeroize( handshake->premaster,
746 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 }
748 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
751 /*
752 * Swap the client and server random values.
753 */
Paul Bakker48916f92012-09-16 19:57:18 +0000754 memcpy( tmp, handshake->randbytes, 64 );
755 memcpy( handshake->randbytes, tmp + 32, 32 );
756 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500757 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
759 /*
760 * SSLv3:
761 * key block =
762 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
763 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
764 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
765 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
766 * ...
767 *
768 * TLSv1:
769 * key block = PRF( master, "key expansion", randbytes )
770 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100771 ret = handshake->tls_prf( session->master, 48, "key expansion",
772 handshake->randbytes, 64, keyblk, 256 );
773 if( ret != 0 )
774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100776 return( ret );
777 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
780 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
781 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
782 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
783 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500785 mbedtls_platform_zeroize( handshake->randbytes,
786 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787
788 /*
789 * Determine the appropriate key, IV and MAC length.
790 */
Paul Bakker68884e32013-01-07 18:20:04 +0100791
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200792 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200795 cipher_info->mode == MBEDTLS_MODE_CCM ||
796 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000797 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200798 size_t taglen, explicit_ivlen;
799
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200800 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000801 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200802
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200803 /* All modes haves 96-bit IVs;
804 * GCM and CCM has 4 implicit and 8 explicit bytes
805 * ChachaPoly has all 12 bytes implicit
806 */
Paul Bakker68884e32013-01-07 18:20:04 +0100807 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200808 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
809 transform->fixed_ivlen = 12;
810 else
811 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200812
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200813 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
814 taglen = transform->ciphersuite_info->flags &
815 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
816
817
818 /* Minimum length of encrypted record */
819 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
820 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100821 }
822 else
823 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200824 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
826 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200829 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100830 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200832 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000833 mac_key_len = mbedtls_md_get_size( md_info );
834 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200837 /*
838 * If HMAC is to be truncated, we shall keep the leftmost bytes,
839 * (rfc 6066 page 13 or rfc 2104 section 4),
840 * so we only need to adjust the length here.
841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000845
846#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
847 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000848 * HMAC implementation which also truncates the key
849 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000850 mac_key_len = transform->maclen;
851#endif
852 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200854
855 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100856 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000857
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200858 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200860 transform->minlen = transform->maclen;
861 else
Paul Bakker68884e32013-01-07 18:20:04 +0100862 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200863 /*
864 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100865 * 1. if EtM is in use: one block plus MAC
866 * otherwise: * first multiple of blocklen greater than maclen
867 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
870 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100871 {
872 transform->minlen = transform->maclen
873 + cipher_info->block_size;
874 }
875 else
876#endif
877 {
878 transform->minlen = transform->maclen
879 + cipher_info->block_size
880 - transform->maclen % cipher_info->block_size;
881 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
884 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
885 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200886 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100887 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
890 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
891 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200892 {
893 transform->minlen += transform->ivlen;
894 }
895 else
896#endif
897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200900 }
Paul Bakker68884e32013-01-07 18:20:04 +0100901 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000902 }
903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000905 transform->keylen, transform->minlen, transform->ivlen,
906 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
908 /*
909 * Finally setup the cipher contexts, IVs and MAC secrets.
910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200912 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000913 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000914 key1 = keyblk + mac_key_len * 2;
915 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Paul Bakker68884e32013-01-07 18:20:04 +0100917 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000918 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000920 /*
921 * This is not used in TLS v1.1.
922 */
Paul Bakker48916f92012-09-16 19:57:18 +0000923 iv_copy_len = ( transform->fixed_ivlen ) ?
924 transform->fixed_ivlen : transform->ivlen;
925 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
926 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000927 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 }
929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_SSL_CLI_C */
931#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200932 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000934 key1 = keyblk + mac_key_len * 2 + transform->keylen;
935 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Hanno Becker81c7b182017-11-09 18:39:33 +0000937 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100938 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000939
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000940 /*
941 * This is not used in TLS v1.1.
942 */
Paul Bakker48916f92012-09-16 19:57:18 +0000943 iv_copy_len = ( transform->fixed_ivlen ) ?
944 transform->fixed_ivlen : transform->ivlen;
945 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
946 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000947 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000948 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100949 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
953 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100954 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#if defined(MBEDTLS_SSL_PROTO_SSL3)
957 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100958 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000959 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100963 }
964
Hanno Becker81c7b182017-11-09 18:39:33 +0000965 memcpy( transform->mac_enc, mac_enc, mac_key_len );
966 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100967 }
968 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#endif /* MBEDTLS_SSL_PROTO_SSL3 */
970#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
971 defined(MBEDTLS_SSL_PROTO_TLS1_2)
972 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100973 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100974 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
975 For AEAD-based ciphersuites, there is nothing to do here. */
976 if( mac_key_len != 0 )
977 {
978 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
979 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
980 }
Paul Bakker68884e32013-01-07 18:20:04 +0100981 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200982 else
983#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200987 }
Paul Bakker68884e32013-01-07 18:20:04 +0100988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
990 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000991 {
992 int ret = 0;
993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100997 transform->iv_enc, transform->iv_dec,
998 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100999 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001000 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1003 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001004 }
1005 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001007
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001008#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1009 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001010 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001011 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1012 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001013 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001014 iv_copy_len );
1015 }
1016#endif
1017
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001018 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001019 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001022 return( ret );
1023 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001024
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001025 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001026 cipher_info ) ) != 0 )
1027 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001029 return( ret );
1030 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001033 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001037 return( ret );
1038 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001041 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001045 return( ret );
1046 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_CIPHER_MODE_CBC)
1049 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1052 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001055 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001056 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1059 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001062 return( ret );
1063 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001064 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001066
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001067 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001070 // Initialize compression
1071 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001073 {
Paul Bakker16770332013-10-11 09:59:44 +02001074 if( ssl->compress_buf == NULL )
1075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001077 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001078 if( ssl->compress_buf == NULL )
1079 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001081 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001082 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001083 }
1084 }
1085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001087
Paul Bakker48916f92012-09-16 19:57:18 +00001088 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1089 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001090
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001091 if( deflateInit( &transform->ctx_deflate,
1092 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001093 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1096 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001097 }
1098 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
1103 return( 0 );
1104}
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#if defined(MBEDTLS_SSL_PROTO_SSL3)
1107void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001108{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001109 mbedtls_md5_context md5;
1110 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 unsigned char pad_1[48];
1112 unsigned char pad_2[48];
1113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001116 mbedtls_md5_init( &md5 );
1117 mbedtls_sha1_init( &sha1 );
1118
1119 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1120 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Paul Bakker380da532012-04-18 16:10:25 +00001122 memset( pad_1, 0x36, 48 );
1123 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001125 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1126 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1127 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001128
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001129 mbedtls_md5_starts_ret( &md5 );
1130 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1131 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1132 mbedtls_md5_update_ret( &md5, hash, 16 );
1133 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001135 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1136 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1137 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001138
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001139 mbedtls_sha1_starts_ret( &sha1 );
1140 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1141 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1142 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1143 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001147
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001148 mbedtls_md5_free( &md5 );
1149 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001150
Paul Bakker380da532012-04-18 16:10:25 +00001151 return;
1152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1156void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001157{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001158 mbedtls_md5_context md5;
1159 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001162
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001163 mbedtls_md5_init( &md5 );
1164 mbedtls_sha1_init( &sha1 );
1165
1166 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1167 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001168
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001169 mbedtls_md5_finish_ret( &md5, hash );
1170 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001174
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001175 mbedtls_md5_free( &md5 );
1176 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001177
Paul Bakker380da532012-04-18 16:10:25 +00001178 return;
1179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1183#if defined(MBEDTLS_SHA256_C)
1184void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001185{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001186 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001187
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001188 mbedtls_sha256_init( &sha256 );
1189
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001191
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001192 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001193 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001197
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001198 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001199
Paul Bakker380da532012-04-18 16:10:25 +00001200 return;
1201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_SHA512_C)
1205void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001206{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001207 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001208
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001209 mbedtls_sha512_init( &sha512 );
1210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001212
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001213 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001214 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001219 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001220
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 return;
1222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#endif /* MBEDTLS_SHA512_C */
1224#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1227int 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 +02001228{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001229 unsigned char *p = ssl->handshake->premaster;
1230 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001231 const unsigned char *psk = ssl->conf->psk;
1232 size_t psk_len = ssl->conf->psk_len;
1233
1234 /* If the psk callback was called, use its result */
1235 if( ssl->handshake->psk != NULL )
1236 {
1237 psk = ssl->handshake->psk;
1238 psk_len = ssl->handshake->psk_len;
1239 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001240
1241 /*
1242 * PMS = struct {
1243 * opaque other_secret<0..2^16-1>;
1244 * opaque psk<0..2^16-1>;
1245 * };
1246 * with "other_secret" depending on the particular key exchange
1247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1249 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001250 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001251 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001253
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001254 *(p++) = (unsigned char)( psk_len >> 8 );
1255 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001256
1257 if( end < p || (size_t)( end - p ) < psk_len )
1258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1259
1260 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001261 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001262 }
1263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1265#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1266 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001267 {
1268 /*
1269 * other_secret already set by the ClientKeyExchange message,
1270 * and is 48 bytes long
1271 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001272 if( end - p < 2 )
1273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1274
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001275 *p++ = 0;
1276 *p++ = 48;
1277 p += 48;
1278 }
1279 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1281#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1282 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001283 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001284 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001285 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001286
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001287 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001289 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001290 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001293 return( ret );
1294 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001295 *(p++) = (unsigned char)( len >> 8 );
1296 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001297 p += len;
1298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001300 }
1301 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1303#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1304 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001305 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001306 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001307 size_t zlen;
1308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001310 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001311 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001314 return( ret );
1315 }
1316
1317 *(p++) = (unsigned char)( zlen >> 8 );
1318 *(p++) = (unsigned char)( zlen );
1319 p += zlen;
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001322 }
1323 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001328 }
1329
1330 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001331 if( end - p < 2 )
1332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001333
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001334 *(p++) = (unsigned char)( psk_len >> 8 );
1335 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001336
1337 if( end < p || (size_t)( end - p ) < psk_len )
1338 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1339
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001340 memcpy( p, psk, psk_len );
1341 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001342
1343 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1344
1345 return( 0 );
1346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001350/*
1351 * SSLv3.0 MAC functions
1352 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001353#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001354static void ssl_mac( mbedtls_md_context_t *md_ctx,
1355 const unsigned char *secret,
1356 const unsigned char *buf, size_t len,
1357 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001358 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359{
1360 unsigned char header[11];
1361 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001362 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1364 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001365
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001366 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001368 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001369 else
Paul Bakker68884e32013-01-07 18:20:04 +01001370 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
1372 memcpy( header, ctr, 8 );
1373 header[ 8] = (unsigned char) type;
1374 header[ 9] = (unsigned char)( len >> 8 );
1375 header[10] = (unsigned char)( len );
1376
Paul Bakker68884e32013-01-07 18:20:04 +01001377 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 mbedtls_md_starts( md_ctx );
1379 mbedtls_md_update( md_ctx, secret, md_size );
1380 mbedtls_md_update( md_ctx, padding, padlen );
1381 mbedtls_md_update( md_ctx, header, 11 );
1382 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001383 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Paul Bakker68884e32013-01-07 18:20:04 +01001385 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 mbedtls_md_starts( md_ctx );
1387 mbedtls_md_update( md_ctx, secret, md_size );
1388 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001389 mbedtls_md_update( md_ctx, out, md_size );
1390 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1395 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001396 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001397#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001398#endif
1399
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001400/* The function below is only used in the Lucky 13 counter-measure in
1401 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1402#if defined(SSL_SOME_MODES_USE_MAC) && \
1403 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1404 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1405 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1406/* This function makes sure every byte in the memory region is accessed
1407 * (in ascending addresses order) */
1408static void ssl_read_memory( unsigned char *p, size_t len )
1409{
1410 unsigned char acc = 0;
1411 volatile unsigned char force;
1412
1413 for( ; len != 0; p++, len-- )
1414 acc ^= *p;
1415
1416 force = acc;
1417 (void) force;
1418}
1419#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1420
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001421/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001425{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001427 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001431 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001435 }
1436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001440 ssl->out_msg, ssl->out_msglen );
1441
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001443 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001445#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 if( mode == MBEDTLS_MODE_STREAM ||
1447 ( mode == MBEDTLS_MODE_CBC
1448#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1449 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001450#endif
1451 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#if defined(MBEDTLS_SSL_PROTO_SSL3)
1454 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001455 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001456 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001457
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001458 ssl_mac( &ssl->transform_out->md_ctx_enc,
1459 ssl->transform_out->mac_enc,
1460 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001461 ssl->out_ctr, ssl->out_msgtype,
1462 mac );
1463
1464 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001465 }
1466 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1469 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1470 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001471 {
Hanno Becker992b6872017-11-09 18:57:39 +00001472 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1475 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1476 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1477 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001478 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001479 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001481
1482 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001483 }
1484 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001485#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1488 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001489 }
1490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001492 ssl->out_msg + ssl->out_msglen,
1493 ssl->transform_out->maclen );
1494
1495 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001496 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001497 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001498#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001499
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001500 /*
1501 * Encrypt
1502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1504 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001506 int ret;
1507 size_t olen = 0;
1508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 "including %d bytes of padding",
1511 ssl->out_msglen, 0 ) );
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001514 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001515 ssl->transform_out->ivlen,
1516 ssl->out_msg, ssl->out_msglen,
1517 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001520 return( ret );
1521 }
1522
1523 if( ssl->out_msglen != olen )
1524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001527 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 }
Paul Bakker68884e32013-01-07 18:20:04 +01001529 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001531#if defined(MBEDTLS_GCM_C) || \
1532 defined(MBEDTLS_CCM_C) || \
1533 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001535 mode == MBEDTLS_MODE_CCM ||
1536 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001537 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001538 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001539 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001540 unsigned char *enc_msg;
1541 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001542 unsigned char iv[12];
1543 mbedtls_ssl_transform *transform = ssl->transform_out;
1544 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001546 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001547
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001548 /*
1549 * Prepare additional authenticated data
1550 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551 memcpy( add_data, ssl->out_ctr, 8 );
1552 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001554 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001555 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1556 add_data[12] = ssl->out_msglen & 0xFF;
1557
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001558 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001559
Paul Bakker68884e32013-01-07 18:20:04 +01001560 /*
1561 * Generate IV
1562 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001563 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1564 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001565 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001566 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1567 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1568 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1569
1570 }
1571 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1572 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001573 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001574 unsigned char i;
1575
1576 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1577
1578 for( i = 0; i < 8; i++ )
1579 iv[i+4] ^= ssl->out_ctr[i];
1580 }
1581 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001582 {
1583 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001586 }
1587
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001588 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1589 iv, transform->ivlen );
1590 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1591 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001592
Paul Bakker68884e32013-01-07 18:20:04 +01001593 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001594 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001595 */
1596 enc_msg = ssl->out_msg;
1597 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001598 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001601 "including 0 bytes of padding",
1602 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001603
Paul Bakker68884e32013-01-07 18:20:04 +01001604 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001605 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001606 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001607 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1608 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001609 add_data, 13,
1610 enc_msg, enc_msglen,
1611 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001612 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001615 return( ret );
1616 }
1617
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001618 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1621 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001622 }
1623
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001624 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001625 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001628 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1631#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001632 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001635 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001636 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001637 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001638
Paul Bakker48916f92012-09-16 19:57:18 +00001639 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1640 ssl->transform_out->ivlen;
1641 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 padlen = 0;
1643
1644 for( i = 0; i <= padlen; i++ )
1645 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1646
1647 ssl->out_msglen += padlen + 1;
1648
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001649 enc_msglen = ssl->out_msglen;
1650 enc_msg = ssl->out_msg;
1651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001653 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001654 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1655 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001658 {
1659 /*
1660 * Generate IV
1661 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001662 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001663 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001664 if( ret != 0 )
1665 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001666
Paul Bakker92be97b2013-01-02 17:30:03 +01001667 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001668 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001669
1670 /*
1671 * Fix pointer positions and message length with added IV
1672 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001673 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001674 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001675 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001680 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001681 ssl->out_msglen, ssl->transform_out->ivlen,
1682 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001685 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001686 ssl->transform_out->ivlen,
1687 enc_msg, enc_msglen,
1688 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001691 return( ret );
1692 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001693
Paul Bakkercca5b812013-08-31 17:40:26 +02001694 if( enc_msglen != olen )
1695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1697 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001698 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1701 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001702 {
1703 /*
1704 * Save IV in SSL3 and TLS1
1705 */
1706 memcpy( ssl->transform_out->iv_enc,
1707 ssl->transform_out->cipher_ctx_enc.iv,
1708 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001710#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001713 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001714 {
1715 /*
1716 * MAC(MAC_write_key, seq_num +
1717 * TLSCipherText.type +
1718 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001719 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001720 * IV + // except for TLS 1.0
1721 * ENC(content + padding + padding_length));
1722 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001723 unsigned char pseudo_hdr[13];
1724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001726
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001727 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1728 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001729 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1730 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1735 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001736 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001738 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001740
1741 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001742 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001743 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001745 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001746 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001748 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1751 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001752 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001753
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001754 /* Make extra sure authentication was performed, exactly once */
1755 if( auth_done != 1 )
1756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001759 }
1760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
1763 return( 0 );
1764}
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001769 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001770#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001771 size_t padlen = 0, correct = 1;
1772#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001776 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1779 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001780 }
1781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001783
Paul Bakker48916f92012-09-16 19:57:18 +00001784 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001787 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 }
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1792 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001793 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001794 int ret;
1795 size_t olen = 0;
1796
Paul Bakker68884e32013-01-07 18:20:04 +01001797 padlen = 0;
1798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001800 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001801 ssl->transform_in->ivlen,
1802 ssl->in_msg, ssl->in_msglen,
1803 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001806 return( ret );
1807 }
1808
1809 if( ssl->in_msglen != olen )
1810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 }
Paul Bakker68884e32013-01-07 18:20:04 +01001815 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001817#if defined(MBEDTLS_GCM_C) || \
1818 defined(MBEDTLS_CCM_C) || \
1819 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001821 mode == MBEDTLS_MODE_CCM ||
1822 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001823 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001824 int ret;
1825 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001826 unsigned char *dec_msg;
1827 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001828 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001829 unsigned char iv[12];
1830 mbedtls_ssl_transform *transform = ssl->transform_in;
1831 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001833 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001834
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001835 /*
1836 * Compute and update sizes
1837 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001838 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001841 "+ taglen (%d)", ssl->in_msglen,
1842 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001844 }
1845 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1846
Paul Bakker68884e32013-01-07 18:20:04 +01001847 dec_msg = ssl->in_msg;
1848 dec_msg_result = ssl->in_msg;
1849 ssl->in_msglen = dec_msglen;
1850
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851 /*
1852 * Prepare additional authenticated data
1853 */
Paul Bakker68884e32013-01-07 18:20:04 +01001854 memcpy( add_data, ssl->in_ctr, 8 );
1855 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001857 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001858 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1859 add_data[12] = ssl->in_msglen & 0xFF;
1860
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001861 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001862
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001863 /*
1864 * Prepare IV
1865 */
1866 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1867 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001868 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001869 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1870 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001871
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001872 }
1873 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1874 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001875 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001876 unsigned char i;
1877
1878 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1879
1880 for( i = 0; i < 8; i++ )
1881 iv[i+4] ^= ssl->in_ctr[i];
1882 }
1883 else
1884 {
1885 /* Reminder if we ever add an AEAD mode with a different size */
1886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1888 }
1889
1890 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001892
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001893 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001894 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001897 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001898 add_data, 13,
1899 dec_msg, dec_msglen,
1900 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001901 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1906 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001907
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001908 return( ret );
1909 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001910 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001911
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001912 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1915 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001916 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1920#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001921 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 {
Paul Bakker45829992013-01-03 14:52:21 +01001924 /*
1925 * Decrypt and check the padding
1926 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001927 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001928 unsigned char *dec_msg;
1929 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001930 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001931 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001932 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001933
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 /*
Paul Bakker45829992013-01-03 14:52:21 +01001935 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1938 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001939 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001940#endif
Paul Bakker45829992013-01-03 14:52:21 +01001941
1942 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1943 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001946 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1947 ssl->transform_in->ivlen,
1948 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001950 }
1951
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001952 dec_msglen = ssl->in_msglen;
1953 dec_msg = ssl->in_msg;
1954 dec_msg_result = ssl->in_msg;
1955
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001956 /*
1957 * Authenticate before decrypt if enabled
1958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1960 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001961 {
Hanno Becker992b6872017-11-09 18:57:39 +00001962 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001963 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001966
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001967 dec_msglen -= ssl->transform_in->maclen;
1968 ssl->in_msglen -= ssl->transform_in->maclen;
1969
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001970 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1971 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1972 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1973 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1978 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001979 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001980 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001985 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001986 ssl->transform_in->maclen );
1987
Hanno Becker992b6872017-11-09 18:57:39 +00001988 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1989 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001994 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001995 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001996 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001998
1999 /*
2000 * Check length sanity
2001 */
2002 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002005 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002007 }
2008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002010 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002011 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002014 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002015 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002016 dec_msglen -= ssl->transform_in->ivlen;
2017 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002018
Paul Bakker48916f92012-09-16 19:57:18 +00002019 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002020 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002025 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002026 ssl->transform_in->ivlen,
2027 dec_msg, dec_msglen,
2028 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002031 return( ret );
2032 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002033
Paul Bakkercca5b812013-08-31 17:40:26 +02002034 if( dec_msglen != olen )
2035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002038 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2041 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002042 {
2043 /*
2044 * Save IV in SSL3 and TLS1
2045 */
2046 memcpy( ssl->transform_in->iv_dec,
2047 ssl->transform_in->cipher_ctx_dec.iv,
2048 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
2052 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002053
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002054 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002055 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057#if defined(MBEDTLS_SSL_DEBUG_ALL)
2058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002059 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002060#endif
Paul Bakker45829992013-01-03 14:52:21 +01002061 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002062 correct = 0;
2063 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065#if defined(MBEDTLS_SSL_PROTO_SSL3)
2066 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 {
Paul Bakker48916f92012-09-16 19:57:18 +00002068 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#if defined(MBEDTLS_SSL_DEBUG_ALL)
2071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002073 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002074#endif
Paul Bakker45829992013-01-03 14:52:21 +01002075 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 }
2077 }
2078 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2080#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2081 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2082 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 {
2084 /*
Paul Bakker45829992013-01-03 14:52:21 +01002085 * TLSv1+: always check the padding up to the first failure
2086 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002088 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002089 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002091
Paul Bakker956c9e02013-12-19 14:42:28 +01002092 /*
2093 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002094 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002095 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002096 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002097 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002098 *
2099 * In both cases we reset padding_idx to a safe value (0) to
2100 * prevent out-of-buffer reads.
2101 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002102 correct &= ( padlen <= ssl->in_msglen );
2103 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002104 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002105
2106 padding_idx *= correct;
2107
Angus Grattonb512bc12018-06-19 15:57:50 +10002108 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002109 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002110 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002111 pad_count += real_count *
2112 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2113 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002114
2115 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002118 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002120#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002121 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2125 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2128 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002129 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002130
2131 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002133 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002135 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2138 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002139 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002141#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002144#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
2146 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002147 * Authenticate if not done yet.
2148 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002150#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002151 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002152 {
Hanno Becker992b6872017-11-09 18:57:39 +00002153 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002154
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002155 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002157 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2158 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#if defined(MBEDTLS_SSL_PROTO_SSL3)
2161 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002162 {
2163 ssl_mac( &ssl->transform_in->md_ctx_dec,
2164 ssl->transform_in->mac_dec,
2165 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002166 ssl->in_ctr, ssl->in_msgtype,
2167 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002168 }
2169 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2171#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2172 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2173 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002174 {
2175 /*
2176 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002177 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002178 *
2179 * Known timing attacks:
2180 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2181 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002182 * To compensate for different timings for the MAC calculation
2183 * depending on how much padding was removed (which is determined
2184 * by padlen), process extra_run more blocks through the hash
2185 * function.
2186 *
2187 * The formula in the paper is
2188 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2189 * where L1 is the size of the header plus the decrypted message
2190 * plus CBC padding and L2 is the size of the header plus the
2191 * decrypted message. This is for an underlying hash function
2192 * with 64-byte blocks.
2193 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2194 * correctly. We round down instead of up, so -56 is the correct
2195 * value for our calculations instead of -55.
2196 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002197 * Repeat the formula rather than defining a block_size variable.
2198 * This avoids requiring division by a variable at runtime
2199 * (which would be marginally less efficient and would require
2200 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002201 */
2202 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002203
2204 /*
2205 * The next two sizes are the minimum and maximum values of
2206 * in_msglen over all padlen values.
2207 *
2208 * They're independent of padlen, since we previously did
2209 * in_msglen -= padlen.
2210 *
2211 * Note that max_len + maclen is never more than the buffer
2212 * length, as we previously did in_msglen -= maclen too.
2213 */
2214 const size_t max_len = ssl->in_msglen + padlen;
2215 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2216
Gilles Peskine20b44082018-05-29 14:06:49 +02002217 switch( ssl->transform_in->ciphersuite_info->mac )
2218 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002219#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2220 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002221 case MBEDTLS_MD_MD5:
2222 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002223 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002224 /* 8 bytes of message size, 64-byte compression blocks */
2225 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2226 ( 13 + ssl->in_msglen + 8 ) / 64;
2227 break;
2228#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002229#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002230 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002231 /* 16 bytes of message size, 128-byte compression blocks */
2232 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2233 ( 13 + ssl->in_msglen + 16 ) / 128;
2234 break;
2235#endif
2236 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2239 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002240
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002241 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2244 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2245 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2246 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002247 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002248 /* Make sure we access everything even when padlen > 0. This
2249 * makes the synchronisation requirements for just-in-time
2250 * Prime+Probe attacks much tighter and hopefully impractical. */
2251 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002252 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002253
2254 /* Call mbedtls_md_process at least once due to cache attacks
2255 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002256 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002260
2261 /* Make sure we access all the memory that could contain the MAC,
2262 * before we check it in the next code block. This makes the
2263 * synchronisation requirements for just-in-time Prime+Probe
2264 * attacks much tighter and hopefully impractical. */
2265 ssl_read_memory( ssl->in_msg + min_len,
2266 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002267 }
2268 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2270 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002274 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002276#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002277 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2278 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2279 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Hanno Becker992b6872017-11-09 18:57:39 +00002282 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2283 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_SSL_DEBUG_ALL)
2286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002287#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002288 correct = 0;
2289 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002290 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002292 /*
2293 * Finally check the correct flag
2294 */
2295 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002297 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002298#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002299
2300 /* Make extra sure authentication was performed, exactly once */
2301 if( auth_done != 1 )
2302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002306
2307 if( ssl->in_msglen == 0 )
2308 {
Angus Gratton34817922018-06-19 15:58:22 +10002309#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2310 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2311 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2312 {
2313 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2315 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2316 }
2317#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2318
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 ssl->nb_zero++;
2320
2321 /*
2322 * Three or more empty messages may be a DoS attack
2323 * (excessive CPU consumption).
2324 */
2325 if( ssl->nb_zero > 3 )
2326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 }
2331 }
2332 else
2333 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002336 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002337 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002338 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002339 }
2340 else
2341#endif
2342 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002343 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002344 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2345 if( ++ssl->in_ctr[i - 1] != 0 )
2346 break;
2347
2348 /* The loop goes to its end iff the counter is wrapping */
2349 if( i == ssl_ep_len( ssl ) )
2350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2352 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002353 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002354 }
2355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
2358 return( 0 );
2359}
2360
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002361#undef MAC_NONE
2362#undef MAC_PLAINTEXT
2363#undef MAC_CIPHERTEXT
2364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002366/*
2367 * Compression/decompression functions
2368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002370{
2371 int ret;
2372 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002373 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002374 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002375 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002378
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002379 if( len_pre == 0 )
2380 return( 0 );
2381
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382 memcpy( msg_pre, ssl->out_msg, len_pre );
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002385 ssl->out_msglen ) );
2386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 ssl->out_msg, ssl->out_msglen );
2389
Paul Bakker48916f92012-09-16 19:57:18 +00002390 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2391 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2392 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002393 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394
Paul Bakker48916f92012-09-16 19:57:18 +00002395 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002396 if( ret != Z_OK )
2397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2399 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002400 }
2401
Angus Grattond8213d02016-05-25 20:56:48 +10002402 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002403 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002406 ssl->out_msglen ) );
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002409 ssl->out_msg, ssl->out_msglen );
2410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002412
2413 return( 0 );
2414}
2415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417{
2418 int ret;
2419 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002420 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002421 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002422 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002425
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002426 if( len_pre == 0 )
2427 return( 0 );
2428
Paul Bakker2770fbd2012-07-03 13:30:23 +00002429 memcpy( msg_pre, ssl->in_msg, len_pre );
2430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002432 ssl->in_msglen ) );
2433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002435 ssl->in_msg, ssl->in_msglen );
2436
Paul Bakker48916f92012-09-16 19:57:18 +00002437 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2438 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2439 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002440 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002441 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002442
Paul Bakker48916f92012-09-16 19:57:18 +00002443 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002444 if( ret != Z_OK )
2445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2447 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002448 }
2449
Angus Grattond8213d02016-05-25 20:56:48 +10002450 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002451 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002454 ssl->in_msglen ) );
2455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002457 ssl->in_msg, ssl->in_msglen );
2458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002460
2461 return( 0 );
2462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2466static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#if defined(MBEDTLS_SSL_PROTO_DTLS)
2469static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002470{
2471 /* If renegotiation is not enforced, retransmit until we would reach max
2472 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002473 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002474 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002475 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002476 unsigned char doublings = 1;
2477
2478 while( ratio != 0 )
2479 {
2480 ++doublings;
2481 ratio >>= 1;
2482 }
2483
2484 if( ++ssl->renego_records_seen > doublings )
2485 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002487 return( 0 );
2488 }
2489 }
2490
2491 return( ssl_write_hello_request( ssl ) );
2492}
2493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002495
Paul Bakker5121ce52009-01-03 21:22:43 +00002496/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002497 * Fill the input message buffer by appending data to it.
2498 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002499 *
2500 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2501 * available (from this read and/or a previous one). Otherwise, an error code
2502 * is returned (possibly EOF or WANT_READ).
2503 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002504 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2505 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2506 * since we always read a whole datagram at once.
2507 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002508 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002509 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002512{
Paul Bakker23986e52011-04-24 08:57:21 +00002513 int ret;
2514 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002518 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002521 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002523 }
2524
Angus Grattond8213d02016-05-25 20:56:48 +10002525 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002529 }
2530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002532 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002534 uint32_t timeout;
2535
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002536 /* Just to be sure */
2537 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2538 {
2539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2540 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2542 }
2543
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002544 /*
2545 * The point is, we need to always read a full datagram at once, so we
2546 * sometimes read more then requested, and handle the additional data.
2547 * It could be the rest of the current record (while fetching the
2548 * header) and/or some other records in the same datagram.
2549 */
2550
2551 /*
2552 * Move to the next record in the already read datagram if applicable
2553 */
2554 if( ssl->next_record_offset != 0 )
2555 {
2556 if( ssl->in_left < ssl->next_record_offset )
2557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002560 }
2561
2562 ssl->in_left -= ssl->next_record_offset;
2563
2564 if( ssl->in_left != 0 )
2565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002567 ssl->next_record_offset ) );
2568 memmove( ssl->in_hdr,
2569 ssl->in_hdr + ssl->next_record_offset,
2570 ssl->in_left );
2571 }
2572
2573 ssl->next_record_offset = 0;
2574 }
2575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002578
2579 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002580 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002581 */
2582 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002585 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002586 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002587
2588 /*
2589 * A record can't be split accross datagrams. If we need to read but
2590 * are not at the beginning of a new record, the caller did something
2591 * wrong.
2592 */
2593 if( ssl->in_left != 0 )
2594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002597 }
2598
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002599 /*
2600 * Don't even try to read if time's out already.
2601 * This avoids by-passing the timer when repeatedly receiving messages
2602 * that will end up being dropped.
2603 */
2604 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002605 {
2606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002607 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002608 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002609 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002610 {
Angus Grattond8213d02016-05-25 20:56:48 +10002611 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002614 timeout = ssl->handshake->retransmit_timeout;
2615 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002616 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002619
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002620 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002621 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2622 timeout );
2623 else
2624 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002627
2628 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002630 }
2631
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002632 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002635 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002638 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002639 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002642 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002643 }
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002648 return( ret );
2649 }
2650
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002651 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002652 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002654 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002656 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002657 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002660 return( ret );
2661 }
2662
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002663 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002666 }
2667
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 if( ret < 0 )
2669 return( ret );
2670
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002671 ssl->in_left = ret;
2672 }
2673 else
2674#endif
2675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002677 ssl->in_left, nb_want ) );
2678
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002679 while( ssl->in_left < nb_want )
2680 {
2681 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002682
2683 if( ssl_check_timer( ssl ) != 0 )
2684 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2685 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002686 {
2687 if( ssl->f_recv_timeout != NULL )
2688 {
2689 ret = ssl->f_recv_timeout( ssl->p_bio,
2690 ssl->in_hdr + ssl->in_left, len,
2691 ssl->conf->read_timeout );
2692 }
2693 else
2694 {
2695 ret = ssl->f_recv( ssl->p_bio,
2696 ssl->in_hdr + ssl->in_left, len );
2697 }
2698 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002701 ssl->in_left, nb_want ) );
2702 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002703
2704 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002706
2707 if( ret < 0 )
2708 return( ret );
2709
mohammad160352aecb92018-03-28 23:41:40 -07002710 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002711 {
Darryl Green11999bb2018-03-13 15:22:58 +00002712 MBEDTLS_SSL_DEBUG_MSG( 1,
2713 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002714 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2716 }
2717
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002718 ssl->in_left += ret;
2719 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
2724 return( 0 );
2725}
2726
2727/*
2728 * Flush any data not yet written
2729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002731{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002732 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002733 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002737 if( ssl->f_send == NULL )
2738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002740 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002742 }
2743
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002744 /* Avoid incrementing counter if data is flushed */
2745 if( ssl->out_left == 0 )
2746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002748 return( 0 );
2749 }
2750
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 while( ssl->out_left > 0 )
2752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2754 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Hanno Becker2b1e3542018-08-06 11:19:13 +01002756 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002757 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
2761 if( ret <= 0 )
2762 return( ret );
2763
mohammad160352aecb92018-03-28 23:41:40 -07002764 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002765 {
Darryl Green11999bb2018-03-13 15:22:58 +00002766 MBEDTLS_SSL_DEBUG_MSG( 1,
2767 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002768 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002769 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2770 }
2771
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 ssl->out_left -= ret;
2773 }
2774
Hanno Becker2b1e3542018-08-06 11:19:13 +01002775#if defined(MBEDTLS_SSL_PROTO_DTLS)
2776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2777 {
2778 ssl->out_hdr = ssl->out_buf;
2779 }
2780 else
2781#endif
2782 {
2783 ssl->out_hdr = ssl->out_buf + 8;
2784 }
2785 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
2789 return( 0 );
2790}
2791
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002792/*
2793 * Functions to handle the DTLS retransmission state machine
2794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002796/*
2797 * Append current handshake message to current outgoing flight
2798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002800{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2803 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2804 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002805
2806 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002807 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002808 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002811 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002812 }
2813
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002814 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002815 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002818 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002819 }
2820
2821 /* Copy current handshake message with headers */
2822 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2823 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002824 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002825 msg->next = NULL;
2826
2827 /* Append to the current flight */
2828 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002829 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002830 else
2831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002833 while( cur->next != NULL )
2834 cur = cur->next;
2835 cur->next = msg;
2836 }
2837
Hanno Becker3b235902018-08-06 09:54:53 +01002838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002839 return( 0 );
2840}
2841
2842/*
2843 * Free the current flight of handshake messages
2844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 mbedtls_ssl_flight_item *cur = flight;
2848 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002849
2850 while( cur != NULL )
2851 {
2852 next = cur->next;
2853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 mbedtls_free( cur->p );
2855 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002856
2857 cur = next;
2858 }
2859}
2860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2862static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002863#endif
2864
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002865/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002866 * Swap transform_out and out_ctr with the alternative ones
2867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002869{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002871 unsigned char tmp_out_ctr[8];
2872
2873 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002876 return;
2877 }
2878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002880
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002881 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002882 tmp_transform = ssl->transform_out;
2883 ssl->transform_out = ssl->handshake->alt_transform_out;
2884 ssl->handshake->alt_transform_out = tmp_transform;
2885
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002886 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002887 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2888 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002889 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002890
2891 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002892 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2895 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2900 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002901 }
2902 }
2903#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002904}
2905
2906/*
2907 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002908 */
2909int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2910{
2911 int ret = 0;
2912
2913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2914
2915 ret = mbedtls_ssl_flight_transmit( ssl );
2916
2917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2918
2919 return( ret );
2920}
2921
2922/*
2923 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002924 *
2925 * Need to remember the current message in case flush_output returns
2926 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002927 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002928 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002929int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002930{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002931 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002935 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002937
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002938 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002939 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002940 ssl_swap_epochs( ssl );
2941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002943 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002944
2945 while( ssl->handshake->cur_msg != NULL )
2946 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002947 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002948 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002949
Hanno Beckere1dcb032018-08-17 16:47:58 +01002950 int const is_finished =
2951 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2952 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2953
Hanno Becker04da1892018-08-14 13:22:10 +01002954 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2955 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2956
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002957 /* Swap epochs before sending Finished: we can't do it after
2958 * sending ChangeCipherSpec, in case write returns WANT_READ.
2959 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002960 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002961 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002963 ssl_swap_epochs( ssl );
2964 }
2965
Hanno Becker67bc7c32018-08-06 11:33:50 +01002966 ret = ssl_get_remaining_payload_in_datagram( ssl );
2967 if( ret < 0 )
2968 return( ret );
2969 max_frag_len = (size_t) ret;
2970
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002971 /* CCS is copied as is, while HS messages may need fragmentation */
2972 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2973 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002974 if( max_frag_len == 0 )
2975 {
2976 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2977 return( ret );
2978
2979 continue;
2980 }
2981
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002982 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002983 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002984 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002985
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002986 /* Update position inside current message */
2987 ssl->handshake->cur_msg_p += cur->len;
2988 }
2989 else
2990 {
2991 const unsigned char * const p = ssl->handshake->cur_msg_p;
2992 const size_t hs_len = cur->len - 12;
2993 const size_t frag_off = p - ( cur->p + 12 );
2994 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002995 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002996
Hanno Beckere1dcb032018-08-17 16:47:58 +01002997 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002998 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002999 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003000 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003001
3002 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3003 return( ret );
3004
3005 continue;
3006 }
3007 max_hs_frag_len = max_frag_len - 12;
3008
3009 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3010 max_hs_frag_len : rem_len;
3011
3012 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003013 {
3014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003015 (unsigned) cur_hs_frag_len,
3016 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003017 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003018
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003019 /* Messages are stored with handshake headers as if not fragmented,
3020 * copy beginning of headers then fill fragmentation fields.
3021 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3022 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003023
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003024 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3025 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3026 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3027
Hanno Becker67bc7c32018-08-06 11:33:50 +01003028 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3029 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3030 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003031
3032 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3033
Hanno Becker67bc7c32018-08-06 11:33:50 +01003034 /* Copy the handshame message content and set records fields */
3035 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3036 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003037 ssl->out_msgtype = cur->type;
3038
3039 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003040 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003041 }
3042
3043 /* If done with the current message move to the next one if any */
3044 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3045 {
3046 if( cur->next != NULL )
3047 {
3048 ssl->handshake->cur_msg = cur->next;
3049 ssl->handshake->cur_msg_p = cur->next->p + 12;
3050 }
3051 else
3052 {
3053 ssl->handshake->cur_msg = NULL;
3054 ssl->handshake->cur_msg_p = NULL;
3055 }
3056 }
3057
3058 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003059 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003062 return( ret );
3063 }
3064 }
3065
Hanno Becker67bc7c32018-08-06 11:33:50 +01003066 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3067 return( ret );
3068
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003069 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3071 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003072 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003075 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3076 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003077
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003079
3080 return( 0 );
3081}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003082
3083/*
3084 * To be called when the last message of an incoming flight is received.
3085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003087{
3088 /* We won't need to resend that one any more */
3089 ssl_flight_free( ssl->handshake->flight );
3090 ssl->handshake->flight = NULL;
3091 ssl->handshake->cur_msg = NULL;
3092
3093 /* The next incoming flight will start with this msg_seq */
3094 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3095
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003096 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003097 ssl_set_timer( ssl, 0 );
3098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3100 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003103 }
3104 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003106}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003107
3108/*
3109 * To be called when the last message of an outgoing flight is send.
3110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003112{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003113 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003114 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3117 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003120 }
3121 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003123}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003125
Paul Bakker5121ce52009-01-03 21:22:43 +00003126/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003127 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003128 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003129
3130/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003131 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003132 *
3133 * - fill in handshake headers
3134 * - update handshake checksum
3135 * - DTLS: save message for resending
3136 * - then pass to the record layer
3137 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003138 * DTLS: except for HelloRequest, messages are only queued, and will only be
3139 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003140 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003141 * Inputs:
3142 * - ssl->out_msglen: 4 + actual handshake message len
3143 * (4 is the size of handshake headers for TLS)
3144 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3145 * - ssl->out_msg + 4: the handshake message body
3146 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003147 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003148 * - ssl->out_msglen: the length of the record contents
3149 * (including handshake headers but excluding record headers)
3150 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003151 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003152int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003153{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003154 int ret;
3155 const size_t hs_len = ssl->out_msglen - 4;
3156 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003157
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3159
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003160 /*
3161 * Sanity checks
3162 */
Hanno Becker2c98db22018-08-22 16:05:47 +01003163 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003164 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3165 {
Hanno Becker2c98db22018-08-22 16:05:47 +01003166 /* In SSLv3, the client might send a NoCertificate alert. */
3167#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3168 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3169 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3170 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3171#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3172 {
3173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3175 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003176 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003177
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003178 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3179 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3180 ssl->handshake == NULL )
3181 {
3182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3184 }
3185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003188 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003190 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3192 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003193 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003194#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003195
Hanno Beckerb50a2532018-08-06 11:52:54 +01003196 /* Double-check that we did not exceed the bounds
3197 * of the outgoing record buffer.
3198 * This should never fail as the various message
3199 * writing functions must obey the bounds of the
3200 * outgoing record buffer, but better be safe.
3201 *
3202 * Note: We deliberately do not check for the MTU or MFL here.
3203 */
3204 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3205 {
3206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3207 "size %u, maximum %u",
3208 (unsigned) ssl->out_msglen,
3209 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3210 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3211 }
3212
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003213 /*
3214 * Fill handshake headers
3215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003218 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3219 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3220 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003222 /*
3223 * DTLS has additional fields in the Handshake layer,
3224 * between the length field and the actual payload:
3225 * uint16 message_seq;
3226 * uint24 fragment_offset;
3227 * uint24 fragment_length;
3228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003231 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003232 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003233 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003234 {
3235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3236 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003237 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003238 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3240 }
3241
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003242 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003243 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003244
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003245 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003246 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003247 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003248 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3249 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3250 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003251 }
3252 else
3253 {
3254 ssl->out_msg[4] = 0;
3255 ssl->out_msg[5] = 0;
3256 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003257
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003258 /* Handshake hashes are computed without fragmentation,
3259 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003260 memset( ssl->out_msg + 6, 0x00, 3 );
3261 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003264
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003265 /* Update running hashes of hanshake messages seen */
3266 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3267 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 }
3269
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003270 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker551835d2018-08-22 16:07:59 +01003273 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3274 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003275 {
3276 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003279 return( ret );
3280 }
3281 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003282 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003283#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003284 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003285 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003286 {
3287 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3288 return( ret );
3289 }
3290 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003291
3292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3293
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003294 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003295}
3296
3297/*
3298 * Record layer functions
3299 */
3300
3301/*
3302 * Write current record.
3303 *
3304 * Uses:
3305 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3306 * - ssl->out_msglen: length of the record content (excl headers)
3307 * - ssl->out_msg: record content
3308 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003309int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003310{
3311 int ret, done = 0;
3312 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003313 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003314
3315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003318 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003320 {
3321 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003324 return( ret );
3325 }
3326
3327 len = ssl->out_msglen;
3328 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3332 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 ret = mbedtls_ssl_hw_record_write( ssl );
3337 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3340 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003341 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003342
3343 if( ret == 0 )
3344 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003347 if( !done )
3348 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003349 unsigned i;
3350 size_t protected_record_size;
3351
Paul Bakker05ef8352012-05-08 09:17:57 +00003352 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003354 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003355
Hanno Becker19859472018-08-06 09:40:20 +01003356 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003357 ssl->out_len[0] = (unsigned char)( len >> 8 );
3358 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003359
Paul Bakker48916f92012-09-16 19:57:18 +00003360 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003361 {
3362 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003365 return( ret );
3366 }
3367
3368 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003369 ssl->out_len[0] = (unsigned char)( len >> 8 );
3370 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003371 }
3372
Hanno Becker2b1e3542018-08-06 11:19:13 +01003373 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3374
3375#if defined(MBEDTLS_SSL_PROTO_DTLS)
3376 /* In case of DTLS, double-check that we don't exceed
3377 * the remaining space in the datagram. */
3378 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3379 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003380 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003381 if( ret < 0 )
3382 return( ret );
3383
3384 if( protected_record_size > (size_t) ret )
3385 {
3386 /* Should never happen */
3387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3388 }
3389 }
3390#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003393 "version = [%d:%d], msglen = %d",
3394 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3395
Paul Bakker05ef8352012-05-08 09:17:57 +00003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003398 ssl->out_hdr, protected_record_size );
3399
3400 ssl->out_left += protected_record_size;
3401 ssl->out_hdr += protected_record_size;
3402 ssl_update_out_pointers( ssl, ssl->transform_out );
3403
Hanno Becker04484622018-08-06 09:49:38 +01003404 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3405 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3406 break;
3407
3408 /* The loop goes to its end iff the counter is wrapping */
3409 if( i == ssl_ep_len( ssl ) )
3410 {
3411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3412 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3413 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 }
3415
Hanno Becker67bc7c32018-08-06 11:33:50 +01003416#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003417 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3418 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003419 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003420 size_t remaining;
3421 ret = ssl_get_remaining_payload_in_datagram( ssl );
3422 if( ret < 0 )
3423 {
3424 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3425 ret );
3426 return( ret );
3427 }
3428
3429 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003430 if( remaining == 0 )
3431 flush = SSL_FORCE_FLUSH;
3432 else
3433 {
Hanno Becker513815a2018-08-20 11:56:09 +01003434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003435 }
3436 }
3437#endif /* MBEDTLS_SSL_PROTO_DTLS */
3438
3439 if( ( flush == SSL_FORCE_FLUSH ) &&
3440 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 return( ret );
3444 }
3445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003447
3448 return( 0 );
3449}
3450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003452/*
3453 * Mark bits in bitmask (used for DTLS HS reassembly)
3454 */
3455static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3456{
3457 unsigned int start_bits, end_bits;
3458
3459 start_bits = 8 - ( offset % 8 );
3460 if( start_bits != 8 )
3461 {
3462 size_t first_byte_idx = offset / 8;
3463
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003464 /* Special case */
3465 if( len <= start_bits )
3466 {
3467 for( ; len != 0; len-- )
3468 mask[first_byte_idx] |= 1 << ( start_bits - len );
3469
3470 /* Avoid potential issues with offset or len becoming invalid */
3471 return;
3472 }
3473
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003474 offset += start_bits; /* Now offset % 8 == 0 */
3475 len -= start_bits;
3476
3477 for( ; start_bits != 0; start_bits-- )
3478 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3479 }
3480
3481 end_bits = len % 8;
3482 if( end_bits != 0 )
3483 {
3484 size_t last_byte_idx = ( offset + len ) / 8;
3485
3486 len -= end_bits; /* Now len % 8 == 0 */
3487
3488 for( ; end_bits != 0; end_bits-- )
3489 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3490 }
3491
3492 memset( mask + offset / 8, 0xFF, len / 8 );
3493}
3494
3495/*
3496 * Check that bitmask is full
3497 */
3498static int ssl_bitmask_check( unsigned char *mask, size_t len )
3499{
3500 size_t i;
3501
3502 for( i = 0; i < len / 8; i++ )
3503 if( mask[i] != 0xFF )
3504 return( -1 );
3505
3506 for( i = 0; i < len % 8; i++ )
3507 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3508 return( -1 );
3509
3510 return( 0 );
3511}
3512
3513/*
3514 * Reassemble fragmented DTLS handshake messages.
3515 *
3516 * Use a temporary buffer for reassembly, divided in two parts:
3517 * - the first holds the reassembled message (including handshake header),
3518 * - the second holds a bitmask indicating which parts of the message
3519 * (excluding headers) have been received so far.
3520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003522{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003523 unsigned char *msg, *bitmask;
3524 size_t frag_len, frag_off;
3525 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3526
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003527 if( ssl->handshake == NULL )
3528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3530 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003531 }
3532
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003533 /*
3534 * For first fragment, check size and allocate buffer
3535 */
3536 if( ssl->handshake->hs_msg == NULL )
3537 {
3538 size_t alloc_len;
3539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003541 msg_len ) );
3542
Angus Grattond8213d02016-05-25 20:56:48 +10003543 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3546 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003547 }
3548
3549 /* The bitmask needs one bit per byte of message excluding header */
3550 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3551
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003552 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003553 if( ssl->handshake->hs_msg == NULL )
3554 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003556 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003557 }
3558
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003559 /* Prepare final header: copy msg_type, length and message_seq,
3560 * then add standardised fragment_offset and fragment_length */
3561 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3562 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3563 memcpy( ssl->handshake->hs_msg + 9,
3564 ssl->handshake->hs_msg + 1, 3 );
3565 }
3566 else
3567 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003568 /* Make sure msg_type and length are consistent */
3569 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3572 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003573 }
3574 }
3575
3576 msg = ssl->handshake->hs_msg + 12;
3577 bitmask = msg + msg_len;
3578
3579 /*
3580 * Check and copy current fragment
3581 */
3582 frag_off = ( ssl->in_msg[6] << 16 ) |
3583 ( ssl->in_msg[7] << 8 ) |
3584 ssl->in_msg[8];
3585 frag_len = ( ssl->in_msg[9] << 16 ) |
3586 ( ssl->in_msg[10] << 8 ) |
3587 ssl->in_msg[11];
3588
3589 if( frag_off + frag_len > msg_len )
3590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003592 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003594 }
3595
3596 if( frag_len + 12 > ssl->in_msglen )
3597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003599 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003601 }
3602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003604 frag_off, frag_len ) );
3605
3606 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3607 ssl_bitmask_set( bitmask, frag_off, frag_len );
3608
3609 /*
3610 * Do we have the complete message by now?
3611 * If yes, finalize it, else ask to read the next record.
3612 */
3613 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003616 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003617 }
3618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003620
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003621 if( frag_len + 12 < ssl->in_msglen )
3622 {
3623 /*
3624 * We'got more handshake messages in the same record.
3625 * This case is not handled now because no know implementation does
3626 * that and it's hard to test, so we prefer to fail cleanly for now.
3627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3629 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003630 }
3631
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003632 if( ssl->in_left > ssl->next_record_offset )
3633 {
3634 /*
3635 * We've got more data in the buffer after the current record,
3636 * that we don't want to overwrite. Move it before writing the
3637 * reassembled message, and adjust in_left and next_record_offset.
3638 */
3639 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3640 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3641 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3642
3643 /* First compute and check new lengths */
3644 ssl->next_record_offset = new_remain - ssl->in_hdr;
3645 ssl->in_left = ssl->next_record_offset + remain_len;
3646
Angus Grattond8213d02016-05-25 20:56:48 +10003647 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003648 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3651 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003652 }
3653
3654 memmove( new_remain, cur_remain, remain_len );
3655 }
3656
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003657 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003660 ssl->handshake->hs_msg = NULL;
3661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003663 ssl->in_msg, ssl->in_hslen );
3664
3665 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003668
Simon Butcher99000142016-10-13 17:21:01 +01003669int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003670{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003674 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003676 }
3677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003679 ( ssl->in_msg[1] << 16 ) |
3680 ( ssl->in_msg[2] << 8 ) |
3681 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003684 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003685 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003688 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003689 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003690 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003691 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003692
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003693 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003694 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3695 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3696 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3697 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003698 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003699 /* Retransmit only on last message from previous flight, to avoid
3700 * too many retransmissions.
3701 * Besides, No sane server ever retransmits HelloVerifyRequest */
3702 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003706 "message_seq = %d, start_of_flight = %d",
3707 recv_msg_seq,
3708 ssl->handshake->in_flight_start_seq ) );
3709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003713 return( ret );
3714 }
3715 }
3716 else
3717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003719 "message_seq = %d, expected = %d",
3720 recv_msg_seq,
3721 ssl->handshake->in_msg_seq ) );
3722 }
3723
Hanno Becker90333da2017-10-10 11:27:13 +01003724 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003725 }
3726 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003727
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003728 /* Reassemble if current message is fragmented or reassembly is
3729 * already in progress */
3730 if( ssl->in_msglen < ssl->in_hslen ||
3731 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3732 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3733 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003736
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003737 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003740 return( ret );
3741 }
3742 }
3743 }
3744 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003746 /* With TLS we don't handle fragmentation (for now) */
3747 if( ssl->in_msglen < ssl->in_hslen )
3748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3750 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003751 }
3752
Simon Butcher99000142016-10-13 17:21:01 +01003753 return( 0 );
3754}
3755
3756void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3757{
3758
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003759 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3760 ssl->handshake != NULL )
3761 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003762 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003763 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003764
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003765 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003767 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003768 ssl->handshake != NULL )
3769 {
3770 ssl->handshake->in_msg_seq++;
3771 }
3772#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003773}
3774
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003775/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003776 * DTLS anti-replay: RFC 6347 4.1.2.6
3777 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003778 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3779 * Bit n is set iff record number in_window_top - n has been seen.
3780 *
3781 * Usually, in_window_top is the last record number seen and the lsb of
3782 * in_window is set. The only exception is the initial state (record number 0
3783 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3786static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003787{
3788 ssl->in_window_top = 0;
3789 ssl->in_window = 0;
3790}
3791
3792static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3793{
3794 return( ( (uint64_t) buf[0] << 40 ) |
3795 ( (uint64_t) buf[1] << 32 ) |
3796 ( (uint64_t) buf[2] << 24 ) |
3797 ( (uint64_t) buf[3] << 16 ) |
3798 ( (uint64_t) buf[4] << 8 ) |
3799 ( (uint64_t) buf[5] ) );
3800}
3801
3802/*
3803 * Return 0 if sequence number is acceptable, -1 otherwise
3804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003806{
3807 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3808 uint64_t bit;
3809
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003810 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003811 return( 0 );
3812
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003813 if( rec_seqnum > ssl->in_window_top )
3814 return( 0 );
3815
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003816 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003817
3818 if( bit >= 64 )
3819 return( -1 );
3820
3821 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3822 return( -1 );
3823
3824 return( 0 );
3825}
3826
3827/*
3828 * Update replay window on new validated record
3829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003831{
3832 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3833
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003834 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003835 return;
3836
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003837 if( rec_seqnum > ssl->in_window_top )
3838 {
3839 /* Update window_top and the contents of the window */
3840 uint64_t shift = rec_seqnum - ssl->in_window_top;
3841
3842 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003843 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003844 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003845 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003846 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003847 ssl->in_window |= 1;
3848 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003849
3850 ssl->in_window_top = rec_seqnum;
3851 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003852 else
3853 {
3854 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003855 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003856
3857 if( bit < 64 ) /* Always true, but be extra sure */
3858 ssl->in_window |= (uint64_t) 1 << bit;
3859 }
3860}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003862
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003863#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003864/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003865static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3866
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003867/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003868 * Without any SSL context, check if a datagram looks like a ClientHello with
3869 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003870 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003871 *
3872 * - if cookie is valid, return 0
3873 * - if ClientHello looks superficially valid but cookie is not,
3874 * fill obuf and set olen, then
3875 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3876 * - otherwise return a specific error code
3877 */
3878static int ssl_check_dtls_clihlo_cookie(
3879 mbedtls_ssl_cookie_write_t *f_cookie_write,
3880 mbedtls_ssl_cookie_check_t *f_cookie_check,
3881 void *p_cookie,
3882 const unsigned char *cli_id, size_t cli_id_len,
3883 const unsigned char *in, size_t in_len,
3884 unsigned char *obuf, size_t buf_len, size_t *olen )
3885{
3886 size_t sid_len, cookie_len;
3887 unsigned char *p;
3888
3889 if( f_cookie_write == NULL || f_cookie_check == NULL )
3890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3891
3892 /*
3893 * Structure of ClientHello with record and handshake headers,
3894 * and expected values. We don't need to check a lot, more checks will be
3895 * done when actually parsing the ClientHello - skipping those checks
3896 * avoids code duplication and does not make cookie forging any easier.
3897 *
3898 * 0-0 ContentType type; copied, must be handshake
3899 * 1-2 ProtocolVersion version; copied
3900 * 3-4 uint16 epoch; copied, must be 0
3901 * 5-10 uint48 sequence_number; copied
3902 * 11-12 uint16 length; (ignored)
3903 *
3904 * 13-13 HandshakeType msg_type; (ignored)
3905 * 14-16 uint24 length; (ignored)
3906 * 17-18 uint16 message_seq; copied
3907 * 19-21 uint24 fragment_offset; copied, must be 0
3908 * 22-24 uint24 fragment_length; (ignored)
3909 *
3910 * 25-26 ProtocolVersion client_version; (ignored)
3911 * 27-58 Random random; (ignored)
3912 * 59-xx SessionID session_id; 1 byte len + sid_len content
3913 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3914 * ...
3915 *
3916 * Minimum length is 61 bytes.
3917 */
3918 if( in_len < 61 ||
3919 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3920 in[3] != 0 || in[4] != 0 ||
3921 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3922 {
3923 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3924 }
3925
3926 sid_len = in[59];
3927 if( sid_len > in_len - 61 )
3928 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3929
3930 cookie_len = in[60 + sid_len];
3931 if( cookie_len > in_len - 60 )
3932 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3933
3934 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3935 cli_id, cli_id_len ) == 0 )
3936 {
3937 /* Valid cookie */
3938 return( 0 );
3939 }
3940
3941 /*
3942 * If we get here, we've got an invalid cookie, let's prepare HVR.
3943 *
3944 * 0-0 ContentType type; copied
3945 * 1-2 ProtocolVersion version; copied
3946 * 3-4 uint16 epoch; copied
3947 * 5-10 uint48 sequence_number; copied
3948 * 11-12 uint16 length; olen - 13
3949 *
3950 * 13-13 HandshakeType msg_type; hello_verify_request
3951 * 14-16 uint24 length; olen - 25
3952 * 17-18 uint16 message_seq; copied
3953 * 19-21 uint24 fragment_offset; copied
3954 * 22-24 uint24 fragment_length; olen - 25
3955 *
3956 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3957 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3958 *
3959 * Minimum length is 28.
3960 */
3961 if( buf_len < 28 )
3962 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3963
3964 /* Copy most fields and adapt others */
3965 memcpy( obuf, in, 25 );
3966 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3967 obuf[25] = 0xfe;
3968 obuf[26] = 0xff;
3969
3970 /* Generate and write actual cookie */
3971 p = obuf + 28;
3972 if( f_cookie_write( p_cookie,
3973 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3974 {
3975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3976 }
3977
3978 *olen = p - obuf;
3979
3980 /* Go back and fill length fields */
3981 obuf[27] = (unsigned char)( *olen - 28 );
3982
3983 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3984 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3985 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3986
3987 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3988 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3989
3990 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3991}
3992
3993/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003994 * Handle possible client reconnect with the same UDP quadruplet
3995 * (RFC 6347 Section 4.2.8).
3996 *
3997 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3998 * that looks like a ClientHello.
3999 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004000 * - if the input looks like a ClientHello without cookies,
4001 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004002 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004003 * - if the input looks like a ClientHello with a valid cookie,
4004 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004005 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004006 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004007 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004008 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004009 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4010 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004011 */
4012static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4013{
4014 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004015 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004016
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004017 ret = ssl_check_dtls_clihlo_cookie(
4018 ssl->conf->f_cookie_write,
4019 ssl->conf->f_cookie_check,
4020 ssl->conf->p_cookie,
4021 ssl->cli_id, ssl->cli_id_len,
4022 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004023 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004024
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004025 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4026
4027 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004028 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004029 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004030 * If the error is permanent we'll catch it later,
4031 * if it's not, then hopefully it'll work next time. */
4032 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4033
4034 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004035 }
4036
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004037 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004038 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004039 /* Got a valid cookie, partially reset context */
4040 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4041 {
4042 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4043 return( ret );
4044 }
4045
4046 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004047 }
4048
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004049 return( ret );
4050}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004051#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004052
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004053/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004054 * ContentType type;
4055 * ProtocolVersion version;
4056 * uint16 epoch; // DTLS only
4057 * uint48 sequence_number; // DTLS only
4058 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004059 *
4060 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004061 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004062 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4063 *
4064 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004065 * 1. proceed with the record if this function returns 0
4066 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4067 * 3. return CLIENT_RECONNECT if this function return that value
4068 * 4. drop the whole datagram if this function returns anything else.
4069 * Point 2 is needed when the peer is resending, and we have already received
4070 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004073{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004074 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004077
Paul Bakker5121ce52009-01-03 21:22:43 +00004078 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004079 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004080 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004083 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004084 ssl->in_msgtype,
4085 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004086
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004087 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4089 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4090 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4091 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004094
4095#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004096 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4097 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004098 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4099#endif /* MBEDTLS_SSL_PROTO_DTLS */
4100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4101 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004104 }
4105
4106 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004107 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4110 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004111 }
4112
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004113 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4116 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004117 }
4118
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004119 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004120 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004121 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4124 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004125 }
4126
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004127 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004128 * DTLS-related tests.
4129 * Check epoch before checking length constraint because
4130 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4131 * message gets duplicated before the corresponding Finished message,
4132 * the second ChangeCipherSpec should be discarded because it belongs
4133 * to an old epoch, but not because its length is shorter than
4134 * the minimum record length for packets using the new record transform.
4135 * Note that these two kinds of failures are handled differently,
4136 * as an unexpected record is silently skipped but an invalid
4137 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004138 */
4139#if defined(MBEDTLS_SSL_PROTO_DTLS)
4140 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4141 {
4142 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4143
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004144 /* Check epoch (and sequence number) with DTLS */
4145 if( rec_epoch != ssl->in_epoch )
4146 {
4147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4148 "expected %d, received %d",
4149 ssl->in_epoch, rec_epoch ) );
4150
4151#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4152 /*
4153 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4154 * access the first byte of record content (handshake type), as we
4155 * have an active transform (possibly iv_len != 0), so use the
4156 * fact that the record header len is 13 instead.
4157 */
4158 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4159 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4160 rec_epoch == 0 &&
4161 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4162 ssl->in_left > 13 &&
4163 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4164 {
4165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4166 "from the same port" ) );
4167 return( ssl_handle_possible_reconnect( ssl ) );
4168 }
4169 else
4170#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
4171 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4172 }
4173
4174#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4175 /* Replay detection only works for the current epoch */
4176 if( rec_epoch == ssl->in_epoch &&
4177 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4178 {
4179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4180 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4181 }
4182#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004183
4184 /* Drop unexpected ChangeCipherSpec messages */
4185 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4186 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4187 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4188 {
4189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4190 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4191 }
4192
4193 /* Drop unexpected ApplicationData records,
4194 * except at the beginning of renegotiations */
4195 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4196 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4197#if defined(MBEDTLS_SSL_RENEGOTIATION)
4198 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4199 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4200#endif
4201 )
4202 {
4203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4204 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4205 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004206 }
4207#endif /* MBEDTLS_SSL_PROTO_DTLS */
4208
Hanno Becker52c6dc62017-05-26 16:07:36 +01004209
4210 /* Check length against bounds of the current transform and version */
4211 if( ssl->transform_in == NULL )
4212 {
4213 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004214 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004215 {
4216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4217 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4218 }
4219 }
4220 else
4221 {
4222 if( ssl->in_msglen < ssl->transform_in->minlen )
4223 {
4224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4225 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4226 }
4227
4228#if defined(MBEDTLS_SSL_PROTO_SSL3)
4229 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004230 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004231 {
4232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4233 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4234 }
4235#endif
4236#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4237 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4238 /*
4239 * TLS encrypted messages can have up to 256 bytes of padding
4240 */
4241 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4242 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004243 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004244 {
4245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4246 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4247 }
4248#endif
4249 }
4250
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004251 return( 0 );
4252}
Paul Bakker5121ce52009-01-03 21:22:43 +00004253
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004254/*
4255 * If applicable, decrypt (and decompress) record content
4256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004258{
4259 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4262 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4265 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269 ret = mbedtls_ssl_hw_record_read( ssl );
4270 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4273 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004274 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004275
4276 if( ret == 0 )
4277 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004278 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004280 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004281 {
4282 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004285 return( ret );
4286 }
4287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004289 ssl->in_msg, ssl->in_msglen );
4290
Angus Grattond8213d02016-05-25 20:56:48 +10004291 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4294 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004295 }
4296 }
4297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004299 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004301 {
4302 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004305 return( ret );
4306 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004314 }
4315#endif
4316
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004317 return( 0 );
4318}
4319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004321
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004322/*
4323 * Read a record.
4324 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004325 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4326 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4327 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004330{
4331 int ret;
4332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004334
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004335 if( ssl->keep_current_message == 0 )
4336 {
4337 do {
Simon Butcher99000142016-10-13 17:21:01 +01004338
Hanno Becker90333da2017-10-10 11:27:13 +01004339 do ret = mbedtls_ssl_read_record_layer( ssl );
4340 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4341
4342 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004343 {
4344 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4345 return( ret );
4346 }
4347
4348 ret = mbedtls_ssl_handle_message_type( ssl );
4349
Hanno Becker90333da2017-10-10 11:27:13 +01004350 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4351 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004352
4353 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004354 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004355 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004356 return( ret );
4357 }
4358
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004359 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4360 {
4361 mbedtls_ssl_update_handshake_status( ssl );
4362 }
Simon Butcher99000142016-10-13 17:21:01 +01004363 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004364 else
Simon Butcher99000142016-10-13 17:21:01 +01004365 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4367 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004368 }
4369
4370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4371
4372 return( 0 );
4373}
4374
4375int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4376{
4377 int ret;
4378
Hanno Becker4a810fb2017-05-24 16:27:30 +01004379 /*
4380 * Step A
4381 *
4382 * Consume last content-layer message and potentially
4383 * update in_msglen which keeps track of the contents'
4384 * consumption state.
4385 *
4386 * (1) Handshake messages:
4387 * Remove last handshake message, move content
4388 * and adapt in_msglen.
4389 *
4390 * (2) Alert messages:
4391 * Consume whole record content, in_msglen = 0.
4392 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004393 * (3) Change cipher spec:
4394 * Consume whole record content, in_msglen = 0.
4395 *
4396 * (4) Application data:
4397 * Don't do anything - the record layer provides
4398 * the application data as a stream transport
4399 * and consumes through mbedtls_ssl_read only.
4400 *
4401 */
4402
4403 /* Case (1): Handshake messages */
4404 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004405 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004406 /* Hard assertion to be sure that no application data
4407 * is in flight, as corrupting ssl->in_msglen during
4408 * ssl->in_offt != NULL is fatal. */
4409 if( ssl->in_offt != NULL )
4410 {
4411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4413 }
4414
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004415 /*
4416 * Get next Handshake message in the current record
4417 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004418
Hanno Becker4a810fb2017-05-24 16:27:30 +01004419 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004420 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004421 * current handshake content: If DTLS handshake
4422 * fragmentation is used, that's the fragment
4423 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004424 * size here is faulty and should be changed at
4425 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004426 * (2) While it doesn't seem to cause problems, one
4427 * has to be very careful not to assume that in_hslen
4428 * is always <= in_msglen in a sensible communication.
4429 * Again, it's wrong for DTLS handshake fragmentation.
4430 * The following check is therefore mandatory, and
4431 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004432 * Additionally, ssl->in_hslen might be arbitrarily out of
4433 * bounds after handling a DTLS message with an unexpected
4434 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004435 */
4436 if( ssl->in_hslen < ssl->in_msglen )
4437 {
4438 ssl->in_msglen -= ssl->in_hslen;
4439 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4440 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004441
Hanno Becker4a810fb2017-05-24 16:27:30 +01004442 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4443 ssl->in_msg, ssl->in_msglen );
4444 }
4445 else
4446 {
4447 ssl->in_msglen = 0;
4448 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004449
Hanno Becker4a810fb2017-05-24 16:27:30 +01004450 ssl->in_hslen = 0;
4451 }
4452 /* Case (4): Application data */
4453 else if( ssl->in_offt != NULL )
4454 {
4455 return( 0 );
4456 }
4457 /* Everything else (CCS & Alerts) */
4458 else
4459 {
4460 ssl->in_msglen = 0;
4461 }
4462
4463 /*
4464 * Step B
4465 *
4466 * Fetch and decode new record if current one is fully consumed.
4467 *
4468 */
4469
4470 if( ssl->in_msglen > 0 )
4471 {
4472 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004473 return( 0 );
4474 }
4475
Hanno Becker4a810fb2017-05-24 16:27:30 +01004476 /* Current record either fully processed or to be discarded. */
4477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004481 return( ret );
4482 }
4483
4484 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004487 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4488 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004489 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004490 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4491 {
4492 /* Skip unexpected record (but not whole datagram) */
4493 ssl->next_record_offset = ssl->in_msglen
4494 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004495
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4497 "(header)" ) );
4498 }
4499 else
4500 {
4501 /* Skip invalid record and the rest of the datagram */
4502 ssl->next_record_offset = 0;
4503 ssl->in_left = 0;
4504
4505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4506 "(header)" ) );
4507 }
4508
4509 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004510 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004511 }
4512#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004513 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004514 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004515
4516 /*
4517 * Read and optionally decrypt the message contents
4518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4520 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004523 return( ret );
4524 }
4525
4526 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004528 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004531 if( ssl->next_record_offset < ssl->in_left )
4532 {
4533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4534 }
4535 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004536 else
4537#endif
4538 ssl->in_left = 0;
4539
4540 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004543 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004544 {
4545 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4547 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004548 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004549 /* Except when waiting for Finished as a bad mac here
4550 * probably means something went wrong in the handshake
4551 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4552 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4553 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4554 {
4555#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4556 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4557 {
4558 mbedtls_ssl_send_alert_message( ssl,
4559 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4560 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4561 }
4562#endif
4563 return( ret );
4564 }
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004567 if( ssl->conf->badmac_limit != 0 &&
4568 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4571 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004572 }
4573#endif
4574
Hanno Becker4a810fb2017-05-24 16:27:30 +01004575 /* As above, invalid records cause
4576 * dismissal of the whole datagram. */
4577
4578 ssl->next_record_offset = 0;
4579 ssl->in_left = 0;
4580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004582 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004583 }
4584
4585 return( ret );
4586 }
4587 else
4588#endif
4589 {
4590 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4592 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594 mbedtls_ssl_send_alert_message( ssl,
4595 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4596 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004597 }
4598#endif
4599 return( ret );
4600 }
4601 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004602
Simon Butcher99000142016-10-13 17:21:01 +01004603 return( 0 );
4604}
4605
4606int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4607{
4608 int ret;
4609
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004610 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004611 * Handle particular types of records
4612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004614 {
Simon Butcher99000142016-10-13 17:21:01 +01004615 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4616 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004617 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004618 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004619 }
4620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004623 if( ssl->in_msglen != 2 )
4624 {
4625 /* Note: Standard allows for more than one 2 byte alert
4626 to be packed in a single message, but Mbed TLS doesn't
4627 currently support this. */
4628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4629 ssl->in_msglen ) );
4630 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4631 }
4632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004634 ssl->in_msg[0], ssl->in_msg[1] ) );
4635
4636 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004637 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004642 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004644 }
4645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4647 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4650 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004652
4653#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4654 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4655 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4656 {
Hanno Becker90333da2017-10-10 11:27:13 +01004657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004658 /* Will be handled when trying to parse ServerHello */
4659 return( 0 );
4660 }
4661#endif
4662
4663#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4664 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4665 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4666 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4667 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4668 {
4669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4670 /* Will be handled in mbedtls_ssl_parse_certificate() */
4671 return( 0 );
4672 }
4673#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4674
4675 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004676 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 }
4678
Hanno Beckerc76c6192017-06-06 10:03:17 +01004679#if defined(MBEDTLS_SSL_PROTO_DTLS)
4680 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4681 ssl->handshake != NULL &&
4682 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4683 {
4684 ssl_handshake_wrapup_free_hs_transform( ssl );
4685 }
4686#endif
4687
Paul Bakker5121ce52009-01-03 21:22:43 +00004688 return( 0 );
4689}
4690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004691int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004692{
4693 int ret;
4694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004695 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4696 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4697 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004698 {
4699 return( ret );
4700 }
4701
4702 return( 0 );
4703}
4704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004706 unsigned char level,
4707 unsigned char message )
4708{
4709 int ret;
4710
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004711 if( ssl == NULL || ssl->conf == NULL )
4712 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004718 ssl->out_msglen = 2;
4719 ssl->out_msg[0] = level;
4720 ssl->out_msg[1] = message;
4721
Hanno Becker67bc7c32018-08-06 11:33:50 +01004722 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004725 return( ret );
4726 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004728
4729 return( 0 );
4730}
4731
Paul Bakker5121ce52009-01-03 21:22:43 +00004732/*
4733 * Handshake functions
4734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4736 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4737 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4738 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4739 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4740 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4741 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004742/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4750 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004751 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4752 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004755 ssl->state++;
4756 return( 0 );
4757 }
4758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4760 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004761}
4762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004769 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4770 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004771 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4772 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004775 ssl->state++;
4776 return( 0 );
4777 }
4778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004781}
Gilles Peskinef9828522017-05-03 12:28:43 +02004782
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004783#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004784/* Some certificate support -> implement write and parse */
4785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004789 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 const mbedtls_x509_crt *crt;
4791 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4796 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004797 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4798 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004801 ssl->state++;
4802 return( 0 );
4803 }
4804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004806 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004807 {
4808 if( ssl->client_auth == 0 )
4809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004811 ssl->state++;
4812 return( 0 );
4813 }
4814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004816 /*
4817 * If using SSLv3 and got no cert, send an Alert message
4818 * (otherwise an empty Certificate message will be sent).
4819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4821 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004822 {
4823 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4825 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4826 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 goto write_msg;
4830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004832 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833#endif /* MBEDTLS_SSL_CLI_C */
4834#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004835 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4840 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004841 }
4842 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004843#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004846
4847 /*
4848 * 0 . 0 handshake type
4849 * 1 . 3 handshake length
4850 * 4 . 6 length of all certs
4851 * 7 . 9 length of cert. 1
4852 * 10 . n-1 peer certificate
4853 * n . n+2 length of cert. 2
4854 * n+3 . ... upper level cert, etc.
4855 */
4856 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004858
Paul Bakker29087132010-03-21 21:03:34 +00004859 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004860 {
4861 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004862 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004865 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004867 }
4868
4869 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4870 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4871 ssl->out_msg[i + 2] = (unsigned char)( n );
4872
4873 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4874 i += n; crt = crt->next;
4875 }
4876
4877 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4878 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4879 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4880
4881 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4883 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004884
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004885#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004886write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004887#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004888
4889 ssl->state++;
4890
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004891 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004894 return( ret );
4895 }
4896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004898
Paul Bakkered27a042013-04-18 22:46:23 +02004899 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004900}
4901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004905 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004907 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004908 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4913 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004914 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4915 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004918 ssl->state++;
4919 return( 0 );
4920 }
4921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004923 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004924 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4925 {
4926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4927 ssl->state++;
4928 return( 0 );
4929 }
4930
4931#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4932 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4933 authmode = ssl->handshake->sni_authmode;
4934#endif
4935
4936 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4937 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004938 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004939 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004941 ssl->state++;
4942 return( 0 );
4943 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004944#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004947 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004948 /* mbedtls_ssl_read_record may have sent an alert already. We
4949 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004951 return( ret );
4952 }
4953
4954 ssl->state++;
4955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004956#if defined(MBEDTLS_SSL_SRV_C)
4957#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004958 /*
4959 * Check if the client sent an empty certificate
4960 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004961 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004963 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004964 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4966 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4967 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004970
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004971 /* The client was asked for a certificate but didn't send
4972 one. The client should know what's going on, so we
4973 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004974 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004975 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004976 return( 0 );
4977 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004979 }
4980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4984 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004985 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4989 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4990 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4991 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004994
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004995 /* The client was asked for a certificate but didn't send
4996 one. The client should know what's going on, so we
4997 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004998 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004999 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005000 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005001 else
5002 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005003 }
5004 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5006 MBEDTLS_SSL_PROTO_TLS1_2 */
5007#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005009 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005012 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5013 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005015 }
5016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5018 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005021 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5022 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005024 }
5025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005027
Paul Bakker5121ce52009-01-03 21:22:43 +00005028 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005029 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005030 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005031 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005032
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005033 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005034 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005037 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5038 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005040 }
5041
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005042 /* In case we tried to reuse a session but it failed */
5043 if( ssl->session_negotiate->peer_cert != NULL )
5044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005045 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5046 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005047 }
5048
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005049 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005051 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5055 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005056 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005057 }
5058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005060
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005061 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005062
5063 while( i < ssl->in_hslen )
5064 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005065 if ( i + 3 > ssl->in_hslen ) {
5066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5067 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5068 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5069 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5070 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 if( ssl->in_msg[i] != 0 )
5072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005074 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5075 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005077 }
5078
5079 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5080 | (unsigned int) ssl->in_msg[i + 2];
5081 i += 3;
5082
5083 if( n < 128 || i + n > ssl->in_hslen )
5084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005086 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5087 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005089 }
5090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005092 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005093 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005094 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005095 case 0: /*ok*/
5096 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5097 /* Ignore certificate with an unknown algorithm: maybe a
5098 prior certificate was already trusted. */
5099 break;
5100
5101 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5102 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5103 goto crt_parse_der_failed;
5104
5105 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5106 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5107 goto crt_parse_der_failed;
5108
5109 default:
5110 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5111 crt_parse_der_failed:
5112 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005114 return( ret );
5115 }
5116
5117 i += n;
5118 }
5119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005121
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005122 /*
5123 * On client, make sure the server cert doesn't change during renego to
5124 * avoid "triple handshake" attack: https://secure-resumption.com/
5125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005127 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005129 {
5130 if( ssl->session->peer_cert == NULL )
5131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005133 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5134 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005136 }
5137
5138 if( ssl->session->peer_cert->raw.len !=
5139 ssl->session_negotiate->peer_cert->raw.len ||
5140 memcmp( ssl->session->peer_cert->raw.p,
5141 ssl->session_negotiate->peer_cert->raw.p,
5142 ssl->session->peer_cert->raw.len ) != 0 )
5143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005145 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5146 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005148 }
5149 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005151
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005152 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005153 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005154 mbedtls_x509_crt *ca_chain;
5155 mbedtls_x509_crl *ca_crl;
5156
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005157#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005158 if( ssl->handshake->sni_ca_chain != NULL )
5159 {
5160 ca_chain = ssl->handshake->sni_ca_chain;
5161 ca_crl = ssl->handshake->sni_ca_crl;
5162 }
5163 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005164#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005165 {
5166 ca_chain = ssl->conf->ca_chain;
5167 ca_crl = ssl->conf->ca_crl;
5168 }
5169
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005170 /*
5171 * Main check: verify certificate
5172 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005173 ret = mbedtls_x509_crt_verify_with_profile(
5174 ssl->session_negotiate->peer_cert,
5175 ca_chain, ca_crl,
5176 ssl->conf->cert_profile,
5177 ssl->hostname,
5178 &ssl->session_negotiate->verify_result,
5179 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005180
5181 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005183 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005184 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005185
5186 /*
5187 * Secondary checks: always done, but change 'ret' only if it was 0
5188 */
5189
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005190#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005193
5194 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005196 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005197 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005198 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005201 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005203 }
5204 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005205#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005208 ciphersuite_info,
5209 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005210 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005213 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005215 }
5216
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005217 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5218 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5219 * with details encoded in the verification flags. All other kinds
5220 * of error codes, including those from the user provided f_vrfy
5221 * functions, are treated as fatal and lead to a failure of
5222 * ssl_parse_certificate even if verification was optional. */
5223 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5224 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5225 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5226 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005227 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005228 }
5229
5230 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5231 {
5232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5233 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5234 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005235
5236 if( ret != 0 )
5237 {
5238 /* The certificate may have been rejected for several reasons.
5239 Pick one and send the corresponding alert. Which alert to send
5240 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005241 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5242 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5243 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5244 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5245 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5246 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5247 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5248 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5249 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5250 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5251 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5252 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5253 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5254 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5255 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5256 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5257 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5258 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5259 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5260 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005261 else
5262 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5264 alert );
5265 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005266
Hanno Beckere6706e62017-05-15 16:05:15 +01005267#if defined(MBEDTLS_DEBUG_C)
5268 if( ssl->session_negotiate->verify_result != 0 )
5269 {
5270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5271 ssl->session_negotiate->verify_result ) );
5272 }
5273 else
5274 {
5275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5276 }
5277#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005278 }
5279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005281
5282 return( ret );
5283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5285 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5286 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5287 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5288 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5289 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5290 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005293{
5294 int ret;
5295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005299 ssl->out_msglen = 1;
5300 ssl->out_msg[0] = 1;
5301
Paul Bakker5121ce52009-01-03 21:22:43 +00005302 ssl->state++;
5303
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005304 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005305 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005307 return( ret );
5308 }
5309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005311
5312 return( 0 );
5313}
5314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005316{
5317 int ret;
5318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 return( ret );
5325 }
5326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005330 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5331 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005333 }
5334
5335 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005338 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5339 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005341 }
5342
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005343 /*
5344 * Switch to our negotiated transform and session parameters for inbound
5345 * data.
5346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005348 ssl->transform_in = ssl->transform_negotiate;
5349 ssl->session_in = ssl->session_negotiate;
5350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005352 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005355 ssl_dtls_replay_reset( ssl );
5356#endif
5357
5358 /* Increment epoch */
5359 if( ++ssl->in_epoch == 0 )
5360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005362 /* This is highly unlikely to happen for legitimate reasons, so
5363 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005365 }
5366 }
5367 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005369 memset( ssl->in_ctr, 0, 8 );
5370
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005371 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5374 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005379 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5380 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005382 }
5383 }
5384#endif
5385
Paul Bakker5121ce52009-01-03 21:22:43 +00005386 ssl->state++;
5387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005389
5390 return( 0 );
5391}
5392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5394 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005395{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005396 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5399 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5400 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005401 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005402 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5405#if defined(MBEDTLS_SHA512_C)
5406 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005407 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5408 else
5409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410#if defined(MBEDTLS_SHA256_C)
5411 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005412 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005413 else
5414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005418 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005419 }
Paul Bakker380da532012-04-18 16:10:25 +00005420}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005423{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5425 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005426 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5427 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005428#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005429#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5430#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005431 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005432#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005434 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005435#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005437}
5438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005440 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005441{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5443 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005444 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5445 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5448#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005449 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005452 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005455}
5456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5458 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5459static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005460 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005461{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005462 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5463 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005464}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005465#endif
Paul Bakker380da532012-04-18 16:10:25 +00005466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5468#if defined(MBEDTLS_SHA256_C)
5469static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005470 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005471{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005472 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005473}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005474#endif
Paul Bakker380da532012-04-18 16:10:25 +00005475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476#if defined(MBEDTLS_SHA512_C)
5477static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005478 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005479{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005480 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005481}
Paul Bakker769075d2012-11-24 11:26:46 +01005482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005486static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005488{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005489 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005490 mbedtls_md5_context md5;
5491 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005492
Paul Bakker5121ce52009-01-03 21:22:43 +00005493 unsigned char padbuf[48];
5494 unsigned char md5sum[16];
5495 unsigned char sha1sum[20];
5496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005498 if( !session )
5499 session = ssl->session;
5500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005502
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005503 mbedtls_md5_init( &md5 );
5504 mbedtls_sha1_init( &sha1 );
5505
5506 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5507 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005508
5509 /*
5510 * SSLv3:
5511 * hash =
5512 * MD5( master + pad2 +
5513 * MD5( handshake + sender + master + pad1 ) )
5514 * + SHA1( master + pad2 +
5515 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005516 */
5517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005519 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5520 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005521#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005524 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5525 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005529 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005530
Paul Bakker1ef83d62012-04-11 12:09:53 +00005531 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005532
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005533 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5534 mbedtls_md5_update_ret( &md5, session->master, 48 );
5535 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5536 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005537
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005538 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5539 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5540 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5541 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005542
Paul Bakker1ef83d62012-04-11 12:09:53 +00005543 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005544
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005545 mbedtls_md5_starts_ret( &md5 );
5546 mbedtls_md5_update_ret( &md5, session->master, 48 );
5547 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5548 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5549 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005550
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005551 mbedtls_sha1_starts_ret( &sha1 );
5552 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5553 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5554 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5555 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005558
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005559 mbedtls_md5_free( &md5 );
5560 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005561
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005562 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5563 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5564 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005571static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005573{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005574 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005575 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005576 mbedtls_md5_context md5;
5577 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005578 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005581 if( !session )
5582 session = ssl->session;
5583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005585
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005586 mbedtls_md5_init( &md5 );
5587 mbedtls_sha1_init( &sha1 );
5588
5589 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5590 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005591
Paul Bakker1ef83d62012-04-11 12:09:53 +00005592 /*
5593 * TLSv1:
5594 * hash = PRF( master, finished_label,
5595 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5596 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005599 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5600 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005601#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005604 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5605 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005606#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005609 ? "client finished"
5610 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005611
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005612 mbedtls_md5_finish_ret( &md5, padbuf );
5613 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005614
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005615 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005616 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005619
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005620 mbedtls_md5_free( &md5 );
5621 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005622
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005623 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005626}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5630#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005631static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005633{
5634 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005635 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005636 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005637 unsigned char padbuf[32];
5638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005640 if( !session )
5641 session = ssl->session;
5642
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005643 mbedtls_sha256_init( &sha256 );
5644
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005646
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005647 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005648
5649 /*
5650 * TLSv1.2:
5651 * hash = PRF( master, finished_label,
5652 * Hash( handshake ) )[0.11]
5653 */
5654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655#if !defined(MBEDTLS_SHA256_ALT)
5656 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005657 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005658#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005661 ? "client finished"
5662 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005663
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005664 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005665
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005666 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005667 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005670
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005671 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005672
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005673 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005680static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005682{
5683 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005684 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005685 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005686 unsigned char padbuf[48];
5687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005689 if( !session )
5690 session = ssl->session;
5691
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005692 mbedtls_sha512_init( &sha512 );
5693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005695
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005696 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005697
5698 /*
5699 * TLSv1.2:
5700 * hash = PRF( master, finished_label,
5701 * Hash( handshake ) )[0.11]
5702 */
5703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005705 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5706 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005707#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005710 ? "client finished"
5711 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005712
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005713 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005714
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005715 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005716 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005719
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005720 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005721
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005722 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005726#endif /* MBEDTLS_SHA512_C */
5727#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005732
5733 /*
5734 * Free our handshake params
5735 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005736 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005738 ssl->handshake = NULL;
5739
5740 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005741 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005742 */
5743 if( ssl->transform )
5744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745 mbedtls_ssl_transform_free( ssl->transform );
5746 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005747 }
5748 ssl->transform = ssl->transform_negotiate;
5749 ssl->transform_negotiate = NULL;
5750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005752}
5753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005755{
5756 int resume = ssl->handshake->resume;
5757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760#if defined(MBEDTLS_SSL_RENEGOTIATION)
5761 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005764 ssl->renego_records_seen = 0;
5765 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005766#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005767
5768 /*
5769 * Free the previous session and switch in the current one
5770 */
Paul Bakker0a597072012-09-25 21:55:46 +00005771 if( ssl->session )
5772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005774 /* RFC 7366 3.1: keep the EtM state */
5775 ssl->session_negotiate->encrypt_then_mac =
5776 ssl->session->encrypt_then_mac;
5777#endif
5778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 mbedtls_ssl_session_free( ssl->session );
5780 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005781 }
5782 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005783 ssl->session_negotiate = NULL;
5784
Paul Bakker0a597072012-09-25 21:55:46 +00005785 /*
5786 * Add cache entry
5787 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005788 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005789 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005790 resume == 0 )
5791 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005792 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005794 }
Paul Bakker0a597072012-09-25 21:55:46 +00005795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005797 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005798 ssl->handshake->flight != NULL )
5799 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005800 /* Cancel handshake timer */
5801 ssl_set_timer( ssl, 0 );
5802
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005803 /* Keep last flight around in case we need to resend it:
5804 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005806 }
5807 else
5808#endif
5809 ssl_handshake_wrapup_free_hs_transform( ssl );
5810
Paul Bakker48916f92012-09-16 19:57:18 +00005811 ssl->state++;
5812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005814}
5815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005817{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005818 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005821
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005822 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01005823
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005824 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005825
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005826 /*
5827 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5828 * may define some other value. Currently (early 2016), no defined
5829 * ciphersuite does this (and this is unlikely to change as activity has
5830 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005835 ssl->verify_data_len = hash_len;
5836 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005837#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005838
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5841 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005842
5843 /*
5844 * In case of session resuming, invert the client and server
5845 * ChangeCipherSpec messages order.
5846 */
Paul Bakker0a597072012-09-25 21:55:46 +00005847 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005850 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005852#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005854 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005856#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005857 }
5858 else
5859 ssl->state++;
5860
Paul Bakker48916f92012-09-16 19:57:18 +00005861 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005862 * Switch to our negotiated transform and session parameters for outbound
5863 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005868 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005869 {
5870 unsigned char i;
5871
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005872 /* Remember current epoch settings for resending */
5873 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01005874 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005875
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005876 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01005877 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005878
5879 /* Increment epoch */
5880 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01005881 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005882 break;
5883
5884 /* The loop goes to its end iff the counter is wrapping */
5885 if( i == 0 )
5886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5888 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005889 }
5890 }
5891 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01005893 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005894
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005895 ssl->transform_out = ssl->transform_negotiate;
5896 ssl->session_out = ssl->session_negotiate;
5897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5899 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5904 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005905 }
5906 }
5907#endif
5908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005912#endif
5913
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005914 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005915 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005917 return( ret );
5918 }
5919
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005920#if defined(MBEDTLS_SSL_PROTO_DTLS)
5921 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5922 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5923 {
5924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5925 return( ret );
5926 }
5927#endif
5928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005930
5931 return( 0 );
5932}
5933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005935#define SSL_MAX_HASH_LEN 36
5936#else
5937#define SSL_MAX_HASH_LEN 12
5938#endif
5939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005941{
Paul Bakker23986e52011-04-24 08:57:21 +00005942 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005943 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005944 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005947
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005948 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005953 return( ret );
5954 }
5955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005959 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5960 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005962 }
5963
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005964 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965#if defined(MBEDTLS_SSL_PROTO_SSL3)
5966 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005967 hash_len = 36;
5968 else
5969#endif
5970 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5973 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005976 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5977 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005979 }
5980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005982 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005985 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5986 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005988 }
5989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005991 ssl->verify_data_len = hash_len;
5992 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005993#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005994
Paul Bakker0a597072012-09-25 21:55:46 +00005995 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005998 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006000#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006002 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006004#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006005 }
6006 else
6007 ssl->state++;
6008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006010 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006012#endif
6013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006015
6016 return( 0 );
6017}
6018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6024 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6025 mbedtls_md5_init( &handshake->fin_md5 );
6026 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006027 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6028 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006029#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6031#if defined(MBEDTLS_SHA256_C)
6032 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006033 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035#if defined(MBEDTLS_SHA512_C)
6036 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006037 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006040
6041 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006042
6043#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6044 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6045 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6046#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048#if defined(MBEDTLS_DHM_C)
6049 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051#if defined(MBEDTLS_ECDH_C)
6052 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006053#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006054#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006055 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006056#if defined(MBEDTLS_SSL_CLI_C)
6057 handshake->ecjpake_cache = NULL;
6058 handshake->ecjpake_cache_len = 0;
6059#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006060#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006061
6062#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6063 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6064#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006065}
6066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6072 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 mbedtls_md_init( &transform->md_ctx_enc );
6075 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006076}
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006081}
6082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006084{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006085 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006086 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006088 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006090 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006091 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006092
6093 /*
6094 * Either the pointers are now NULL or cleared properly and can be freed.
6095 * Now allocate missing structures.
6096 */
6097 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006098 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006099 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006100 }
Paul Bakker48916f92012-09-16 19:57:18 +00006101
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006102 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006103 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006104 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006105 }
Paul Bakker48916f92012-09-16 19:57:18 +00006106
Paul Bakker82788fb2014-10-20 13:59:19 +02006107 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006108 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006109 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006110 }
Paul Bakker48916f92012-09-16 19:57:18 +00006111
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006112 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006113 if( ssl->handshake == NULL ||
6114 ssl->transform_negotiate == NULL ||
6115 ssl->session_negotiate == NULL )
6116 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 mbedtls_free( ssl->handshake );
6120 mbedtls_free( ssl->transform_negotiate );
6121 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006122
6123 ssl->handshake = NULL;
6124 ssl->transform_negotiate = NULL;
6125 ssl->session_negotiate = NULL;
6126
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006127 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006128 }
6129
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006130 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006132 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006133 ssl_handshake_params_init( ssl->handshake );
6134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006136 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6137 {
6138 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006139
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006140 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6141 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6142 else
6143 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006144
6145 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006146 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006147#endif
6148
Paul Bakker48916f92012-09-16 19:57:18 +00006149 return( 0 );
6150}
6151
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006152#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006153/* Dummy cookie callbacks for defaults */
6154static int ssl_cookie_write_dummy( void *ctx,
6155 unsigned char **p, unsigned char *end,
6156 const unsigned char *cli_id, size_t cli_id_len )
6157{
6158 ((void) ctx);
6159 ((void) p);
6160 ((void) end);
6161 ((void) cli_id);
6162 ((void) cli_id_len);
6163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006165}
6166
6167static int ssl_cookie_check_dummy( void *ctx,
6168 const unsigned char *cookie, size_t cookie_len,
6169 const unsigned char *cli_id, size_t cli_id_len )
6170{
6171 ((void) ctx);
6172 ((void) cookie);
6173 ((void) cookie_len);
6174 ((void) cli_id);
6175 ((void) cli_id_len);
6176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006178}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006179#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006180
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006181/* Once ssl->out_hdr as the address of the beginning of the
6182 * next outgoing record is set, deduce the other pointers.
6183 *
6184 * Note: For TLS, we save the implicit record sequence number
6185 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6186 * and the caller has to make sure there's space for this.
6187 */
6188
6189static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6190 mbedtls_ssl_transform *transform )
6191{
6192#if defined(MBEDTLS_SSL_PROTO_DTLS)
6193 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6194 {
6195 ssl->out_ctr = ssl->out_hdr + 3;
6196 ssl->out_len = ssl->out_hdr + 11;
6197 ssl->out_iv = ssl->out_hdr + 13;
6198 }
6199 else
6200#endif
6201 {
6202 ssl->out_ctr = ssl->out_hdr - 8;
6203 ssl->out_len = ssl->out_hdr + 3;
6204 ssl->out_iv = ssl->out_hdr + 5;
6205 }
6206
6207 /* Adjust out_msg to make space for explicit IV, if used. */
6208 if( transform != NULL &&
6209 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6210 {
6211 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6212 }
6213 else
6214 ssl->out_msg = ssl->out_iv;
6215}
6216
6217/* Once ssl->in_hdr as the address of the beginning of the
6218 * next incoming record is set, deduce the other pointers.
6219 *
6220 * Note: For TLS, we save the implicit record sequence number
6221 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6222 * and the caller has to make sure there's space for this.
6223 */
6224
6225static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6226 mbedtls_ssl_transform *transform )
6227{
6228#if defined(MBEDTLS_SSL_PROTO_DTLS)
6229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6230 {
6231 ssl->in_ctr = ssl->in_hdr + 3;
6232 ssl->in_len = ssl->in_hdr + 11;
6233 ssl->in_iv = ssl->in_hdr + 13;
6234 }
6235 else
6236#endif
6237 {
6238 ssl->in_ctr = ssl->in_hdr - 8;
6239 ssl->in_len = ssl->in_hdr + 3;
6240 ssl->in_iv = ssl->in_hdr + 5;
6241 }
6242
6243 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6244 if( transform != NULL &&
6245 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6246 {
6247 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6248 }
6249 else
6250 ssl->in_msg = ssl->in_iv;
6251}
6252
Paul Bakker5121ce52009-01-03 21:22:43 +00006253/*
6254 * Initialize an SSL context
6255 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006256void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6257{
6258 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6259}
6260
6261/*
6262 * Setup an SSL context
6263 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006264
6265static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6266{
6267 /* Set the incoming and outgoing record pointers. */
6268#if defined(MBEDTLS_SSL_PROTO_DTLS)
6269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6270 {
6271 ssl->out_hdr = ssl->out_buf;
6272 ssl->in_hdr = ssl->in_buf;
6273 }
6274 else
6275#endif /* MBEDTLS_SSL_PROTO_DTLS */
6276 {
6277 ssl->out_hdr = ssl->out_buf + 8;
6278 ssl->in_hdr = ssl->in_buf + 8;
6279 }
6280
6281 /* Derive other internal pointers. */
6282 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6283 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6284}
6285
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006286int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006287 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006288{
Paul Bakker48916f92012-09-16 19:57:18 +00006289 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006290
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006291 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006292
6293 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006294 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006295 */
Angus Grattond8213d02016-05-25 20:56:48 +10006296 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6297 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006298 {
Angus Grattond8213d02016-05-25 20:56:48 +10006299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6300 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6301 }
6302
6303 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6304 if( ssl->out_buf == NULL )
6305 {
6306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006308 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006309 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006310 }
6311
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006312 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006313
Paul Bakker48916f92012-09-16 19:57:18 +00006314 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6315 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006316
6317 return( 0 );
6318}
6319
6320/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006321 * Reset an initialized and used SSL context for re-use while retaining
6322 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006323 *
6324 * If partial is non-zero, keep data in the input buffer and client ID.
6325 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006326 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006327static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006328{
Paul Bakker48916f92012-09-16 19:57:18 +00006329 int ret;
6330
Hanno Becker7e772132018-08-10 12:38:21 +01006331#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6332 !defined(MBEDTLS_SSL_SRV_C)
6333 ((void) partial);
6334#endif
6335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006337
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006338 /* Cancel any possibly running timer */
6339 ssl_set_timer( ssl, 0 );
6340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341#if defined(MBEDTLS_SSL_RENEGOTIATION)
6342 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006343 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006344
6345 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6347 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006348#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006350
Paul Bakker7eb013f2011-10-06 12:37:39 +00006351 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006352 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006353
6354 ssl->in_msgtype = 0;
6355 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006357 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006358 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006359#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006361 ssl_dtls_replay_reset( ssl );
6362#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006363
6364 ssl->in_hslen = 0;
6365 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006366
6367 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006368
6369 ssl->out_msgtype = 0;
6370 ssl->out_msglen = 0;
6371 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6373 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006374 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006375#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006376
Hanno Becker19859472018-08-06 09:40:20 +01006377 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6378
Paul Bakker48916f92012-09-16 19:57:18 +00006379 ssl->transform_in = NULL;
6380 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006381
Hanno Becker78640902018-08-13 16:35:15 +01006382 ssl->session_in = NULL;
6383 ssl->session_out = NULL;
6384
Angus Grattond8213d02016-05-25 20:56:48 +10006385 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006386
6387#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006388 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006389#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6390 {
6391 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006392 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006393 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6396 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6399 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6402 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006403 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006404 }
6405#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006406
Paul Bakker48916f92012-09-16 19:57:18 +00006407 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 mbedtls_ssl_transform_free( ssl->transform );
6410 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006411 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006412 }
Paul Bakker48916f92012-09-16 19:57:18 +00006413
Paul Bakkerc0463502013-02-14 11:19:38 +01006414 if( ssl->session )
6415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 mbedtls_ssl_session_free( ssl->session );
6417 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006418 ssl->session = NULL;
6419 }
6420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006421#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006422 ssl->alpn_chosen = NULL;
6423#endif
6424
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006425#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006426#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006427 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006428#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006429 {
6430 mbedtls_free( ssl->cli_id );
6431 ssl->cli_id = NULL;
6432 ssl->cli_id_len = 0;
6433 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006434#endif
6435
Paul Bakker48916f92012-09-16 19:57:18 +00006436 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6437 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006438
6439 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006440}
6441
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006442/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006443 * Reset an initialized and used SSL context for re-use while retaining
6444 * all application-set variables, function pointers and data.
6445 */
6446int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6447{
6448 return( ssl_session_reset_int( ssl, 0 ) );
6449}
6450
6451/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 * SSL set accessors
6453 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006454void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006455{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006456 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006457}
6458
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006459void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006460{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006461 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006462}
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006465void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006466{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006467 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006468}
6469#endif
6470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006472void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006473{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006474 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006475}
6476#endif
6477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006479
Hanno Becker1841b0a2018-08-24 11:13:57 +01006480void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
6481 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01006482{
6483 ssl->disable_datagram_packing = !allow_packing;
6484}
6485
6486void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6487 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006488{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006489 conf->hs_timeout_min = min;
6490 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006491}
6492#endif
6493
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006494void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006495{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006496 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006497}
6498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006500void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006501 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006502 void *p_vrfy )
6503{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006504 conf->f_vrfy = f_vrfy;
6505 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006508
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006509void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006510 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006511 void *p_rng )
6512{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006513 conf->f_rng = f_rng;
6514 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006515}
6516
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006517void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006518 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006519 void *p_dbg )
6520{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006521 conf->f_dbg = f_dbg;
6522 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006523}
6524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006526 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006527 mbedtls_ssl_send_t *f_send,
6528 mbedtls_ssl_recv_t *f_recv,
6529 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006530{
6531 ssl->p_bio = p_bio;
6532 ssl->f_send = f_send;
6533 ssl->f_recv = f_recv;
6534 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006535}
6536
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02006537#if defined(MBEDTLS_SSL_PROTO_DTLS)
6538void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
6539{
6540 ssl->mtu = mtu;
6541}
6542#endif
6543
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006544void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006545{
6546 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006547}
6548
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006549void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6550 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006551 mbedtls_ssl_set_timer_t *f_set_timer,
6552 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006553{
6554 ssl->p_timer = p_timer;
6555 ssl->f_set_timer = f_set_timer;
6556 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006557
6558 /* Make sure we start with no timer running */
6559 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006560}
6561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006563void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006564 void *p_cache,
6565 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6566 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006567{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006568 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006569 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006570 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574#if defined(MBEDTLS_SSL_CLI_C)
6575int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006576{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006577 int ret;
6578
6579 if( ssl == NULL ||
6580 session == NULL ||
6581 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006582 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006585 }
6586
6587 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6588 return( ret );
6589
Paul Bakker0a597072012-09-25 21:55:46 +00006590 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006591
6592 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006595
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006596void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006597 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006598{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006599 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6600 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6601 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6602 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006603}
6604
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006605void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006606 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006607 int major, int minor )
6608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006610 return;
6611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006613 return;
6614
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006615 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006616}
6617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006619void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006620 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006621{
6622 conf->cert_profile = profile;
6623}
6624
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006625/* Append a new keycert entry to a (possibly empty) list */
6626static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6627 mbedtls_x509_crt *cert,
6628 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006629{
niisato8ee24222018-06-25 19:05:48 +09006630 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006631
niisato8ee24222018-06-25 19:05:48 +09006632 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6633 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006634 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006635
niisato8ee24222018-06-25 19:05:48 +09006636 new_cert->cert = cert;
6637 new_cert->key = key;
6638 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006639
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006640 /* Update head is the list was null, else add to the end */
6641 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006642 {
niisato8ee24222018-06-25 19:05:48 +09006643 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006644 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006645 else
6646 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006647 mbedtls_ssl_key_cert *cur = *head;
6648 while( cur->next != NULL )
6649 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006650 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006651 }
6652
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006653 return( 0 );
6654}
6655
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006656int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006657 mbedtls_x509_crt *own_cert,
6658 mbedtls_pk_context *pk_key )
6659{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006660 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006661}
6662
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006663void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006664 mbedtls_x509_crt *ca_chain,
6665 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006666{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006667 conf->ca_chain = ca_chain;
6668 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006671
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006672#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6673int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6674 mbedtls_x509_crt *own_cert,
6675 mbedtls_pk_context *pk_key )
6676{
6677 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6678 own_cert, pk_key ) );
6679}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006680
6681void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6682 mbedtls_x509_crt *ca_chain,
6683 mbedtls_x509_crl *ca_crl )
6684{
6685 ssl->handshake->sni_ca_chain = ca_chain;
6686 ssl->handshake->sni_ca_crl = ca_crl;
6687}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006688
6689void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6690 int authmode )
6691{
6692 ssl->handshake->sni_authmode = authmode;
6693}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006694#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6695
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006696#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006697/*
6698 * Set EC J-PAKE password for current handshake
6699 */
6700int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6701 const unsigned char *pw,
6702 size_t pw_len )
6703{
6704 mbedtls_ecjpake_role role;
6705
Janos Follath8eb64132016-06-03 15:40:57 +01006706 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6708
6709 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6710 role = MBEDTLS_ECJPAKE_SERVER;
6711 else
6712 role = MBEDTLS_ECJPAKE_CLIENT;
6713
6714 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6715 role,
6716 MBEDTLS_MD_SHA256,
6717 MBEDTLS_ECP_DP_SECP256R1,
6718 pw, pw_len ) );
6719}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006720#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006723int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006724 const unsigned char *psk, size_t psk_len,
6725 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006726{
Paul Bakker6db455e2013-09-18 17:29:31 +02006727 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6731 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006732
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006733 /* Identity len will be encoded on two bytes */
6734 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006735 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006736 {
6737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6738 }
6739
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006740 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006741 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006742 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006743
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006744 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006745 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006746 conf->psk_len = 0;
6747 }
6748 if( conf->psk_identity != NULL )
6749 {
6750 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006751 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006752 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006753 }
6754
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006755 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6756 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006757 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006758 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006759 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006760 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006761 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006762 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006763 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006764
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006765 conf->psk_len = psk_len;
6766 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006767
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006768 memcpy( conf->psk, psk, conf->psk_len );
6769 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006770
6771 return( 0 );
6772}
6773
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006774int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6775 const unsigned char *psk, size_t psk_len )
6776{
6777 if( psk == NULL || ssl->handshake == NULL )
6778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6779
6780 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6782
6783 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006784 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006785 mbedtls_platform_zeroize( ssl->handshake->psk,
6786 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006787 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006788 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006789 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006790
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006791 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006792 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006793
6794 ssl->handshake->psk_len = psk_len;
6795 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6796
6797 return( 0 );
6798}
6799
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006800void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006802 size_t),
6803 void *p_psk )
6804{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006805 conf->f_psk = f_psk;
6806 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006807}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006809
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006810#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006811
6812#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006813int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006814{
6815 int ret;
6816
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006817 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6818 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6819 {
6820 mbedtls_mpi_free( &conf->dhm_P );
6821 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
6825 return( 0 );
6826}
Hanno Becker470a8c42017-10-04 15:28:46 +01006827#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006828
Hanno Beckera90658f2017-10-04 15:29:08 +01006829int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6830 const unsigned char *dhm_P, size_t P_len,
6831 const unsigned char *dhm_G, size_t G_len )
6832{
6833 int ret;
6834
6835 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6836 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6837 {
6838 mbedtls_mpi_free( &conf->dhm_P );
6839 mbedtls_mpi_free( &conf->dhm_G );
6840 return( ret );
6841 }
6842
6843 return( 0 );
6844}
Paul Bakker5121ce52009-01-03 21:22:43 +00006845
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006846int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006847{
6848 int ret;
6849
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006850 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6851 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6852 {
6853 mbedtls_mpi_free( &conf->dhm_P );
6854 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006855 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006856 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006857
6858 return( 0 );
6859}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006860#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006861
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006862#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6863/*
6864 * Set the minimum length for Diffie-Hellman parameters
6865 */
6866void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6867 unsigned int bitlen )
6868{
6869 conf->dhm_min_bitlen = bitlen;
6870}
6871#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6872
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006873#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006874/*
6875 * Set allowed/preferred hashes for handshake signatures
6876 */
6877void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6878 const int *hashes )
6879{
6880 conf->sig_hashes = hashes;
6881}
Hanno Becker947194e2017-04-07 13:25:49 +01006882#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006883
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006884#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006885/*
6886 * Set the allowed elliptic curves
6887 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006888void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006889 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006890{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006891 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006892}
Hanno Becker947194e2017-04-07 13:25:49 +01006893#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006894
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006895#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006897{
Hanno Becker947194e2017-04-07 13:25:49 +01006898 /* Initialize to suppress unnecessary compiler warning */
6899 size_t hostname_len = 0;
6900
6901 /* Check if new hostname is valid before
6902 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006903 if( hostname != NULL )
6904 {
6905 hostname_len = strlen( hostname );
6906
6907 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6909 }
6910
6911 /* Now it's clear that we will overwrite the old hostname,
6912 * so we can free it safely */
6913
6914 if( ssl->hostname != NULL )
6915 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006916 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006917 mbedtls_free( ssl->hostname );
6918 }
6919
6920 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006921
Paul Bakker5121ce52009-01-03 21:22:43 +00006922 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006923 {
6924 ssl->hostname = NULL;
6925 }
6926 else
6927 {
6928 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006929 if( ssl->hostname == NULL )
6930 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006931
Hanno Becker947194e2017-04-07 13:25:49 +01006932 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006933
Hanno Becker947194e2017-04-07 13:25:49 +01006934 ssl->hostname[hostname_len] = '\0';
6935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006936
6937 return( 0 );
6938}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006939#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006940
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006941#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006942void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006944 const unsigned char *, size_t),
6945 void *p_sni )
6946{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006947 conf->f_sni = f_sni;
6948 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006949}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006953int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006954{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006955 size_t cur_len, tot_len;
6956 const char **p;
6957
6958 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006959 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6960 * MUST NOT be truncated."
6961 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006962 */
6963 tot_len = 0;
6964 for( p = protos; *p != NULL; p++ )
6965 {
6966 cur_len = strlen( *p );
6967 tot_len += cur_len;
6968
6969 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006971 }
6972
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006973 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006974
6975 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006976}
6977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006979{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006980 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006983
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006984void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006985{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006986 conf->max_major_ver = major;
6987 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006988}
6989
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006990void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006991{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006992 conf->min_major_ver = major;
6993 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006994}
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006997void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006998{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006999 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007000}
7001#endif
7002
Janos Follath088ce432017-04-10 12:42:31 +01007003#if defined(MBEDTLS_SSL_SRV_C)
7004void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7005 char cert_req_ca_list )
7006{
7007 conf->cert_req_ca_list = cert_req_ca_list;
7008}
7009#endif
7010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007012void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007013{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007014 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007015}
7016#endif
7017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007019void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007020{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007021 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007022}
7023#endif
7024
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007025#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007026void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007027{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007028 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007029}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007030#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007033int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007036 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007039 }
7040
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007041 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007042
7043 return( 0 );
7044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007048void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007049{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007050 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007055void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007056{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007057 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007058}
7059#endif
7060
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007061void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007062{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007063 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007064}
7065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007067void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007068{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007069 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007070}
7071
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007072void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007073{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007074 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007075}
7076
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007077void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007078 const unsigned char period[8] )
7079{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007080 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007085#if defined(MBEDTLS_SSL_CLI_C)
7086void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007087{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007088 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007089}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007090#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007091
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007092#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007093void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7094 mbedtls_ssl_ticket_write_t *f_ticket_write,
7095 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7096 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007097{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007098 conf->f_ticket_write = f_ticket_write;
7099 conf->f_ticket_parse = f_ticket_parse;
7100 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007101}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007102#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007104
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007105#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7106void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7107 mbedtls_ssl_export_keys_t *f_export_keys,
7108 void *p_export_keys )
7109{
7110 conf->f_export_keys = f_export_keys;
7111 conf->p_export_keys = p_export_keys;
7112}
7113#endif
7114
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007115#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007116void mbedtls_ssl_conf_async_private_cb(
7117 mbedtls_ssl_config *conf,
7118 mbedtls_ssl_async_sign_t *f_async_sign,
7119 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7120 mbedtls_ssl_async_resume_t *f_async_resume,
7121 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007122 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007123{
7124 conf->f_async_sign_start = f_async_sign;
7125 conf->f_async_decrypt_start = f_async_decrypt;
7126 conf->f_async_resume = f_async_resume;
7127 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007128 conf->p_async_config_data = async_config_data;
7129}
7130
Gilles Peskine8f97af72018-04-26 11:46:10 +02007131void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7132{
7133 return( conf->p_async_config_data );
7134}
7135
Gilles Peskine1febfef2018-04-30 11:54:39 +02007136void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007137{
7138 if( ssl->handshake == NULL )
7139 return( NULL );
7140 else
7141 return( ssl->handshake->user_async_ctx );
7142}
7143
Gilles Peskine1febfef2018-04-30 11:54:39 +02007144void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007145 void *ctx )
7146{
7147 if( ssl->handshake != NULL )
7148 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007149}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007150#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007151
Paul Bakker5121ce52009-01-03 21:22:43 +00007152/*
7153 * SSL get accessors
7154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007156{
7157 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7158}
7159
Hanno Becker8b170a02017-10-10 11:51:19 +01007160int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7161{
7162 /*
7163 * Case A: We're currently holding back
7164 * a message for further processing.
7165 */
7166
7167 if( ssl->keep_current_message == 1 )
7168 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007169 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007170 return( 1 );
7171 }
7172
7173 /*
7174 * Case B: Further records are pending in the current datagram.
7175 */
7176
7177#if defined(MBEDTLS_SSL_PROTO_DTLS)
7178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7179 ssl->in_left > ssl->next_record_offset )
7180 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007182 return( 1 );
7183 }
7184#endif /* MBEDTLS_SSL_PROTO_DTLS */
7185
7186 /*
7187 * Case C: A handshake message is being processed.
7188 */
7189
Hanno Becker8b170a02017-10-10 11:51:19 +01007190 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7191 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007193 return( 1 );
7194 }
7195
7196 /*
7197 * Case D: An application data message is being processed
7198 */
7199 if( ssl->in_offt != NULL )
7200 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007202 return( 1 );
7203 }
7204
7205 /*
7206 * In all other cases, the rest of the message can be dropped.
7207 * As in ssl_read_record_layer, this needs to be adapted if
7208 * we implement support for multiple alerts in single records.
7209 */
7210
7211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7212 return( 0 );
7213}
7214
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007215uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007216{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007217 if( ssl->session != NULL )
7218 return( ssl->session->verify_result );
7219
7220 if( ssl->session_negotiate != NULL )
7221 return( ssl->session_negotiate->verify_result );
7222
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007223 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007224}
7225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007227{
Paul Bakker926c8e42013-03-06 10:23:34 +01007228 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007229 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007232}
7233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007237 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007238 {
7239 switch( ssl->minor_ver )
7240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007242 return( "DTLSv1.0" );
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007245 return( "DTLSv1.2" );
7246
7247 default:
7248 return( "unknown (DTLS)" );
7249 }
7250 }
7251#endif
7252
Paul Bakker43ca69c2011-01-15 17:35:19 +00007253 switch( ssl->minor_ver )
7254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007256 return( "SSLv3.0" );
7257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007259 return( "TLSv1.0" );
7260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007262 return( "TLSv1.1" );
7263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007265 return( "TLSv1.2" );
7266
Paul Bakker43ca69c2011-01-15 17:35:19 +00007267 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007268 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007269 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007270}
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007273{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007274 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007276 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007277
Hanno Becker78640902018-08-13 16:35:15 +01007278 if( transform == NULL )
7279 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281#if defined(MBEDTLS_ZLIB_SUPPORT)
7282 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7283 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007284#endif
7285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 case MBEDTLS_MODE_GCM:
7289 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007290 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007292 transform_expansion = transform->minlen;
7293 break;
7294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007296
7297 block_size = mbedtls_cipher_get_block_size(
7298 &transform->cipher_ctx_enc );
7299
7300#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7301 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7302 {
7303 /* Expansion due to addition of
7304 * - MAC
7305 * - CBC padding (theoretically up to 256 bytes, but
7306 * we never use more than block_size)
7307 * - explicit IV
7308 */
7309 transform_expansion = transform->maclen + 2 * block_size;
7310 }
7311 else
7312#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7313 {
7314 /* No explicit IV prior to TLS 1.1. */
7315 transform_expansion = transform->maclen + block_size;
7316 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007317 break;
7318
7319 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007322 }
7323
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007324 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007325}
7326
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007327#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7328size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7329{
7330 size_t max_len;
7331
7332 /*
7333 * Assume mfl_code is correct since it was checked when set
7334 */
Angus Grattond8213d02016-05-25 20:56:48 +10007335 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007336
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007337 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007338 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007339 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007340 {
Angus Grattond8213d02016-05-25 20:56:48 +10007341 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007342 }
7343
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007344 /* During a handshake, use the value being negotiated */
7345 if( ssl->session_negotiate != NULL &&
7346 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7347 {
7348 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7349 }
7350
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007351 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007352}
7353#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7354
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007355#if defined(MBEDTLS_SSL_PROTO_DTLS)
7356static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7357{
7358 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7359 return( ssl->mtu );
7360
7361 if( ssl->mtu == 0 )
7362 return( ssl->handshake->mtu );
7363
7364 return( ssl->mtu < ssl->handshake->mtu ?
7365 ssl->mtu : ssl->handshake->mtu );
7366}
7367#endif /* MBEDTLS_SSL_PROTO_DTLS */
7368
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007369int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7370{
7371 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7372
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007373#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7374 !defined(MBEDTLS_SSL_PROTO_DTLS)
7375 (void) ssl;
7376#endif
7377
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007378#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7379 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7380
7381 if( max_len > mfl )
7382 max_len = mfl;
7383#endif
7384
7385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007386 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007387 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007388 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007389 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7390 const size_t overhead = (size_t) ret;
7391
7392 if( ret < 0 )
7393 return( ret );
7394
7395 if( mtu <= overhead )
7396 {
7397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7398 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7399 }
7400
7401 if( max_len > mtu - overhead )
7402 max_len = mtu - overhead;
7403 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007404#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007405
Hanno Becker0defedb2018-08-10 12:35:02 +01007406#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7407 !defined(MBEDTLS_SSL_PROTO_DTLS)
7408 ((void) ssl);
7409#endif
7410
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007411 return( (int) max_len );
7412}
7413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414#if defined(MBEDTLS_X509_CRT_PARSE_C)
7415const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007416{
7417 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007418 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007419
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007420 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007421}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424#if defined(MBEDTLS_SSL_CLI_C)
7425int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007426{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007427 if( ssl == NULL ||
7428 dst == NULL ||
7429 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007430 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007433 }
7434
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007435 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007438
Paul Bakker5121ce52009-01-03 21:22:43 +00007439/*
Paul Bakker1961b702013-01-25 14:49:24 +01007440 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007445
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007446 if( ssl == NULL || ssl->conf == NULL )
7447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007450 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007452#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007454 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007456#endif
7457
Paul Bakker1961b702013-01-25 14:49:24 +01007458 return( ret );
7459}
7460
7461/*
7462 * Perform the SSL handshake
7463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007465{
7466 int ret = 0;
7467
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007468 if( ssl == NULL || ssl->conf == NULL )
7469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007476
7477 if( ret != 0 )
7478 break;
7479 }
7480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007482
7483 return( ret );
7484}
7485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486#if defined(MBEDTLS_SSL_RENEGOTIATION)
7487#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007488/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007489 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007492{
7493 int ret;
7494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007496
7497 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7499 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007500
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007501 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007502 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007504 return( ret );
7505 }
7506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007508
7509 return( 0 );
7510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007512
7513/*
7514 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 * - any side: calling mbedtls_ssl_renegotiate(),
7516 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7517 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007518 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007519 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007523{
7524 int ret;
7525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007527
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007528 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7529 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007530
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007531 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7532 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007536 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007537 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007538 ssl->handshake->out_msg_seq = 1;
7539 else
7540 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007541 }
7542#endif
7543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7545 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007550 return( ret );
7551 }
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007554
7555 return( 0 );
7556}
7557
7558/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007559 * Renegotiate current connection on client,
7560 * or request renegotiation on server
7561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007565
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007566 if( ssl == NULL || ssl->conf == NULL )
7567 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007570 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007571 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007577
7578 /* Did we already try/start sending HelloRequest? */
7579 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007581
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007582 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007587 /*
7588 * On client, either start the renegotiation process or,
7589 * if already in progress, continue the handshake
7590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007595
7596 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007599 return( ret );
7600 }
7601 }
7602 else
7603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007607 return( ret );
7608 }
7609 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007611
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007612 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007613}
7614
7615/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007616 * Check record counters and renegotiate if they're above the limit.
7617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007619{
Andres AG2196c7f2016-12-15 17:01:16 +00007620 size_t ep_len = ssl_ep_len( ssl );
7621 int in_ctr_cmp;
7622 int out_ctr_cmp;
7623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7625 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007626 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007627 {
7628 return( 0 );
7629 }
7630
Andres AG2196c7f2016-12-15 17:01:16 +00007631 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7632 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01007633 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00007634 ssl->conf->renego_period + ep_len, 8 - ep_len );
7635
7636 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007637 {
7638 return( 0 );
7639 }
7640
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007645
7646/*
7647 * Receive application data decrypted from the SSL layer
7648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007650{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007651 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007652 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007653
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007654 if( ssl == NULL || ssl->conf == NULL )
7655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007663 return( ret );
7664
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007665 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007667 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007668 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007669 return( ret );
7670 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007671 }
7672#endif
7673
Hanno Becker4a810fb2017-05-24 16:27:30 +01007674 /*
7675 * Check if renegotiation is necessary and/or handshake is
7676 * in process. If yes, perform/continue, and fall through
7677 * if an unexpected packet is received while the client
7678 * is waiting for the ServerHello.
7679 *
7680 * (There is no equivalent to the last condition on
7681 * the server-side as it is not treated as within
7682 * a handshake while waiting for the ClientHello
7683 * after a renegotiation request.)
7684 */
7685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007687 ret = ssl_check_ctr_renegotiate( ssl );
7688 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7689 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007692 return( ret );
7693 }
7694#endif
7695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007699 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7700 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007703 return( ret );
7704 }
7705 }
7706
Hanno Beckere41158b2017-10-23 13:30:32 +01007707 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007708 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007709 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007710 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007711 if( ssl->f_get_timer != NULL &&
7712 ssl->f_get_timer( ssl->p_timer ) == -1 )
7713 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007714 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007715 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007716
Hanno Becker4a810fb2017-05-24 16:27:30 +01007717 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007718 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007719 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7720 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007721
Hanno Becker4a810fb2017-05-24 16:27:30 +01007722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7723 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 }
7725
7726 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007728 {
7729 /*
7730 * OpenSSL sends empty messages to randomize the IV
7731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007735 return( 0 );
7736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007738 return( ret );
7739 }
7740 }
7741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007745
Hanno Becker4a810fb2017-05-24 16:27:30 +01007746 /*
7747 * - For client-side, expect SERVER_HELLO_REQUEST.
7748 * - For server-side, expect CLIENT_HELLO.
7749 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7750 */
7751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007753 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007755 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007758
7759 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007762 {
7763 continue;
7764 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007765#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007767 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007768#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007769
Hanno Becker4a810fb2017-05-24 16:27:30 +01007770#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007771 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007775
7776 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007778 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007779 {
7780 continue;
7781 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007782#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007784 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007785#endif /* MBEDTLS_SSL_SRV_C */
7786
Hanno Becker21df7f92017-10-17 11:03:26 +01007787#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007788 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007789 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7790 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7791 ssl->conf->allow_legacy_renegotiation ==
7792 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7793 {
7794 /*
7795 * Accept renegotiation request
7796 */
Paul Bakker48916f92012-09-16 19:57:18 +00007797
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007798 /* DTLS clients need to know renego is server-initiated */
7799#if defined(MBEDTLS_SSL_PROTO_DTLS)
7800 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7801 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7802 {
7803 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7804 }
7805#endif
7806 ret = ssl_start_renegotiation( ssl );
7807 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7808 ret != 0 )
7809 {
7810 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7811 return( ret );
7812 }
7813 }
7814 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007815#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007816 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007817 /*
7818 * Refuse renegotiation
7819 */
7820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823#if defined(MBEDTLS_SSL_PROTO_SSL3)
7824 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007825 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007826 /* SSLv3 does not have a "no_renegotiation" warning, so
7827 we send a fatal alert and abort the connection. */
7828 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7829 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7830 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007831 }
7832 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7834#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7835 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7836 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7839 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7840 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007841 {
7842 return( ret );
7843 }
Paul Bakker48916f92012-09-16 19:57:18 +00007844 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007845 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7847 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7850 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007851 }
Paul Bakker48916f92012-09-16 19:57:18 +00007852 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007853
Hanno Becker90333da2017-10-10 11:27:13 +01007854 /* At this point, we don't know whether the renegotiation has been
7855 * completed or not. The cases to consider are the following:
7856 * 1) The renegotiation is complete. In this case, no new record
7857 * has been read yet.
7858 * 2) The renegotiation is incomplete because the client received
7859 * an application data record while awaiting the ServerHello.
7860 * 3) The renegotiation is incomplete because the client received
7861 * a non-handshake, non-application data message while awaiting
7862 * the ServerHello.
7863 * In each of these case, looping will be the proper action:
7864 * - For 1), the next iteration will read a new record and check
7865 * if it's application data.
7866 * - For 2), the loop condition isn't satisfied as application data
7867 * is present, hence continue is the same as break
7868 * - For 3), the loop condition is satisfied and read_record
7869 * will re-deliver the message that was held back by the client
7870 * when expecting the ServerHello.
7871 */
7872 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007873 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007874#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007876 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007877 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007878 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007879 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007882 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007884 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007885 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007886 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7890 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007893 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007894 }
7895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7899 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007900 }
7901
7902 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007903
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007904 /* We're going to return something now, cancel timer,
7905 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007907 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007908
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007910 /* If we requested renego but received AppData, resend HelloRequest.
7911 * Do it now, after setting in_offt, to avoid taking this branch
7912 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007914 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007916 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007917 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007920 return( ret );
7921 }
7922 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007924#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007925 }
7926
7927 n = ( len < ssl->in_msglen )
7928 ? len : ssl->in_msglen;
7929
7930 memcpy( buf, ssl->in_offt, n );
7931 ssl->in_msglen -= n;
7932
7933 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007934 {
7935 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007936 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007937 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007939 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007940 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007941 /* more data available */
7942 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007943 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007946
Paul Bakker23986e52011-04-24 08:57:21 +00007947 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007948}
7949
7950/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007951 * Send application data to be encrypted by the SSL layer, taking care of max
7952 * fragment length and buffer size.
7953 *
7954 * According to RFC 5246 Section 6.2.1:
7955 *
7956 * Zero-length fragments of Application data MAY be sent as they are
7957 * potentially useful as a traffic analysis countermeasure.
7958 *
7959 * Therefore, it is possible that the input message length is 0 and the
7960 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007961 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007962static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007963 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007964{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007965 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7966 const size_t max_len = (size_t) ret;
7967
7968 if( ret < 0 )
7969 {
7970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7971 return( ret );
7972 }
7973
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007974 if( len > max_len )
7975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007977 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007980 "maximum fragment length: %d > %d",
7981 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007983 }
7984 else
7985#endif
7986 len = max_len;
7987 }
Paul Bakker887bd502011-06-08 13:10:54 +00007988
Paul Bakker5121ce52009-01-03 21:22:43 +00007989 if( ssl->out_left != 0 )
7990 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007991 /*
7992 * The user has previously tried to send the data and
7993 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7994 * written. In this case, we expect the high-level write function
7995 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008000 return( ret );
8001 }
8002 }
Paul Bakker887bd502011-06-08 13:10:54 +00008003 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008004 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008005 /*
8006 * The user is trying to send a message the first time, so we need to
8007 * copy the data into the internal buffers and setup the data structure
8008 * to keep track of partial writes
8009 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008010 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008012 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008013
Hanno Becker67bc7c32018-08-06 11:33:50 +01008014 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008017 return( ret );
8018 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008019 }
8020
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008021 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008022}
8023
8024/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008025 * Write application data, doing 1/n-1 splitting if necessary.
8026 *
8027 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008028 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008029 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008032static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008033 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008034{
8035 int ret;
8036
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008037 if( ssl->conf->cbc_record_splitting ==
8038 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008039 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8041 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8042 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008043 {
8044 return( ssl_write_real( ssl, buf, len ) );
8045 }
8046
8047 if( ssl->split_done == 0 )
8048 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008049 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008050 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008051 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008052 }
8053
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008054 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8055 return( ret );
8056 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008057
8058 return( ret + 1 );
8059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008061
8062/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008063 * Write application data (public-facing wrapper)
8064 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008065int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008066{
8067 int ret;
8068
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008070
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008071 if( ssl == NULL || ssl->conf == NULL )
8072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8073
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008074#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008075 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8076 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008077 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008078 return( ret );
8079 }
8080#endif
8081
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008082 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008083 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008084 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008085 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008087 return( ret );
8088 }
8089 }
8090
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008091#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008092 ret = ssl_write_split( ssl, buf, len );
8093#else
8094 ret = ssl_write_real( ssl, buf, len );
8095#endif
8096
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008098
8099 return( ret );
8100}
8101
8102/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008103 * Notify the peer that the connection is being closed
8104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008106{
8107 int ret;
8108
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008109 if( ssl == NULL || ssl->conf == NULL )
8110 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008113
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008114 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8120 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8121 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008124 return( ret );
8125 }
8126 }
8127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008129
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008130 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008131}
8132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008134{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008135 if( transform == NULL )
8136 return;
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008139 deflateEnd( &transform->ctx_deflate );
8140 inflateEnd( &transform->ctx_inflate );
8141#endif
8142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8144 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 mbedtls_md_free( &transform->md_ctx_enc );
8147 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008148
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008149 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008150}
8151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152#if defined(MBEDTLS_X509_CRT_PARSE_C)
8153static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008156
8157 while( cur != NULL )
8158 {
8159 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008161 cur = next;
8162 }
8163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008165
Gilles Peskine9b562d52018-04-25 20:32:43 +02008166void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008167{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008168 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8169
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008170 if( handshake == NULL )
8171 return;
8172
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008173#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8174 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8175 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008176 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008177 handshake->async_in_progress = 0;
8178 }
8179#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8180
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008181#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8182 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8183 mbedtls_md5_free( &handshake->fin_md5 );
8184 mbedtls_sha1_free( &handshake->fin_sha1 );
8185#endif
8186#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8187#if defined(MBEDTLS_SHA256_C)
8188 mbedtls_sha256_free( &handshake->fin_sha256 );
8189#endif
8190#if defined(MBEDTLS_SHA512_C)
8191 mbedtls_sha512_free( &handshake->fin_sha512 );
8192#endif
8193#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195#if defined(MBEDTLS_DHM_C)
8196 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198#if defined(MBEDTLS_ECDH_C)
8199 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008200#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008201#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008202 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008203#if defined(MBEDTLS_SSL_CLI_C)
8204 mbedtls_free( handshake->ecjpake_cache );
8205 handshake->ecjpake_cache = NULL;
8206 handshake->ecjpake_cache_len = 0;
8207#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008208#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008209
Janos Follath4ae5c292016-02-10 11:27:43 +00008210#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8211 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008212 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008214#endif
8215
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008216#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8217 if( handshake->psk != NULL )
8218 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008219 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008220 mbedtls_free( handshake->psk );
8221 }
8222#endif
8223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8225 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008226 /*
8227 * Free only the linked list wrapper, not the keys themselves
8228 * since the belong to the SNI callback
8229 */
8230 if( handshake->sni_key_cert != NULL )
8231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008233
8234 while( cur != NULL )
8235 {
8236 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008238 cur = next;
8239 }
8240 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243#if defined(MBEDTLS_SSL_PROTO_DTLS)
8244 mbedtls_free( handshake->verify_cookie );
8245 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008246 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008247#endif
8248
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008249 mbedtls_platform_zeroize( handshake,
8250 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008251}
8252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008254{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008255 if( session == NULL )
8256 return;
8257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008259 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 mbedtls_x509_crt_free( session->peer_cert );
8262 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008263 }
Paul Bakkered27a042013-04-18 22:46:23 +02008264#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008265
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008266#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008268#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008269
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008270 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008271}
8272
Paul Bakker5121ce52009-01-03 21:22:43 +00008273/*
8274 * Free an SSL context
8275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008277{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008278 if( ssl == NULL )
8279 return;
8280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008282
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008283 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008284 {
Angus Grattond8213d02016-05-25 20:56:48 +10008285 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008287 }
8288
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008289 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008290 {
Angus Grattond8213d02016-05-25 20:56:48 +10008291 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008293 }
8294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008296 if( ssl->compress_buf != NULL )
8297 {
Angus Grattond8213d02016-05-25 20:56:48 +10008298 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008300 }
8301#endif
8302
Paul Bakker48916f92012-09-16 19:57:18 +00008303 if( ssl->transform )
8304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305 mbedtls_ssl_transform_free( ssl->transform );
8306 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008307 }
8308
8309 if( ssl->handshake )
8310 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008311 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8313 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315 mbedtls_free( ssl->handshake );
8316 mbedtls_free( ssl->transform_negotiate );
8317 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008318 }
8319
Paul Bakkerc0463502013-02-14 11:19:38 +01008320 if( ssl->session )
8321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 mbedtls_ssl_session_free( ssl->session );
8323 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008324 }
8325
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008326#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008327 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008328 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008329 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008331 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8335 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8338 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008339 }
8340#endif
8341
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008342#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008344#endif
8345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008347
Paul Bakker86f04f42013-02-14 11:20:09 +01008348 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008349 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008350}
8351
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008352/*
8353 * Initialze mbedtls_ssl_config
8354 */
8355void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8356{
8357 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8358}
8359
Simon Butcherc97b6972015-12-27 23:48:17 +00008360#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008361static int ssl_preset_default_hashes[] = {
8362#if defined(MBEDTLS_SHA512_C)
8363 MBEDTLS_MD_SHA512,
8364 MBEDTLS_MD_SHA384,
8365#endif
8366#if defined(MBEDTLS_SHA256_C)
8367 MBEDTLS_MD_SHA256,
8368 MBEDTLS_MD_SHA224,
8369#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008370#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008371 MBEDTLS_MD_SHA1,
8372#endif
8373 MBEDTLS_MD_NONE
8374};
Simon Butcherc97b6972015-12-27 23:48:17 +00008375#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008376
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008377static int ssl_preset_suiteb_ciphersuites[] = {
8378 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8379 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8380 0
8381};
8382
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008383#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008384static int ssl_preset_suiteb_hashes[] = {
8385 MBEDTLS_MD_SHA256,
8386 MBEDTLS_MD_SHA384,
8387 MBEDTLS_MD_NONE
8388};
8389#endif
8390
8391#if defined(MBEDTLS_ECP_C)
8392static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8393 MBEDTLS_ECP_DP_SECP256R1,
8394 MBEDTLS_ECP_DP_SECP384R1,
8395 MBEDTLS_ECP_DP_NONE
8396};
8397#endif
8398
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008399/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008400 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008401 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008402int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008403 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008404{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008405#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008406 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008407#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008408
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008409 /* Use the functions here so that they are covered in tests,
8410 * but otherwise access member directly for efficiency */
8411 mbedtls_ssl_conf_endpoint( conf, endpoint );
8412 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008413
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008414 /*
8415 * Things that are common to all presets
8416 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008417#if defined(MBEDTLS_SSL_CLI_C)
8418 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8419 {
8420 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8421#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8422 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8423#endif
8424 }
8425#endif
8426
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008427#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008428 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008429#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008430
8431#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8432 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8433#endif
8434
8435#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8436 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8437#endif
8438
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008439#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8440 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8441#endif
8442
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008443#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008444 conf->f_cookie_write = ssl_cookie_write_dummy;
8445 conf->f_cookie_check = ssl_cookie_check_dummy;
8446#endif
8447
8448#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8449 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8450#endif
8451
Janos Follath088ce432017-04-10 12:42:31 +01008452#if defined(MBEDTLS_SSL_SRV_C)
8453 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8454#endif
8455
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008456#if defined(MBEDTLS_SSL_PROTO_DTLS)
8457 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8458 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8459#endif
8460
8461#if defined(MBEDTLS_SSL_RENEGOTIATION)
8462 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008463 memset( conf->renego_period, 0x00, 2 );
8464 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008465#endif
8466
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008467#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8468 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8469 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008470 const unsigned char dhm_p[] =
8471 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8472 const unsigned char dhm_g[] =
8473 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8474
Hanno Beckera90658f2017-10-04 15:29:08 +01008475 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8476 dhm_p, sizeof( dhm_p ),
8477 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008478 {
8479 return( ret );
8480 }
8481 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008482#endif
8483
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008484 /*
8485 * Preset-specific defaults
8486 */
8487 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008488 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008489 /*
8490 * NSA Suite B
8491 */
8492 case MBEDTLS_SSL_PRESET_SUITEB:
8493 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8494 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8495 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8496 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8497
8498 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8499 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8500 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8501 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8502 ssl_preset_suiteb_ciphersuites;
8503
8504#if defined(MBEDTLS_X509_CRT_PARSE_C)
8505 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008506#endif
8507
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008508#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008509 conf->sig_hashes = ssl_preset_suiteb_hashes;
8510#endif
8511
8512#if defined(MBEDTLS_ECP_C)
8513 conf->curve_list = ssl_preset_suiteb_curves;
8514#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008515 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008516
8517 /*
8518 * Default
8519 */
8520 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008521 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8522 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8523 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8524 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8525 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8526 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8527 MBEDTLS_SSL_MIN_MINOR_VERSION :
8528 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008529 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8530 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8531
8532#if defined(MBEDTLS_SSL_PROTO_DTLS)
8533 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8534 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8535#endif
8536
8537 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8538 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8539 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8540 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8541 mbedtls_ssl_list_ciphersuites();
8542
8543#if defined(MBEDTLS_X509_CRT_PARSE_C)
8544 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8545#endif
8546
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008547#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008548 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008549#endif
8550
8551#if defined(MBEDTLS_ECP_C)
8552 conf->curve_list = mbedtls_ecp_grp_id_list();
8553#endif
8554
8555#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8556 conf->dhm_min_bitlen = 1024;
8557#endif
8558 }
8559
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008560 return( 0 );
8561}
8562
8563/*
8564 * Free mbedtls_ssl_config
8565 */
8566void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8567{
8568#if defined(MBEDTLS_DHM_C)
8569 mbedtls_mpi_free( &conf->dhm_P );
8570 mbedtls_mpi_free( &conf->dhm_G );
8571#endif
8572
8573#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8574 if( conf->psk != NULL )
8575 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008576 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008577 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008578 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008579 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008580 }
8581
8582 if( conf->psk_identity != NULL )
8583 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008584 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008585 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008586 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008587 conf->psk_identity_len = 0;
8588 }
8589#endif
8590
8591#if defined(MBEDTLS_X509_CRT_PARSE_C)
8592 ssl_key_cert_free( conf->key_cert );
8593#endif
8594
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008595 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008596}
8597
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008598#if defined(MBEDTLS_PK_C) && \
8599 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008600/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605#if defined(MBEDTLS_RSA_C)
8606 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8607 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609#if defined(MBEDTLS_ECDSA_C)
8610 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8611 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008612#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008614}
8615
Hanno Becker7e5437a2017-04-28 17:15:26 +01008616unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8617{
8618 switch( type ) {
8619 case MBEDTLS_PK_RSA:
8620 return( MBEDTLS_SSL_SIG_RSA );
8621 case MBEDTLS_PK_ECDSA:
8622 case MBEDTLS_PK_ECKEY:
8623 return( MBEDTLS_SSL_SIG_ECDSA );
8624 default:
8625 return( MBEDTLS_SSL_SIG_ANON );
8626 }
8627}
8628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008630{
8631 switch( sig )
8632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633#if defined(MBEDTLS_RSA_C)
8634 case MBEDTLS_SSL_SIG_RSA:
8635 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008636#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#if defined(MBEDTLS_ECDSA_C)
8638 case MBEDTLS_SSL_SIG_ECDSA:
8639 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008640#endif
8641 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008643 }
8644}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008645#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008646
Hanno Becker7e5437a2017-04-28 17:15:26 +01008647#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8648 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8649
8650/* Find an entry in a signature-hash set matching a given hash algorithm. */
8651mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8652 mbedtls_pk_type_t sig_alg )
8653{
8654 switch( sig_alg )
8655 {
8656 case MBEDTLS_PK_RSA:
8657 return( set->rsa );
8658 case MBEDTLS_PK_ECDSA:
8659 return( set->ecdsa );
8660 default:
8661 return( MBEDTLS_MD_NONE );
8662 }
8663}
8664
8665/* Add a signature-hash-pair to a signature-hash set */
8666void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8667 mbedtls_pk_type_t sig_alg,
8668 mbedtls_md_type_t md_alg )
8669{
8670 switch( sig_alg )
8671 {
8672 case MBEDTLS_PK_RSA:
8673 if( set->rsa == MBEDTLS_MD_NONE )
8674 set->rsa = md_alg;
8675 break;
8676
8677 case MBEDTLS_PK_ECDSA:
8678 if( set->ecdsa == MBEDTLS_MD_NONE )
8679 set->ecdsa = md_alg;
8680 break;
8681
8682 default:
8683 break;
8684 }
8685}
8686
8687/* Allow exactly one hash algorithm for each signature. */
8688void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8689 mbedtls_md_type_t md_alg )
8690{
8691 set->rsa = md_alg;
8692 set->ecdsa = md_alg;
8693}
8694
8695#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8696 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8697
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008698/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008699 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008702{
8703 switch( hash )
8704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705#if defined(MBEDTLS_MD5_C)
8706 case MBEDTLS_SSL_HASH_MD5:
8707 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008708#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709#if defined(MBEDTLS_SHA1_C)
8710 case MBEDTLS_SSL_HASH_SHA1:
8711 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713#if defined(MBEDTLS_SHA256_C)
8714 case MBEDTLS_SSL_HASH_SHA224:
8715 return( MBEDTLS_MD_SHA224 );
8716 case MBEDTLS_SSL_HASH_SHA256:
8717 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008718#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719#if defined(MBEDTLS_SHA512_C)
8720 case MBEDTLS_SSL_HASH_SHA384:
8721 return( MBEDTLS_MD_SHA384 );
8722 case MBEDTLS_SSL_HASH_SHA512:
8723 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008724#endif
8725 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008727 }
8728}
8729
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008730/*
8731 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8732 */
8733unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8734{
8735 switch( md )
8736 {
8737#if defined(MBEDTLS_MD5_C)
8738 case MBEDTLS_MD_MD5:
8739 return( MBEDTLS_SSL_HASH_MD5 );
8740#endif
8741#if defined(MBEDTLS_SHA1_C)
8742 case MBEDTLS_MD_SHA1:
8743 return( MBEDTLS_SSL_HASH_SHA1 );
8744#endif
8745#if defined(MBEDTLS_SHA256_C)
8746 case MBEDTLS_MD_SHA224:
8747 return( MBEDTLS_SSL_HASH_SHA224 );
8748 case MBEDTLS_MD_SHA256:
8749 return( MBEDTLS_SSL_HASH_SHA256 );
8750#endif
8751#if defined(MBEDTLS_SHA512_C)
8752 case MBEDTLS_MD_SHA384:
8753 return( MBEDTLS_SSL_HASH_SHA384 );
8754 case MBEDTLS_MD_SHA512:
8755 return( MBEDTLS_SSL_HASH_SHA512 );
8756#endif
8757 default:
8758 return( MBEDTLS_SSL_HASH_NONE );
8759 }
8760}
8761
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008762#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008763/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008764 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008765 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008766 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008767int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008770
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008771 if( ssl->conf->curve_list == NULL )
8772 return( -1 );
8773
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008774 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008775 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008776 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008777
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008778 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008779}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008780#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008781
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008782#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008783/*
8784 * Check if a hash proposed by the peer is in our list.
8785 * Return 0 if we're willing to use it, -1 otherwise.
8786 */
8787int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8788 mbedtls_md_type_t md )
8789{
8790 const int *cur;
8791
8792 if( ssl->conf->sig_hashes == NULL )
8793 return( -1 );
8794
8795 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8796 if( *cur == (int) md )
8797 return( 0 );
8798
8799 return( -1 );
8800}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008801#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008803#if defined(MBEDTLS_X509_CRT_PARSE_C)
8804int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8805 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008806 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008807 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008808{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008809 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008811 int usage = 0;
8812#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008814 const char *ext_oid;
8815 size_t ext_len;
8816#endif
8817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8819 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008820 ((void) cert);
8821 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008822 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008823#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8826 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008827 {
8828 /* Server part of the key exchange */
8829 switch( ciphersuite->key_exchange )
8830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 case MBEDTLS_KEY_EXCHANGE_RSA:
8832 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008833 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008834 break;
8835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8837 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8838 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8839 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008840 break;
8841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8843 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008844 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008845 break;
8846
8847 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848 case MBEDTLS_KEY_EXCHANGE_NONE:
8849 case MBEDTLS_KEY_EXCHANGE_PSK:
8850 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8851 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008852 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008853 usage = 0;
8854 }
8855 }
8856 else
8857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8859 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008860 }
8861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008863 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008864 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008865 ret = -1;
8866 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008867#else
8868 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8872 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8875 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008876 }
8877 else
8878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8880 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008881 }
8882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008883 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008884 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008885 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008886 ret = -1;
8887 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008889
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008890 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008893
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008894/*
8895 * Convert version numbers to/from wire format
8896 * and, for DTLS, to/from TLS equivalent.
8897 *
8898 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008899 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008900 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8901 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008904 unsigned char ver[2] )
8905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008906#if defined(MBEDTLS_SSL_PROTO_DTLS)
8907 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008910 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8911
8912 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8913 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8914 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008915 else
8916#else
8917 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008918#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008919 {
8920 ver[0] = (unsigned char) major;
8921 ver[1] = (unsigned char) minor;
8922 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008923}
8924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008925void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008926 const unsigned char ver[2] )
8927{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928#if defined(MBEDTLS_SSL_PROTO_DTLS)
8929 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008930 {
8931 *major = 255 - ver[0] + 2;
8932 *minor = 255 - ver[1] + 1;
8933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008935 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8936 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008937 else
8938#else
8939 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008940#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008941 {
8942 *major = ver[0];
8943 *minor = ver[1];
8944 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008945}
8946
Simon Butcher99000142016-10-13 17:21:01 +01008947int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8948{
8949#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8950 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8951 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8952
8953 switch( md )
8954 {
8955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8956#if defined(MBEDTLS_MD5_C)
8957 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008958 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008959#endif
8960#if defined(MBEDTLS_SHA1_C)
8961 case MBEDTLS_SSL_HASH_SHA1:
8962 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8963 break;
8964#endif
8965#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8966#if defined(MBEDTLS_SHA512_C)
8967 case MBEDTLS_SSL_HASH_SHA384:
8968 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8969 break;
8970#endif
8971#if defined(MBEDTLS_SHA256_C)
8972 case MBEDTLS_SSL_HASH_SHA256:
8973 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8974 break;
8975#endif
8976 default:
8977 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8978 }
8979
8980 return 0;
8981#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8982 (void) ssl;
8983 (void) md;
8984
8985 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8986#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8987}
8988
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008989#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8990 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8991int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8992 unsigned char *output,
8993 unsigned char *data, size_t data_len )
8994{
8995 int ret = 0;
8996 mbedtls_md5_context mbedtls_md5;
8997 mbedtls_sha1_context mbedtls_sha1;
8998
8999 mbedtls_md5_init( &mbedtls_md5 );
9000 mbedtls_sha1_init( &mbedtls_sha1 );
9001
9002 /*
9003 * digitally-signed struct {
9004 * opaque md5_hash[16];
9005 * opaque sha_hash[20];
9006 * };
9007 *
9008 * md5_hash
9009 * MD5(ClientHello.random + ServerHello.random
9010 * + ServerParams);
9011 * sha_hash
9012 * SHA(ClientHello.random + ServerHello.random
9013 * + ServerParams);
9014 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009015 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009016 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009018 goto exit;
9019 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009020 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009021 ssl->handshake->randbytes, 64 ) ) != 0 )
9022 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009024 goto exit;
9025 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009026 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009027 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009029 goto exit;
9030 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009031 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009032 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009034 goto exit;
9035 }
9036
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009037 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009038 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009040 goto exit;
9041 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009042 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009043 ssl->handshake->randbytes, 64 ) ) != 0 )
9044 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009045 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009046 goto exit;
9047 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009048 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009049 data_len ) ) != 0 )
9050 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009052 goto exit;
9053 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009054 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009055 output + 16 ) ) != 0 )
9056 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009057 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009058 goto exit;
9059 }
9060
9061exit:
9062 mbedtls_md5_free( &mbedtls_md5 );
9063 mbedtls_sha1_free( &mbedtls_sha1 );
9064
9065 if( ret != 0 )
9066 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9067 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9068
9069 return( ret );
9070
9071}
9072#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9073 MBEDTLS_SSL_PROTO_TLS1_1 */
9074
9075#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9076 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9077int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009078 unsigned char *hash, size_t *hashlen,
9079 unsigned char *data, size_t data_len,
9080 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009081{
9082 int ret = 0;
9083 mbedtls_md_context_t ctx;
9084 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009085 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009086
9087 mbedtls_md_init( &ctx );
9088
9089 /*
9090 * digitally-signed struct {
9091 * opaque client_random[32];
9092 * opaque server_random[32];
9093 * ServerDHParams params;
9094 * };
9095 */
9096 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9097 {
9098 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9099 goto exit;
9100 }
9101 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9102 {
9103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9104 goto exit;
9105 }
9106 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9107 {
9108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9109 goto exit;
9110 }
9111 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9112 {
9113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9114 goto exit;
9115 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009116 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009117 {
9118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9119 goto exit;
9120 }
9121
9122exit:
9123 mbedtls_md_free( &ctx );
9124
9125 if( ret != 0 )
9126 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9127 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9128
9129 return( ret );
9130}
9131#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9132 MBEDTLS_SSL_PROTO_TLS1_2 */
9133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134#endif /* MBEDTLS_SSL_TLS_C */